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