1 package com.quantum.view;
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
15 import com.quantum.ImageStore;
16 import com.quantum.Messages;
17 import com.quantum.model.Bookmark;
18 import com.quantum.model.ConnectionException;
19 import com.quantum.model.NotConnectedException;
20 import com.quantum.model.Schema;
21 import com.quantum.ui.dialog.ExceptionDisplayDialog;
22 import com.quantum.ui.dialog.SQLExceptionDialog;
23 import com.quantum.ui.dialog.SimpleSelectionDialog;
24 import com.quantum.util.connection.ConnectionUtil;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.viewers.ILabelProviderListener;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredContentProvider;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.ITableLabelProvider;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.viewers.Viewer;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Composite;
49 public class SchemaSelectionControl extends Composite {
51 class ContentProviderImpl implements IStructuredContentProvider {
52 public Object[] getElements(Object inputElement) {
53 List list = new ArrayList((Collection) inputElement);
54 Collections.sort(list);
55 return list.toArray();
58 public void dispose() {
61 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
65 class LabelProviderImpl implements ITableLabelProvider {
67 public Image getColumnImage(Object element, int columnIndex) {
68 if (columnIndex == 0) {
69 return ImageStore.getImage(ImageStore.SCHEMA);
75 public String getColumnText(Object element, int columnIndex) {
76 if (columnIndex == 0) {
77 return ((Schema) element).getDisplayName();
83 public void addListener(ILabelProviderListener listener) {
86 public void dispose() {
89 public boolean isLabelProperty(Object element, String property) {
90 return "displayName".equals(property);
93 public void removeListener(ILabelProviderListener listener) {
98 private final Bookmark bookmarkForConnection;
99 private ConnectionUtil connectionUtil = new ConnectionUtil();
100 private Set schemas = Collections.synchronizedSet(new HashSet());
101 private TableViewer schemaTable;
102 private Button useAllSchemasButton;
104 private int schemaRule;
106 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
107 private Button useUsernameAsSchemaButton;
108 private Button useSelectedSchemasButton;
109 private Button removeButton;
110 private Button addButton;
116 public SchemaSelectionControl(Composite parent, Bookmark bookmarkForConnection) {
117 super(parent, SWT.NONE);
118 this.bookmarkForConnection = bookmarkForConnection;
120 Schema[] schemas = bookmarkForConnection.getSchemaSelections();
121 this.schemas.addAll(Arrays.asList(schemas));
122 this.schemaRule = this.bookmarkForConnection.getSchemaRule();
126 protected void createContents() {
128 GridLayout layout = new GridLayout();
129 layout.numColumns = 1;
131 GridData data = new GridData(GridData.FILL_BOTH);
134 this.useAllSchemasButton = new Button(this, SWT.RADIO);
135 this.useAllSchemasButton.setText(Messages.getString(getClass(), "useAllSchemas")); //$NON-NLS-1$
136 this.useAllSchemasButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
137 this.useAllSchemasButton.addSelectionListener(new SelectionAdapter() {
138 public void widgetSelected(SelectionEvent event) {
139 setSchemaRule(Bookmark.SCHEMA_RULE_USE_ALL);
144 this.useUsernameAsSchemaButton = new Button(this, SWT.RADIO);
145 this.useUsernameAsSchemaButton.setText(Messages.getString(getClass(), "useUsernameAsSchema")); //$NON-NLS-1$
146 this.useUsernameAsSchemaButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
147 this.useUsernameAsSchemaButton.addSelectionListener(new SelectionAdapter() {
148 public void widgetSelected(SelectionEvent event) {
149 setSchemaRule(Bookmark.SCHEMA_RULE_USE_DEFAULT);
154 this.useSelectedSchemasButton = new Button(this, SWT.RADIO);
155 this.useSelectedSchemasButton.setText(Messages.getString(getClass(), "useSelectedSchemas")); //$NON-NLS-1$
156 this.useSelectedSchemasButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
157 this.useSelectedSchemasButton.addSelectionListener(new SelectionAdapter() {
158 public void widgetSelected(SelectionEvent event) {
159 setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
164 Composite composite = new Composite(this, SWT.NONE);
165 layout = new GridLayout();
166 layout.numColumns = 2;
167 composite.setLayout(layout);
168 data = new GridData(GridData.FILL_BOTH);
169 composite.setLayoutData(data);
171 this.schemaTable = new TableViewer(composite,
172 SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
173 layout = new GridLayout();
174 layout.marginWidth = 5;
175 layout.marginHeight = 5;
177 this.schemaTable.getTable().setLayout(layout);
178 data = new GridData(GridData.FILL_BOTH);
179 this.schemaTable.getTable().setLayoutData(data);
180 this.schemaTable.setLabelProvider(new LabelProviderImpl());
181 this.schemaTable.setContentProvider(new ContentProviderImpl());
182 this.schemaTable.setInput(this.schemas);
184 createButtonArea(composite);
189 private void updateControls() {
190 this.useAllSchemasButton.setSelection(this.schemaRule == Bookmark.SCHEMA_RULE_USE_ALL);
191 this.useUsernameAsSchemaButton.setSelection(this.schemaRule == Bookmark.SCHEMA_RULE_USE_DEFAULT);
193 boolean enabled = (this.schemaRule != Bookmark.SCHEMA_RULE_USE_ALL
194 && this.schemaRule != Bookmark.SCHEMA_RULE_USE_DEFAULT);
196 this.useSelectedSchemasButton.setSelection(enabled);
197 this.schemaTable.getControl().setEnabled(enabled);
199 this.addButton.setEnabled(enabled);
200 this.removeButton.setEnabled(
201 enabled && !this.schemaTable.getSelection().isEmpty());
204 private void createButtonArea(Composite composite) {
205 Composite buttonArea = new Composite(composite, SWT.NONE);
206 GridLayout layout = new GridLayout();
207 layout.numColumns = 1;
208 buttonArea.setLayout(layout);
209 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
210 buttonArea.setLayoutData(data);
212 this.addButton = new Button(buttonArea, SWT.NULL);
213 this.addButton.setText("Add");
214 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
215 this.addButton.setLayoutData(data);
216 this.addButton.addSelectionListener(new SelectionAdapter() {
217 public void widgetSelected(SelectionEvent event) {
222 this.removeButton = new Button(buttonArea, SWT.NULL);
223 this.removeButton.setText("Remove");
224 this.removeButton.setEnabled(false);
225 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
226 this.removeButton.setLayoutData(data);
227 this.removeButton.addSelectionListener(new SelectionAdapter() {
228 public void widgetSelected(SelectionEvent event) {
229 removeSchema(SchemaSelectionControl.this.schemaTable.getSelection());
233 schemaTable.addSelectionChangedListener(new ISelectionChangedListener() {
234 public void selectionChanged(SelectionChangedEvent event) {
239 private void addSchema() {
240 Bookmark bookmark = getBookmark();
241 boolean isAlreadyConnected = bookmark.isConnected();
243 if (!isAlreadyConnected) {
244 boolean confirmed = MessageDialog.openConfirm(getShell(),
245 Messages.getString(getClass(), "connectTitle"),
246 Messages.getString(getClass(), "connectMessage"));
248 this.connectionUtil.connect(bookmark, getShell());
253 if (bookmark.isConnected()) {
254 List schemaList = getAllUnselectedSchemas(bookmark);
255 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
256 getShell(), Messages.getString(getClass(), "addSchemaDialog"),
257 schemaList.toArray(),
258 ImageStore.getImage(ImageStore.SCHEMA), true);
259 int result = dialog.open();
260 if (result == SimpleSelectionDialog.OK
261 && !dialog.getSelection().isEmpty()) {
262 for (Iterator i = dialog.getSelection().iterator(); i.hasNext();) {
263 this.schemas.add(i.next());
267 this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
270 if (!isAlreadyConnected) {
271 bookmark.disconnect();
274 } catch (ConnectionException e) {
275 ExceptionDisplayDialog.openError(getShell(), null, null, e);
276 } catch (SQLException e) {
277 SQLExceptionDialog.openException(getShell(), bookmark, e);
284 * @throws NotConnectedException
285 * @throws SQLException
287 private List getAllUnselectedSchemas(Bookmark bookmark)
288 throws NotConnectedException, SQLException {
289 Schema[] schemas = bookmark.getDatabase().getSchemas();
290 List schemaList = new ArrayList(Arrays.asList(schemas));
291 schemaList.removeAll(this.schemas);
292 Collections.sort(schemaList);
296 private void removeSchema(ISelection selection) {
297 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
298 for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
299 Schema element = (Schema) i.next();
300 this.schemas.remove(element);
303 this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
306 private Bookmark getBookmark() {
307 return this.bookmarkForConnection;
310 private void refreshTable() {
311 this.schemaTable.refresh();
314 public Schema[] getSchemas() {
315 return (Schema[]) this.schemas.toArray(new Schema[this.schemas.size()]);
318 public void setSchemas(Schema[] schemas) {
319 this.schemas.clear();
320 this.schemas.addAll(Arrays.asList(schemas));
323 this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
325 public void addPropertyChangeListener(PropertyChangeListener arg0) {
326 this.propertyChangeSupport.addPropertyChangeListener(arg0);
328 public void removePropertyChangeListener(PropertyChangeListener arg0) {
329 this.propertyChangeSupport.removePropertyChangeListener(arg0);
331 public int getSchemaRule() {
332 return this.schemaRule;
334 public void setSchemaRule(int schemaRule) {
335 if (schemaRule != this.schemaRule) {
336 int original = this.schemaRule;
337 this.schemaRule = schemaRule;
340 this.propertyChangeSupport.firePropertyChange(
341 "schemaRule", original, schemaRule);