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