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