misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / AddDriverWizard.java
1 package com.quantum.wizards;
2
3 import com.quantum.Messages;
4 import com.quantum.QuantumPlugin;
5 import com.quantum.adapters.AdapterFactory;
6 import com.quantum.adapters.DatabaseAdapter;
7 import com.quantum.model.BookmarkCollection;
8 import com.quantum.model.JDBCDriver;
9 import com.quantum.ui.dialog.SimpleSelectionDialog;
10 import com.quantum.util.JarUtil;
11 import com.quantum.view.widget.ComboViewer;
12
13 import org.eclipse.jface.viewers.ILabelProvider;
14 import org.eclipse.jface.viewers.ILabelProviderListener;
15 import org.eclipse.jface.viewers.IStructuredContentProvider;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.jface.wizard.Wizard;
20 import org.eclipse.jface.wizard.WizardPage;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.FileDialog;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Text;
34
35 /**
36  * @author BC Holmes
37  */
38 public class AddDriverWizard extends Wizard {
39         
40         public class PageImpl extends WizardPage {
41
42                 private FileDialog fileDialog;
43
44                 private Text driverFileName;
45                 private Text driverClassName;
46                 private ComboViewer type;
47                 private DatabaseAdapter[] adapters = AdapterFactory.getInstance().getDriverList();
48
49                 public PageImpl() {
50                         super("");
51                 }
52                 public void createControl(Composite parent) {
53                         setPageComplete(false);
54
55                         setTitle(Messages.getString(getClass(), "title"));
56                         setDescription(Messages.getString(getClass(), "description"));
57                         
58                         
59                 parent.setLayout(new GridLayout());
60                 Composite composite = new Composite(parent, SWT.NONE);
61                         GridLayout layout = new GridLayout();
62                         composite.setLayout(layout);
63                         layout.numColumns = 3;
64                         GridData fullHorizontal = new GridData(
65                                         GridData.FILL_HORIZONTAL |
66                                         GridData.VERTICAL_ALIGN_BEGINNING);
67                         composite.setLayoutData(fullHorizontal);
68                         
69         
70                         this.fileDialog = new FileDialog(composite.getShell(), SWT.OPEN);
71                         this.fileDialog.setFilterExtensions(new String[] { "*.jar", "*.zip", "*.*" });
72                         this.fileDialog.setFilterNames(new String[] {
73                                         Messages.getString("BookmarkWizard.JarFiles"),
74                                         Messages.getString("BookmarkWizard.ZipFiles"),
75                                         Messages.getString("BookmarkWizard.AllFiles") });
76         
77                         Label label = new Label(composite, SWT.NULL);
78                         label.setText(Messages.getString(getClass(), "fileName"));
79                         this.driverFileName = new Text(composite, SWT.BORDER | SWT.SINGLE);
80                         fullHorizontal = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
81                         this.driverFileName.setLayoutData(fullHorizontal);
82                         
83                         this.driverFileName.addModifyListener(new ModifyListener() {
84                                 public void modifyText(ModifyEvent event) {
85                                         updateButtons();
86                                 }
87                                 
88                         });
89         
90                         Button button = new Button(composite, SWT.PUSH);
91                         button.setText(Messages.getString(getClass(), "browse"));
92         
93                         button.addSelectionListener(new SelectionAdapter() {
94                                 public void widgetSelected(SelectionEvent event) {
95                                         PageImpl.this.fileDialog.setFilterPath(QuantumPlugin.getDefault()
96                                                         .getPreferenceStore().getString(
97                                                                         "quantum.dialogs.bookmarkwizard.path"));
98                                         String filename = PageImpl.this.fileDialog.open();
99                                         if (filename != null) {
100                                                 PageImpl.this.driverFileName.setText(filename);
101                                                 QuantumPlugin.getDefault().getPreferenceStore().setValue(
102                                                                 "quantum.dialogs.bookmarkwizard.path", filename);
103                                                 AddDriverWizard.this.setDriverFileName(filename);
104                                                 updateButtons();
105                                         }
106                                 }
107                         });
108         
109                         label = new Label(composite, SWT.NULL);
110                         label.setText(Messages.getString(getClass(), "driverClassName"));
111                         this.driverClassName = new Text(composite, SWT.BORDER | SWT.SINGLE);
112                         fullHorizontal = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
113                         this.driverClassName.setLayoutData(fullHorizontal);
114         
115                         this.driverClassName.addModifyListener(new ModifyListener() {
116                                 public void modifyText(ModifyEvent event) {
117                                         AddDriverWizard.this.setDriverClassName(((Text) event.getSource()).getText());
118                                         updateButtons();
119                                 }
120                                 
121                         });
122                         button = new Button(composite, SWT.PUSH);
123                         button.setText(Messages.getString(getClass(), "browse"));
124         
125                         button.addSelectionListener(new SelectionAdapter() {
126                                 public void widgetSelected(SelectionEvent event) {
127                                         SimpleSelectionDialog dialog = new SimpleSelectionDialog(
128                                                         getShell(), "Select a Driver", JarUtil.getAllDriverNames(
129                                                                         getDriverFile()), QuantumPlugin.getImage("class.gif"));
130                                         if (dialog.open() == SimpleSelectionDialog.OK) {
131                                                 IStructuredSelection selection = dialog.getSelection();
132                                                 if (!selection.isEmpty()) {
133                                                         String className = (String) selection.getFirstElement();
134                                                         PageImpl.this.driverClassName.setText(
135                                                                         className);
136                                                         AddDriverWizard.this.setDriverClassName(className);
137                                                         updateButtons();
138                                                 }
139                                                 
140                                                 
141                                         }
142                                 }
143                         });
144                         label = new Label(composite, SWT.NULL);
145                         label.setText(Messages.getString("BookmarkWizard.TypeAst")); //$NON-NLS-1$
146                         this.type = new ComboViewer(composite);
147                         this.type.setContentProvider(new IStructuredContentProvider() {
148                                 public void dispose() {
149                                 }
150                                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
151                                 }
152                                 public Object[] getElements(Object inputElement) {
153                                         return PageImpl.this.adapters;
154                                 }
155                         });
156                         
157                         this.type.setLabelProvider(new ILabelProvider() {
158                                 public Image getImage(Object element) {
159                                         return null;
160                                 }
161                                 public String getText(Object element) {
162                                         if (element != null && element instanceof DatabaseAdapter) {
163                                                 return ((DatabaseAdapter) element).getDisplayName();
164                                         } else {
165                                                 return "";
166                                         }
167                                 }
168                                 public void addListener(ILabelProviderListener listener) {
169                                 }
170                                 public void dispose() {
171                                 }
172                                 public boolean isLabelProperty(Object element, String property) {
173                                         return false;
174                                 }
175                                 public void removeListener(ILabelProviderListener listener) {
176                                 }
177                         });
178                         
179                         type.setInput(this);
180                         fullHorizontal = new GridData();
181                         fullHorizontal.horizontalAlignment = GridData.FILL;
182                         type.getControl().setLayoutData(fullHorizontal);
183                         
184                         type.setSelection(new StructuredSelection(
185                                         AdapterFactory.getInstance().getAdapter(AdapterFactory.GENERIC)));
186                         
187                         setControl(composite);
188             }
189             protected void updateButtons() {
190                 Class driver = JarUtil.loadDriverClass(getDriverFile(), getDriverClassName());
191                 setPageComplete(driver != null);
192                 
193                 String adapterType = AdapterFactory.getInstance().getAdapterType(getDriverClassName());
194                 if (adapterType != null) {
195                         this.type.setSelection(new StructuredSelection(
196                                         AdapterFactory.getInstance().getAdapter(adapterType)));
197                 }
198             }
199                 protected String getDriverClassName() {
200                 return this.driverClassName == null ? null : this.driverClassName.getText();
201                 }
202                 protected String getDriverFile() {
203                 return this.driverFileName == null ? null : this.driverFileName.getText();
204             }
205                 
206                 protected String getDriverType() {
207                         DatabaseAdapter driverInfo = (DatabaseAdapter) 
208                                         ((IStructuredSelection) this.type.getSelection()).getFirstElement();
209                         return (driverInfo == null) ? null : driverInfo.getType();
210                 }
211         }
212         private PageImpl page;
213         
214         private String driverClassName;
215         private String driverFileName;
216         private String driverType;
217
218         
219         public void addPages() {
220                 this.page = new PageImpl();
221                 addPage(page);
222         }
223         
224         public AddDriverWizard() {
225                 super();
226                 setWindowTitle(Messages.getString(getClass(), "windowTitle"));
227         }
228         
229         public boolean performFinish() {
230                 JDBCDriver driver = new JDBCDriver(
231                                 getDriverClassName(), 
232                                 getDriverFileName(), 
233                                 this.page.getDriverType());
234                 BookmarkCollection.getInstance().addDriver(driver);
235                 return true;
236         }
237         
238         
239         /**
240          * @return Returns the driverClassName.
241          */
242         public String getDriverClassName() {
243                 return this.driverClassName;
244         }
245         /**
246          * @param driverClassName The driverClassName to set.
247          */
248         public void setDriverClassName(String driverClassName) {
249                 this.driverClassName = driverClassName;
250         }
251         /**
252          * @return Returns the driverType.
253          */
254         public String getDriverType() {
255                 return this.driverType;
256         }
257         /**
258          * @param driverType The driverType to set.
259          */
260         public void setDriverType(String driverType) {
261                 this.driverType = driverType;
262         }
263         /**
264          * @return Returns the driverFileName.
265          */
266         public String getDriverFileName() {
267                 return this.driverFileName;
268         }
269         /**
270          * @param driverFileName The driverFileName to set.
271          */
272         public void setDriverFileName(String driverFileName) {
273                 this.driverFileName = driverFileName;
274         }
275 }