initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / BookmarkPropertyPage.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/BookmarkPropertyPage.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/BookmarkPropertyPage.java
new file mode 100644 (file)
index 0000000..42c4065
--- /dev/null
@@ -0,0 +1,284 @@
+package com.quantum.properties;
+
+import com.quantum.IQuantumConstants;
+import com.quantum.adapters.AdapterFactory;
+import com.quantum.adapters.DriverInfo;
+import com.quantum.model.Bookmark;
+import com.quantum.view.bookmark.TreeNode;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.TabFolder;
+import org.eclipse.swt.widgets.TabItem;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.dialogs.PropertyPage;
+
+public class BookmarkPropertyPage extends PropertyPage {
+    
+    private Text password;
+    private Text userid;
+    private Button prompt;
+    
+    private Text jdbcURL;
+    private Text driverName;
+    private Text driverPath;
+    
+    private Combo type;
+       private Combo autoCommit;
+    private DriverInfo[] adapters = AdapterFactory.getInstance().getDriverList();
+
+    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(GridData.FILL);
+        data.grabExcessHorizontalSpace = true;
+        composite.setLayoutData(data);
+
+               Label nameLabel = new Label(composite, SWT.NONE);
+        nameLabel.setText("Name:");
+
+        Label name = new Label(composite, SWT.NONE);
+
+        Bookmark bookmark = getBookmark();
+        String description = bookmark.getName();
+        name.setText(description);
+        
+        TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
+        layout = new GridLayout();
+        tabFolder.setLayout(layout);
+        data = new GridData(GridData.FILL_BOTH);
+        data.grabExcessHorizontalSpace = true;
+        data.grabExcessVerticalSpace = true;
+        data.verticalAlignment = GridData.FILL;
+        data.horizontalSpan = 2;
+        data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+        tabFolder.setLayoutData(data);
+        
+        createUserTab(tabFolder);
+        createDriverTab(tabFolder);
+        createOptionsTab(tabFolder);
+        
+        performDefaults();
+        return composite;
+       }
+
+       private Bookmark getBookmark() {
+        Bookmark bookmark =
+            ((TreeNode) getElement()).getBookmark();
+        return bookmark;
+    }
+
+    private void createDriverTab(TabFolder tabFolder) {
+        TabItem driverTab = new TabItem(tabFolder, SWT.NONE);
+        driverTab.setText("JDBC Driver");
+        
+        Composite composite = new Composite(tabFolder, SWT.NONE);
+        GridLayout layout = new GridLayout();
+        layout.numColumns = 2;
+        composite.setLayout(layout);
+        GridData data = new GridData(GridData.FILL);
+        data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+        data.grabExcessHorizontalSpace = true;
+        composite.setLayoutData(data);
+        
+        Label label = new Label(composite, SWT.NONE);
+        label.setText("Connection URL:");
+
+        this.jdbcURL = new Text(composite, SWT.BORDER);
+        data = new GridData(GridData.FILL);
+        data.horizontalAlignment = GridData.FILL;
+        data.grabExcessHorizontalSpace = true;
+        data.grabExcessHorizontalSpace = true;
+        this.jdbcURL.setLayoutData(data);
+        
+        label = new Label(composite, SWT.NONE);
+        label.setText("Driver Name:");
+
+        this.driverName = new Text(composite, SWT.BORDER);
+        data = new GridData(GridData.FILL);
+        data.horizontalAlignment = GridData.FILL;
+        data.grabExcessHorizontalSpace = true;
+        this.driverName.setLayoutData(data);
+
+        label = new Label(composite, SWT.NONE);
+        label.setText("Driver Location:");
+
+        this.driverPath = new Text(composite, SWT.BORDER);
+        data = new GridData(GridData.FILL);
+        data.horizontalAlignment = GridData.FILL;
+        data.grabExcessHorizontalSpace = true;
+        this.driverPath.setLayoutData(data);
+
+        label = new Label(composite, SWT.NULL);
+        label.setText("Type:");
+        this.type = new Combo(composite, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
+        String adapterNames[] = new String[adapters.length];
+        for (int i = 0; i < adapters.length; i++) {
+            adapterNames[i] = adapters[i].getDisplayName();
+        }
+        this.type.setItems(adapterNames);
+        
+        data = new GridData();
+        data.horizontalAlignment = GridData.FILL;
+        this.type.setLayoutData(data);
+
+        driverTab.setControl(composite);
+    }
+
+    private void createUserTab(TabFolder tabFolder) {
+        TabItem userTab = new TabItem(tabFolder, SWT.NONE);
+        userTab.setText("User");
+        
+        Composite composite = new Composite(tabFolder, SWT.NONE);
+        GridLayout layout = new GridLayout();
+        layout.numColumns = 2;
+        composite.setLayout(layout);
+        GridData data = new GridData(GridData.FILL);
+        data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+        data.grabExcessHorizontalSpace = true;
+        composite.setLayoutData(data);
+        
+        Label useridLabel = new Label(composite, SWT.NONE);
+        useridLabel.setText("Userid:");
+
+        this.userid = new Text(composite, SWT.BORDER);
+        data = new GridData(GridData.FILL);
+        data.horizontalAlignment = GridData.FILL;
+        data.grabExcessHorizontalSpace = true;
+        data.grabExcessHorizontalSpace = true;
+        this.userid.setLayoutData(data);
+        
+        Label passworLabel = new Label(composite, SWT.NONE);
+        passworLabel.setText("Password:");
+
+        this.password = new Text(composite, SWT.BORDER);
+        this.password.setEchoChar('*');
+        data = new GridData(GridData.FILL);
+        data.horizontalAlignment = GridData.FILL;
+        data.grabExcessHorizontalSpace = true;
+        this.password.setLayoutData(data);
+
+        this.prompt = new Button(composite, SWT.CHECK);
+        this.prompt.setText("Prompt for password");
+        data = new GridData(GridData.FILL);
+        data.horizontalSpan = 2;
+        data.horizontalAlignment = GridData.FILL;
+        data.grabExcessHorizontalSpace = true;
+        this.prompt.setLayoutData(data);
+        
+        this.prompt.addSelectionListener(new SelectionAdapter() {
+            public void widgetSelected(SelectionEvent event) {
+                password.setEditable(!((Button) event.getSource()).getSelection()); 
+            }
+        });
+        
+        userTab.setControl(composite);
+    }
+
+       /**
+         * @param tabFolder
+         */
+        private void createOptionsTab(TabFolder tabFolder) {
+               TabItem optionsTab = new TabItem(tabFolder, SWT.NONE);
+               optionsTab.setText("Options");
+        
+               Composite composite = new Composite(tabFolder, SWT.NONE);
+               GridLayout layout = new GridLayout();
+               layout.numColumns = 2;
+               composite.setLayout(layout);
+               GridData data = new GridData(GridData.FILL);
+               data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
+               data.grabExcessHorizontalSpace = true;
+               composite.setLayoutData(data);
+        
+               Label label;
+               
+               label = new Label(composite, SWT.NULL);
+               label.setText("On connection, Auto-Commit should be:");
+               this.autoCommit = new Combo(composite, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
+               String autoCommitTypes[] = new String[] {
+                       IQuantumConstants.autoCommitTrue, 
+                       IQuantumConstants.autoCommitFalse,
+                       IQuantumConstants.autoCommitSaved
+               };
+               this.autoCommit.setItems(autoCommitTypes);
+        
+               data = new GridData();
+               data.horizontalAlignment = GridData.FILL;
+               this.autoCommit.setLayoutData(data);
+
+               optionsTab.setControl(composite);
+               
+        }
+
+
+
+    /**
+     * @see org.eclipse.jface.preference.PreferencePage#performApply()
+     */
+    public boolean performOk() {
+        Bookmark bookmark = getBookmark();
+        bookmark.setUsername(this.userid.getText());
+        bookmark.setPromptForPassword(this.prompt.getSelection());
+        if (this.prompt.getSelection()) {
+            bookmark.setPassword("");
+        } else {
+            bookmark.setPassword(this.password.getText());
+        }
+
+        int index = this.type.getSelectionIndex();
+        bookmark.setType(this.adapters[index].getDriverType());
+        bookmark.setConnect(this.jdbcURL.getText());
+        bookmark.setDriver(this.driverName.getText());
+        bookmark.setDriverFile(this.driverPath.getText());
+        if (this.autoCommit.getSelectionIndex() >= 0)
+               bookmark.setAutoCommitPreference(this.autoCommit.getItem(this.autoCommit.getSelectionIndex()));
+        return super.performOk();
+    }
+    /* (non-Javadoc)
+     * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
+     */
+    protected void performDefaults() {
+        super.performDefaults();
+        Bookmark bookmark = getBookmark();
+
+        this.prompt.setSelection(bookmark.getPromptForPassword());
+        this.password.setEditable(!bookmark.getPromptForPassword());
+        this.password.setText(bookmark.getPassword());
+        this.userid.setText(bookmark.getUsername());
+
+        this.type.select(0);
+        boolean done = false;
+        for (int i = 0,
+            length = (adapters == null) ? 0 : adapters.length;
+            !done && i < length;
+            i++) {
+            if (bookmark.getType() != null && 
+                bookmark.getType().equals(adapters[i].getDriverType())) {
+                this.type.select(i);
+                done = true;
+            }
+        }
+        if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitTrue))
+               this.autoCommit.select(0);
+        else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitFalse))
+                       this.autoCommit.select(1);
+               else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitSaved))
+                       this.autoCommit.select(2);
+                       
+        this.driverName.setText(bookmark.getDriver());
+        this.jdbcURL.setText(bookmark.getConnect());
+        this.driverPath.setText(bookmark.getDriverFile());
+    }
+}