*** empty log message ***
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / BookmarkPropertyPage.java
1 package com.quantum.properties;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5
6 import com.quantum.IQuantumConstants;
7 import com.quantum.adapters.AdapterFactory;
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.BookmarkHolder;
10 import com.quantum.model.JDBCDriver;
11 import com.quantum.wizards.BookmarkWizard;
12 import com.quantum.wizards.JDBCDriverSelectionWizardPage;
13
14 import org.eclipse.jface.wizard.Wizard;
15 import org.eclipse.jface.wizard.WizardDialog;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.TabFolder;
28 import org.eclipse.swt.widgets.TabItem;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.ui.dialogs.PropertyPage;
31
32 public class BookmarkPropertyPage extends PropertyPage {
33         
34         class ChooseDriverWizard extends Wizard {
35
36                 private JDBCDriver selection;
37                 private JDBCDriverSelectionWizardPage page;
38                 private PropertyChangeListener listener = new PropertyChangeListener() {
39                         public void propertyChange(PropertyChangeEvent event) {
40                                 if ("driver".equals(event.getPropertyName())) {
41                                         ChooseDriverWizard.this.selection = (JDBCDriver) event.getNewValue();
42                                 }
43                         }
44                 };
45                 
46                 public void addPages() {
47                         this.page = new JDBCDriverSelectionWizardPage("page1");
48                         this.page.addPropertyChangeListener(this.listener);
49                         addPage(this.page);
50                 }
51                 
52                 public void dispose() {
53                         this.page.removePropertyChangeListener(this.listener);
54                         super.dispose();
55                 }
56                 public boolean performFinish() {
57                         BookmarkPropertyPage.this.driver = this.selection;
58                         BookmarkPropertyPage.this.setDriverDetails();
59                         return true;
60                 }
61                 
62         }
63         
64     
65     private Text password;
66     private Text userid;
67     private Button prompt;
68     
69     private Text jdbcURL;
70     private Text driverName;
71     private Text driverPath;
72     private Text driverClassName;
73     private Text driverVersion;
74     private Text type;
75     
76         private Combo autoCommit;
77         private JDBCDriver driver;
78
79     protected Control createContents(Composite parent) {
80
81         Composite composite = new Composite(parent, SWT.NONE);
82         GridLayout layout = new GridLayout();
83         layout.numColumns = 2;
84         composite.setLayout(layout);
85         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
86
87                 Label nameLabel = new Label(composite, SWT.NONE);
88         nameLabel.setText("Name:");
89
90         Label name = new Label(composite, SWT.NONE);
91
92         Bookmark bookmark = getBookmark();
93         String description = bookmark.getName();
94         name.setText(description);
95         name.setLayoutData(
96                         new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING 
97                                         | GridData.VERTICAL_ALIGN_BEGINNING));
98         
99         TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
100         layout = new GridLayout();
101         tabFolder.setLayout(layout);
102         GridData data = new GridData(GridData.FILL_BOTH);
103         data.horizontalSpan = 2;
104         tabFolder.setLayoutData(data);
105         
106         createConnectionTab(tabFolder);
107         createDriverTab(tabFolder);
108         createOptionsTab(tabFolder);
109         
110         performDefaults();
111         return composite;
112         }
113
114         private Bookmark getBookmark() {
115         Bookmark bookmark =
116             ((BookmarkHolder) getElement()).getBookmark();
117         return bookmark;
118     }
119
120     private void createDriverTab(TabFolder tabFolder) {
121         TabItem driverTab = new TabItem(tabFolder, SWT.NONE);
122         driverTab.setText("JDBC Driver");
123         
124         Composite composite = new Composite(tabFolder, SWT.NONE);
125         GridLayout layout = new GridLayout();
126         layout.numColumns = 2;
127         composite.setLayout(layout);
128         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
129         
130         Label label = new Label(composite, SWT.NONE);
131         label.setText("Driver Name:");
132
133         this.driverName = new Text(composite, SWT.BORDER);
134         this.driverName.setLayoutData(createFillHorizontalGridData());
135         this.driverName.setEditable(false);
136
137         label = new Label(composite, SWT.NONE);
138         label.setText("Driver Class Name:");
139
140         this.driverClassName = new Text(composite, SWT.BORDER);
141         this.driverClassName.setLayoutData(createFillHorizontalGridData());
142         this.driverClassName.setEditable(false);
143
144         label = new Label(composite, SWT.NONE);
145         label.setText("Driver Version:");
146
147         this.driverVersion = new Text(composite, SWT.BORDER);
148         this.driverVersion.setLayoutData(createFillHorizontalGridData());
149         this.driverVersion.setEditable(false);
150
151         label = new Label(composite, SWT.NONE);
152         label.setText("Driver Path:");
153
154         this.driverPath = new Text(composite, SWT.BORDER);
155         this.driverPath.setLayoutData(createFillHorizontalGridData());
156         this.driverPath.setEditable(false);
157
158         label = new Label(composite, SWT.NULL);
159         label.setText("Type:");
160         this.type = new Text(composite, SWT.BORDER);
161         this.type.setLayoutData(createFillHorizontalGridData());
162         this.type.setEditable(false);
163
164         driverTab.setControl(composite);
165         
166         Button button = new Button(composite, SWT.PUSH);
167         button.setText("Change");
168         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END);
169         data.horizontalSpan = 2;
170         button.setLayoutData(data);
171         button.addSelectionListener(new SelectionListener() {
172                         public void widgetSelected(SelectionEvent event) {
173                         WizardDialog dialog = 
174                                 new WizardDialog(getShell(), new ChooseDriverWizard());
175                         dialog.open();
176                         }
177                         public void widgetDefaultSelected(SelectionEvent event) {
178                         }
179         });
180     }
181     
182     private GridData createFillHorizontalGridData() {
183         GridData data = new GridData(GridData.FILL_HORIZONTAL);
184         data.widthHint = 200;
185         return data;
186     }
187
188     private void createConnectionTab(TabFolder tabFolder) {
189         TabItem userTab = new TabItem(tabFolder, SWT.NONE);
190         userTab.setText("Connection");
191         
192         Composite composite = new Composite(tabFolder, SWT.NONE);
193         GridLayout layout = new GridLayout();
194         layout.numColumns = 2;
195         composite.setLayout(layout);
196         GridData data = new GridData(GridData.FILL);
197         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
198         data.grabExcessHorizontalSpace = true;
199         composite.setLayoutData(data);
200         
201         Label useridLabel = new Label(composite, SWT.NONE);
202         useridLabel.setText("Userid:");
203
204         this.userid = new Text(composite, SWT.BORDER);
205         data = new GridData(GridData.FILL);
206         data.horizontalAlignment = GridData.FILL;
207         data.grabExcessHorizontalSpace = true;
208         data.grabExcessHorizontalSpace = true;
209         this.userid.setLayoutData(data);
210         
211         Label passworLabel = new Label(composite, SWT.NONE);
212         passworLabel.setText("Password:");
213
214         this.password = new Text(composite, SWT.BORDER);
215         this.password.setEchoChar('*');
216         data = new GridData(GridData.FILL);
217         data.horizontalAlignment = GridData.FILL;
218         data.grabExcessHorizontalSpace = true;
219         this.password.setLayoutData(data);
220
221         this.prompt = new Button(composite, SWT.CHECK);
222         this.prompt.setText("Prompt for password");
223         data = new GridData(GridData.FILL);
224         data.horizontalSpan = 2;
225         data.horizontalAlignment = GridData.FILL;
226         data.grabExcessHorizontalSpace = true;
227         this.prompt.setLayoutData(data);
228         
229         this.prompt.addSelectionListener(new SelectionAdapter() {
230             public void widgetSelected(SelectionEvent event) {
231                 password.setEditable(!((Button) event.getSource()).getSelection()); 
232             }
233         });
234         
235         Label label = new Label(composite, SWT.NONE);
236         label.setText("Connection URL:");
237
238         this.jdbcURL = new Text(composite, SWT.BORDER);
239         data = new GridData(GridData.FILL);
240         data.horizontalAlignment = GridData.FILL;
241         data.grabExcessHorizontalSpace = true;
242         data.grabExcessHorizontalSpace = true;
243         this.jdbcURL.setLayoutData(data);
244         
245         userTab.setControl(composite);
246     }
247
248         /**
249           * @param tabFolder
250           */
251          private void createOptionsTab(TabFolder tabFolder) {
252                 TabItem optionsTab = new TabItem(tabFolder, SWT.NONE);
253                 optionsTab.setText("Options");
254         
255                 Composite composite = new Composite(tabFolder, SWT.NONE);
256                 GridLayout layout = new GridLayout();
257                 layout.numColumns = 2;
258                 composite.setLayout(layout);
259                 GridData data = new GridData(GridData.FILL);
260                 data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
261                 data.grabExcessHorizontalSpace = true;
262                 composite.setLayoutData(data);
263         
264                 Label label;
265                 
266                 label = new Label(composite, SWT.NULL);
267                 label.setText("On connection, Auto-Commit should be:");
268                 this.autoCommit = new Combo(composite, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
269                 String autoCommitTypes[] = new String[] {
270                         IQuantumConstants.autoCommitTrue, 
271                         IQuantumConstants.autoCommitFalse,
272                         IQuantumConstants.autoCommitSaved
273                 };
274                 this.autoCommit.setItems(autoCommitTypes);
275         
276                 data = new GridData();
277                 data.horizontalAlignment = GridData.FILL;
278                 this.autoCommit.setLayoutData(data);
279
280                 optionsTab.setControl(composite);
281          }
282
283
284
285     /**
286      * @see org.eclipse.jface.preference.PreferencePage#performApply()
287      */
288     public boolean performOk() {
289         Bookmark bookmark = getBookmark();
290         bookmark.setUsername(this.userid.getText());
291         bookmark.setPromptForPassword(this.prompt.getSelection());
292         if (this.prompt.getSelection()) {
293             bookmark.setPassword("");
294         } else {
295             bookmark.setPassword(this.password.getText());
296         }
297         bookmark.setConnect(this.jdbcURL.getText());
298
299                 bookmark.setJDBCDriver(this.driver);
300         if (this.autoCommit.getSelectionIndex() >= 0)
301                 bookmark.setAutoCommitPreference(this.autoCommit.getItem(this.autoCommit.getSelectionIndex()));
302         return super.performOk();
303     }
304     /* (non-Javadoc)
305      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
306      */
307     protected void performDefaults() {
308         super.performDefaults();
309         Bookmark bookmark = getBookmark();
310
311         this.prompt.setSelection(bookmark.getPromptForPassword());
312         this.password.setEditable(!bookmark.getPromptForPassword());
313         String password = bookmark.getPassword();
314         this.password.setText(password == null ? "" : password);
315         this.userid.setText(bookmark.getUsername());
316         this.jdbcURL.setText(bookmark.getConnect());
317
318         this.driver = bookmark.getJDBCDriver();
319         
320         setDriverDetails();
321         
322         if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitTrue))
323                 this.autoCommit.select(0);
324         else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitFalse))
325                         this.autoCommit.select(1);
326                 else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitSaved))
327                         this.autoCommit.select(2);
328                         
329     }
330
331         /**
332          * 
333          */
334         private void setDriverDetails() {
335                 this.driverName.setText(this.driver.getName());
336         this.driverClassName.setText(this.driver.getClassName());
337         String path = this.driver.getJarFilePath();
338         this.driverPath.setText(path == null ? "" : path);
339         String version = this.driver.getVersion();
340         this.driverVersion.setText(version == null ? "" : version);
341         this.type.setText(AdapterFactory.getInstance().getAdapter(this.driver.getType()).getDisplayName());
342         }
343 }