initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / BookmarkPropertyPage.java
1 package com.quantum.properties;
2
3 import com.quantum.IQuantumConstants;
4 import com.quantum.adapters.AdapterFactory;
5 import com.quantum.adapters.DriverInfo;
6 import com.quantum.model.Bookmark;
7 import com.quantum.view.bookmark.TreeNode;
8
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.SelectionAdapter;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Combo;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.TabFolder;
20 import org.eclipse.swt.widgets.TabItem;
21 import org.eclipse.swt.widgets.Text;
22 import org.eclipse.ui.dialogs.PropertyPage;
23
24 public class BookmarkPropertyPage extends PropertyPage {
25     
26     private Text password;
27     private Text userid;
28     private Button prompt;
29     
30     private Text jdbcURL;
31     private Text driverName;
32     private Text driverPath;
33     
34     private Combo type;
35         private Combo autoCommit;
36     private DriverInfo[] adapters = AdapterFactory.getInstance().getDriverList();
37
38     protected Control createContents(Composite parent) {
39
40         Composite composite = new Composite(parent, SWT.NONE);
41         GridLayout layout = new GridLayout();
42         layout.numColumns = 2;
43         composite.setLayout(layout);
44         GridData data = new GridData(GridData.FILL);
45         data.grabExcessHorizontalSpace = true;
46         composite.setLayoutData(data);
47
48                 Label nameLabel = new Label(composite, SWT.NONE);
49         nameLabel.setText("Name:");
50
51         Label name = new Label(composite, SWT.NONE);
52
53         Bookmark bookmark = getBookmark();
54         String description = bookmark.getName();
55         name.setText(description);
56         
57         TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
58         layout = new GridLayout();
59         tabFolder.setLayout(layout);
60         data = new GridData(GridData.FILL_BOTH);
61         data.grabExcessHorizontalSpace = true;
62         data.grabExcessVerticalSpace = true;
63         data.verticalAlignment = GridData.FILL;
64         data.horizontalSpan = 2;
65         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
66         tabFolder.setLayoutData(data);
67         
68         createUserTab(tabFolder);
69         createDriverTab(tabFolder);
70         createOptionsTab(tabFolder);
71         
72         performDefaults();
73         return composite;
74         }
75
76         private Bookmark getBookmark() {
77         Bookmark bookmark =
78             ((TreeNode) getElement()).getBookmark();
79         return bookmark;
80     }
81
82     private void createDriverTab(TabFolder tabFolder) {
83         TabItem driverTab = new TabItem(tabFolder, SWT.NONE);
84         driverTab.setText("JDBC Driver");
85         
86         Composite composite = new Composite(tabFolder, SWT.NONE);
87         GridLayout layout = new GridLayout();
88         layout.numColumns = 2;
89         composite.setLayout(layout);
90         GridData data = new GridData(GridData.FILL);
91         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
92         data.grabExcessHorizontalSpace = true;
93         composite.setLayoutData(data);
94         
95         Label label = new Label(composite, SWT.NONE);
96         label.setText("Connection URL:");
97
98         this.jdbcURL = new Text(composite, SWT.BORDER);
99         data = new GridData(GridData.FILL);
100         data.horizontalAlignment = GridData.FILL;
101         data.grabExcessHorizontalSpace = true;
102         data.grabExcessHorizontalSpace = true;
103         this.jdbcURL.setLayoutData(data);
104         
105         label = new Label(composite, SWT.NONE);
106         label.setText("Driver Name:");
107
108         this.driverName = new Text(composite, SWT.BORDER);
109         data = new GridData(GridData.FILL);
110         data.horizontalAlignment = GridData.FILL;
111         data.grabExcessHorizontalSpace = true;
112         this.driverName.setLayoutData(data);
113
114         label = new Label(composite, SWT.NONE);
115         label.setText("Driver Location:");
116
117         this.driverPath = new Text(composite, SWT.BORDER);
118         data = new GridData(GridData.FILL);
119         data.horizontalAlignment = GridData.FILL;
120         data.grabExcessHorizontalSpace = true;
121         this.driverPath.setLayoutData(data);
122
123         label = new Label(composite, SWT.NULL);
124         label.setText("Type:");
125         this.type = new Combo(composite, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
126         String adapterNames[] = new String[adapters.length];
127         for (int i = 0; i < adapters.length; i++) {
128             adapterNames[i] = adapters[i].getDisplayName();
129         }
130         this.type.setItems(adapterNames);
131         
132         data = new GridData();
133         data.horizontalAlignment = GridData.FILL;
134         this.type.setLayoutData(data);
135
136         driverTab.setControl(composite);
137     }
138
139     private void createUserTab(TabFolder tabFolder) {
140         TabItem userTab = new TabItem(tabFolder, SWT.NONE);
141         userTab.setText("User");
142         
143         Composite composite = new Composite(tabFolder, SWT.NONE);
144         GridLayout layout = new GridLayout();
145         layout.numColumns = 2;
146         composite.setLayout(layout);
147         GridData data = new GridData(GridData.FILL);
148         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
149         data.grabExcessHorizontalSpace = true;
150         composite.setLayoutData(data);
151         
152         Label useridLabel = new Label(composite, SWT.NONE);
153         useridLabel.setText("Userid:");
154
155         this.userid = new Text(composite, SWT.BORDER);
156         data = new GridData(GridData.FILL);
157         data.horizontalAlignment = GridData.FILL;
158         data.grabExcessHorizontalSpace = true;
159         data.grabExcessHorizontalSpace = true;
160         this.userid.setLayoutData(data);
161         
162         Label passworLabel = new Label(composite, SWT.NONE);
163         passworLabel.setText("Password:");
164
165         this.password = new Text(composite, SWT.BORDER);
166         this.password.setEchoChar('*');
167         data = new GridData(GridData.FILL);
168         data.horizontalAlignment = GridData.FILL;
169         data.grabExcessHorizontalSpace = true;
170         this.password.setLayoutData(data);
171
172         this.prompt = new Button(composite, SWT.CHECK);
173         this.prompt.setText("Prompt for password");
174         data = new GridData(GridData.FILL);
175         data.horizontalSpan = 2;
176         data.horizontalAlignment = GridData.FILL;
177         data.grabExcessHorizontalSpace = true;
178         this.prompt.setLayoutData(data);
179         
180         this.prompt.addSelectionListener(new SelectionAdapter() {
181             public void widgetSelected(SelectionEvent event) {
182                 password.setEditable(!((Button) event.getSource()).getSelection()); 
183             }
184         });
185         
186         userTab.setControl(composite);
187     }
188
189         /**
190           * @param tabFolder
191           */
192          private void createOptionsTab(TabFolder tabFolder) {
193                 TabItem optionsTab = new TabItem(tabFolder, SWT.NONE);
194                 optionsTab.setText("Options");
195         
196                 Composite composite = new Composite(tabFolder, SWT.NONE);
197                 GridLayout layout = new GridLayout();
198                 layout.numColumns = 2;
199                 composite.setLayout(layout);
200                 GridData data = new GridData(GridData.FILL);
201                 data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
202                 data.grabExcessHorizontalSpace = true;
203                 composite.setLayoutData(data);
204         
205                 Label label;
206                 
207                 label = new Label(composite, SWT.NULL);
208                 label.setText("On connection, Auto-Commit should be:");
209                 this.autoCommit = new Combo(composite, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
210                 String autoCommitTypes[] = new String[] {
211                         IQuantumConstants.autoCommitTrue, 
212                         IQuantumConstants.autoCommitFalse,
213                         IQuantumConstants.autoCommitSaved
214                 };
215                 this.autoCommit.setItems(autoCommitTypes);
216         
217                 data = new GridData();
218                 data.horizontalAlignment = GridData.FILL;
219                 this.autoCommit.setLayoutData(data);
220
221                 optionsTab.setControl(composite);
222                 
223          }
224
225
226
227     /**
228      * @see org.eclipse.jface.preference.PreferencePage#performApply()
229      */
230     public boolean performOk() {
231         Bookmark bookmark = getBookmark();
232         bookmark.setUsername(this.userid.getText());
233         bookmark.setPromptForPassword(this.prompt.getSelection());
234         if (this.prompt.getSelection()) {
235             bookmark.setPassword("");
236         } else {
237             bookmark.setPassword(this.password.getText());
238         }
239
240         int index = this.type.getSelectionIndex();
241         bookmark.setType(this.adapters[index].getDriverType());
242         bookmark.setConnect(this.jdbcURL.getText());
243         bookmark.setDriver(this.driverName.getText());
244         bookmark.setDriverFile(this.driverPath.getText());
245         if (this.autoCommit.getSelectionIndex() >= 0)
246                 bookmark.setAutoCommitPreference(this.autoCommit.getItem(this.autoCommit.getSelectionIndex()));
247         return super.performOk();
248     }
249     /* (non-Javadoc)
250      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
251      */
252     protected void performDefaults() {
253         super.performDefaults();
254         Bookmark bookmark = getBookmark();
255
256         this.prompt.setSelection(bookmark.getPromptForPassword());
257         this.password.setEditable(!bookmark.getPromptForPassword());
258         this.password.setText(bookmark.getPassword());
259         this.userid.setText(bookmark.getUsername());
260
261         this.type.select(0);
262         boolean done = false;
263         for (int i = 0,
264             length = (adapters == null) ? 0 : adapters.length;
265             !done && i < length;
266             i++) {
267             if (bookmark.getType() != null && 
268                 bookmark.getType().equals(adapters[i].getDriverType())) {
269                 this.type.select(i);
270                 done = true;
271             }
272         }
273         if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitTrue))
274                 this.autoCommit.select(0);
275         else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitFalse))
276                         this.autoCommit.select(1);
277                 else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitSaved))
278                         this.autoCommit.select(2);
279                         
280         this.driverName.setText(bookmark.getDriver());
281         this.jdbcURL.setText(bookmark.getConnect());
282         this.driverPath.setText(bookmark.getDriverFile());
283     }
284 }