initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / SchemaPropertyPage.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/SchemaPropertyPage.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/SchemaPropertyPage.java
new file mode 100644 (file)
index 0000000..d812f59
--- /dev/null
@@ -0,0 +1,256 @@
+package com.quantum.properties;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
+
+import com.quantum.QuantumPlugin;
+import com.quantum.model.Bookmark;
+import com.quantum.model.BookmarkHolder;
+import com.quantum.model.ConnectionException;
+import com.quantum.model.Schema;
+import com.quantum.sql.SQLHelper;
+import com.quantum.ui.dialog.ExceptionDisplayDialog;
+import com.quantum.util.connection.ConnectionUtil;
+import com.quantum.view.bookmark.AddSchemaDialog;
+
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ILabelProviderListener;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITableLabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.ui.dialogs.PropertyPage;
+
+public class SchemaPropertyPage extends PropertyPage {
+    
+    class ContentProviderImpl implements IStructuredContentProvider {
+        public Object[] getElements(Object inputElement) {
+            List list = new ArrayList((Collection) inputElement);
+            Collections.sort(list);
+            return list.toArray();
+        }
+
+        public void dispose() {
+        }
+
+        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+        }
+    }
+    
+    class LabelProviderImpl implements ITableLabelProvider {
+
+        public Image getColumnImage(Object element, int columnIndex) {
+            if (columnIndex == 0) {
+                return QuantumPlugin.getImage("schema.gif");
+            } else {
+                return null;
+            }
+        }
+
+        public String getColumnText(Object element, int columnIndex) {
+            if (columnIndex == 0) {
+                return ((Schema) element).getDisplayName();
+            } else {
+                return null;
+            }
+        }
+
+        public void addListener(ILabelProviderListener listener) {
+        }
+
+        public void dispose() {
+        }
+
+        public boolean isLabelProperty(Object element, String property) {
+            return "displayName".equals(property);
+        }
+
+        public void removeListener(ILabelProviderListener listener) {
+        }
+    }
+    
+    private Set schemas = Collections.synchronizedSet(new HashSet());
+    private TableViewer schemaTable;
+    private Button addButton;
+    private Button removeButton;
+    
+    private ConnectionUtil connectionUtil = new ConnectionUtil();
+    
+    protected Control createContents(Composite parent) {
+
+        Composite composite = new Composite(parent, SWT.NONE);
+        GridLayout layout = new GridLayout();
+        layout.numColumns = 2;
+        composite.setLayout(layout);
+        GridData data = new GridData();
+        composite.setLayoutData(data);
+
+        this.schemaTable = new TableViewer(composite, 
+            SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
+            
+        layout = new GridLayout();
+        layout.marginWidth = 5;
+        layout.marginHeight = 5;
+        
+        this.schemaTable.getTable().setLayout(layout);
+        data = new GridData();
+        data.heightHint = 200;
+        data.widthHint = 200;
+//        data.verticalSpan = 2;
+        this.schemaTable.getTable().setLayoutData(data);
+        this.schemaTable.setLabelProvider(new LabelProviderImpl());
+        this.schemaTable.setContentProvider(new ContentProviderImpl());
+        this.schemaTable.setInput(this.schemas);
+        
+        createButtonArea(composite);
+        
+        performDefaults();
+        return composite;
+    }
+
+    private void createButtonArea(Composite composite) {
+        GridLayout layout;
+        GridData data;
+        Composite buttonArea = new Composite(composite, SWT.NONE);
+        layout = new GridLayout();
+        layout.numColumns = 1;
+        buttonArea.setLayout(layout);
+        data = new GridData();
+        data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+        buttonArea.setLayoutData(data);
+        
+        this.addButton = new Button(buttonArea, SWT.NULL);
+        this.addButton.setText("Add");
+        data = new GridData();
+        data.horizontalAlignment = GridData.FILL_HORIZONTAL;
+        data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+        data.widthHint = 60;
+        this.addButton.setLayoutData(data);
+        this.addButton.addSelectionListener(new SelectionAdapter() {
+            public void widgetSelected(SelectionEvent event) {
+                addSchema();
+            }
+        });
+        
+        this.removeButton = new Button(buttonArea, SWT.NULL);
+        this.removeButton.setText("Remove");
+        this.removeButton.setEnabled(false);
+        data = new GridData();
+        data.horizontalAlignment = GridData.FILL_HORIZONTAL;
+        data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+        data.widthHint = 60;
+        this.removeButton.setLayoutData(data);
+        this.removeButton.addSelectionListener(new SelectionAdapter() {
+            public void widgetSelected(SelectionEvent event) {
+                removeSchema(SchemaPropertyPage.this.schemaTable.getSelection());
+            }
+        });
+        
+        this.schemaTable.addSelectionChangedListener(new ISelectionChangedListener() {
+            public void selectionChanged(SelectionChangedEvent event) {
+                SchemaPropertyPage.this.removeButton.setEnabled(
+                    !event.getSelection().isEmpty());
+            }
+        });
+    }
+    
+    private void addSchema() {
+        Bookmark bookmark = getBookmark();
+        boolean isAlreadyConnected = bookmark.isConnected();
+        
+        if (!isAlreadyConnected) {
+            boolean confirmed = MessageDialog.openConfirm(getShell(), "Connect Required", 
+                "We must connect to the database to retrieve schemas.");
+            if (confirmed) {
+                this.connectionUtil.connect(bookmark, getShell());
+            }
+        }
+        
+        try {
+            if (bookmark.isConnected()) {
+                Vector schemas = SQLHelper.getSchemas(bookmark.getConnection());
+                AddSchemaDialog dialog = new AddSchemaDialog(getShell(), schemas);
+                dialog.open();
+                if (dialog.getSelectedSchemas() != null) {
+                    Collection temp = dialog.getSelectedSchemas();
+                    for (Iterator i = temp.iterator(); i.hasNext();) {
+                        String name = (String) i.next();
+                        this.schemas.add(new Schema(name));
+                    }
+                    refreshTable();
+                }
+    
+                if (!isAlreadyConnected) {
+                    bookmark.disconnect();
+                }
+            }
+        } catch (ConnectionException e) {
+            ExceptionDisplayDialog.openError(getShell(), null, null, e);
+        }
+    }
+    
+    private void removeSchema(ISelection selection) {
+        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
+        for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
+            Schema element = (Schema) i.next();
+            this.schemas.remove(element);
+        }
+        refreshTable();
+    }
+
+    private Bookmark getBookmark() {
+        Bookmark bookmark =
+            ((BookmarkHolder) getElement()).getBookmark();
+        return bookmark;
+    }
+
+    /**
+     * @see org.eclipse.jface.preference.PreferencePage#performApply()
+     */
+    public boolean performOk() {
+        getBookmark().setSchemas((Schema[]) this.schemas.toArray(
+            new Schema[this.schemas.size()]));
+        return true;
+    }
+    /* (non-Javadoc)
+     * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
+     */
+    protected void performDefaults() {
+        super.performDefaults();
+        Bookmark bookmark = getBookmark();
+
+        this.schemas.clear();
+        Schema[] schemas = bookmark.getSchemas();
+        for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
+            i < length;
+            i++) {
+            if (!schemas[i].isDefault()) {
+                this.schemas.add(schemas[i]);
+            }
+        }
+        refreshTable();
+    }
+
+    private void refreshTable() {
+        this.schemaTable.refresh();
+    }
+}
\ No newline at end of file