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