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.SimpleSelectionDialog;
 
  23 import com.quantum.util.connection.ConnectionUtil;
 
  25 import org.eclipse.jface.dialogs.MessageDialog;
 
  26 import org.eclipse.jface.viewers.ILabelProviderListener;
 
  27 import org.eclipse.jface.viewers.ISelection;
 
  28 import org.eclipse.jface.viewers.ISelectionChangedListener;
 
  29 import org.eclipse.jface.viewers.IStructuredContentProvider;
 
  30 import org.eclipse.jface.viewers.IStructuredSelection;
 
  31 import org.eclipse.jface.viewers.ITableLabelProvider;
 
  32 import org.eclipse.jface.viewers.SelectionChangedEvent;
 
  33 import org.eclipse.jface.viewers.TableViewer;
 
  34 import org.eclipse.jface.viewers.Viewer;
 
  35 import org.eclipse.swt.SWT;
 
  36 import org.eclipse.swt.events.SelectionAdapter;
 
  37 import org.eclipse.swt.events.SelectionEvent;
 
  38 import org.eclipse.swt.graphics.Image;
 
  39 import org.eclipse.swt.layout.GridData;
 
  40 import org.eclipse.swt.layout.GridLayout;
 
  41 import org.eclipse.swt.widgets.Button;
 
  42 import org.eclipse.swt.widgets.Composite;
 
  48 public class SchemaSelectionControl extends Composite {
 
  50     class ContentProviderImpl implements IStructuredContentProvider {
 
  51         public Object[] getElements(Object inputElement) {
 
  52             List list = new ArrayList((Collection) inputElement);
 
  53             Collections.sort(list);
 
  54             return list.toArray();
 
  57         public void dispose() {
 
  60         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
 
  64     class LabelProviderImpl implements ITableLabelProvider {
 
  66         public Image getColumnImage(Object element, int columnIndex) {
 
  67             if (columnIndex == 0) {
 
  68                 return ImageStore.getImage(ImageStore.SCHEMA);
 
  74         public String getColumnText(Object element, int columnIndex) {
 
  75             if (columnIndex == 0) {
 
  76                 return ((Schema) element).getDisplayName();
 
  82         public void addListener(ILabelProviderListener listener) {
 
  85         public void dispose() {
 
  88         public boolean isLabelProperty(Object element, String property) {
 
  89             return "displayName".equals(property);
 
  92         public void removeListener(ILabelProviderListener listener) {
 
  97     private final Bookmark bookmarkForConnection;
 
  98     private ConnectionUtil connectionUtil = new ConnectionUtil();
 
  99         private Set schemas = Collections.synchronizedSet(new HashSet());
 
 100         private TableViewer schemaTable;
 
 101         private Button useAllSchemasButton;
 
 103         private int schemaRule;
 
 105         private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
 
 106         private Button useUsernameAsSchemaButton;
 
 107         private Button useSelectedSchemasButton;
 
 108         private Button removeButton;
 
 109         private Button addButton;
 
 115         public SchemaSelectionControl(Composite parent, Bookmark bookmarkForConnection) {
 
 116                 super(parent, SWT.NONE);
 
 117                 this.bookmarkForConnection = bookmarkForConnection;
 
 119                 Schema[] schemas = bookmarkForConnection.getSchemaSelections();
 
 120                 this.schemas.addAll(Arrays.asList(schemas));
 
 121                 this.schemaRule = this.bookmarkForConnection.getSchemaRule();
 
 125         protected void createContents() {
 
 127         GridLayout layout = new GridLayout();
 
 128         layout.numColumns = 1;
 
 130         GridData data = new GridData(GridData.FILL_BOTH);
 
 133         this.useAllSchemasButton = new Button(this, SWT.RADIO);
 
 134         this.useAllSchemasButton.setText(Messages.getString(getClass(), "useAllSchemas")); //$NON-NLS-1$
 
 135                 this.useAllSchemasButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
 136                 this.useAllSchemasButton.addSelectionListener(new SelectionAdapter() {
 
 137                         public void widgetSelected(SelectionEvent event) {
 
 138                                 setSchemaRule(Bookmark.SCHEMA_RULE_USE_ALL);
 
 143         this.useUsernameAsSchemaButton = new Button(this, SWT.RADIO);
 
 144         this.useUsernameAsSchemaButton.setText(Messages.getString(getClass(), "useUsernameAsSchema")); //$NON-NLS-1$
 
 145         this.useUsernameAsSchemaButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
 146                 this.useUsernameAsSchemaButton.addSelectionListener(new SelectionAdapter() {
 
 147                         public void widgetSelected(SelectionEvent event) {
 
 148                                 setSchemaRule(Bookmark.SCHEMA_RULE_USE_DEFAULT);
 
 153         this.useSelectedSchemasButton = new Button(this, SWT.RADIO);
 
 154         this.useSelectedSchemasButton.setText(Messages.getString(getClass(), "useSelectedSchemas")); //$NON-NLS-1$
 
 155         this.useSelectedSchemasButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
 156                 this.useSelectedSchemasButton.addSelectionListener(new SelectionAdapter() {
 
 157                         public void widgetSelected(SelectionEvent event) {
 
 158                                 setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
 
 163                 Composite composite = new Composite(this, SWT.NONE);
 
 164         layout = new GridLayout();
 
 165         layout.numColumns = 2;
 
 166         composite.setLayout(layout);
 
 167         data = new GridData(GridData.FILL_BOTH);
 
 168         composite.setLayoutData(data);
 
 170         this.schemaTable = new TableViewer(composite, 
 
 171                                             SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
 
 172                 layout = new GridLayout();
 
 173         layout.marginWidth = 5;
 
 174         layout.marginHeight = 5;
 
 176         this.schemaTable.getTable().setLayout(layout);
 
 177         data = new GridData(GridData.FILL_BOTH);
 
 178         this.schemaTable.getTable().setLayoutData(data);
 
 179         this.schemaTable.setLabelProvider(new LabelProviderImpl());
 
 180         this.schemaTable.setContentProvider(new ContentProviderImpl());
 
 181         this.schemaTable.setInput(this.schemas);
 
 183         createButtonArea(composite);
 
 188         private void updateControls() {
 
 189                 this.useAllSchemasButton.setSelection(this.schemaRule == Bookmark.SCHEMA_RULE_USE_ALL);
 
 190                 this.useUsernameAsSchemaButton.setSelection(this.schemaRule == Bookmark.SCHEMA_RULE_USE_DEFAULT);
 
 192                 boolean enabled = (this.schemaRule != Bookmark.SCHEMA_RULE_USE_ALL
 
 193                                 && this.schemaRule != Bookmark.SCHEMA_RULE_USE_DEFAULT);
 
 195                 this.useSelectedSchemasButton.setSelection(enabled);
 
 196         this.schemaTable.getControl().setEnabled(enabled);
 
 198         this.addButton.setEnabled(enabled);
 
 199         this.removeButton.setEnabled(
 
 200                         enabled && !this.schemaTable.getSelection().isEmpty());
 
 203     private void createButtonArea(Composite composite) {
 
 204         Composite buttonArea = new Composite(composite, SWT.NONE);
 
 205         GridLayout layout = new GridLayout();
 
 206         layout.numColumns = 1;
 
 207         buttonArea.setLayout(layout);
 
 208         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
 
 209         buttonArea.setLayoutData(data);
 
 211         this.addButton = new Button(buttonArea, SWT.NULL);
 
 212         this.addButton.setText("Add");
 
 213         data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
 
 214         this.addButton.setLayoutData(data);
 
 215         this.addButton.addSelectionListener(new SelectionAdapter() {
 
 216             public void widgetSelected(SelectionEvent event) {
 
 221         this.removeButton = new Button(buttonArea, SWT.NULL);
 
 222         this.removeButton.setText("Remove");
 
 223         this.removeButton.setEnabled(false);
 
 224         data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
 
 225         this.removeButton.setLayoutData(data);
 
 226         this.removeButton.addSelectionListener(new SelectionAdapter() {
 
 227             public void widgetSelected(SelectionEvent event) {
 
 228                 removeSchema(SchemaSelectionControl.this.schemaTable.getSelection());
 
 232         schemaTable.addSelectionChangedListener(new ISelectionChangedListener() {
 
 233             public void selectionChanged(SelectionChangedEvent event) {
 
 238     private void addSchema() {
 
 239         Bookmark bookmark = getBookmark();
 
 240         boolean isAlreadyConnected = bookmark.isConnected();
 
 242         if (!isAlreadyConnected) {
 
 243             boolean confirmed = MessageDialog.openConfirm(getShell(), 
 
 244                         Messages.getString(getClass(), "connectTitle"),
 
 245                         Messages.getString(getClass(), "connectMessage"));
 
 247                 this.connectionUtil.connect(bookmark, getShell());
 
 252             if (bookmark.isConnected()) {
 
 253                 List schemaList = getAllUnselectedSchemas(bookmark);
 
 254                 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
 
 255                                 getShell(), Messages.getString(getClass(), "addSchemaDialog"), 
 
 256                                                 schemaList.toArray(), 
 
 257                                                 ImageStore.getImage(ImageStore.SCHEMA), true);
 
 258                 int result = dialog.open();
 
 259                 if (result == SimpleSelectionDialog.OK 
 
 260                                 && !dialog.getSelection().isEmpty()) {
 
 261                     for (Iterator i = dialog.getSelection().iterator(); i.hasNext();) {
 
 262                         this.schemas.add(i.next());
 
 266                         this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
 
 269                 if (!isAlreadyConnected) {
 
 270                     bookmark.disconnect();
 
 273         } catch (ConnectionException e) {
 
 274             ExceptionDisplayDialog.openError(getShell(), null, null, e);
 
 275         } catch (SQLException e) {
 
 276             ExceptionDisplayDialog.openError(getShell(), null, null, e);
 
 283          * @throws NotConnectedException
 
 284          * @throws SQLException
 
 286         private List getAllUnselectedSchemas(Bookmark bookmark) 
 
 287                         throws NotConnectedException, SQLException {
 
 288                 Schema[] schemas = bookmark.getDatabase().getSchemas();
 
 289                 List schemaList = new ArrayList(Arrays.asList(schemas));
 
 290                 schemaList.removeAll(this.schemas);
 
 291                 Collections.sort(schemaList);
 
 295         private void removeSchema(ISelection selection) {
 
 296         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
 
 297         for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
 
 298             Schema element = (Schema) i.next();
 
 299             this.schemas.remove(element);
 
 302         this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
 
 305     private Bookmark getBookmark() {
 
 306         return this.bookmarkForConnection;
 
 309     private void refreshTable() {
 
 310         this.schemaTable.refresh();
 
 313     public Schema[] getSchemas() {
 
 314         return (Schema[]) this.schemas.toArray(new Schema[this.schemas.size()]);
 
 317     public void setSchemas(Schema[] schemas) {
 
 318         this.schemas.clear();
 
 319         this.schemas.addAll(Arrays.asList(schemas));
 
 322         this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
 
 324         public void addPropertyChangeListener(PropertyChangeListener arg0) {
 
 325                 this.propertyChangeSupport.addPropertyChangeListener(arg0);
 
 327         public void removePropertyChangeListener(PropertyChangeListener arg0) {
 
 328                 this.propertyChangeSupport.removePropertyChangeListener(arg0);
 
 330         public int getSchemaRule() {
 
 331                 return this.schemaRule;
 
 333         public void setSchemaRule(int schemaRule) {
 
 334                 if (schemaRule != this.schemaRule) {
 
 335                         int original = this.schemaRule;
 
 336                         this.schemaRule = schemaRule;
 
 339                         this.propertyChangeSupport.firePropertyChange(
 
 340                                         "schemaRule", original, schemaRule);