1 package com.quantum.wizards;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Iterator;
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;
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;
50 public class AddDriverWizard extends Wizard {
52 public class LabelProviderImpl extends LabelProvider {
54 public Image getImage(Object element) {
55 return ImageStore.getImage(ImageStore.EXTERNAL_JAR);
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;
69 public void dispose() {
71 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
75 public class PageImpl extends WizardPage {
77 private FileDialog fileDialog;
79 private Text driverClassName;
80 private ComboViewer type;
81 private TableViewer driverFiles;
82 private DatabaseAdapter[] adapters = AdapterFactory.getInstance().getDriverList();
87 public void createControl(Composite parent) {
88 setPageComplete(false);
90 setTitle(Messages.getString(getClass(), "title"));
91 setDescription(Messages.getString(getClass(), "description"));
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));
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") });
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);
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);
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));
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));
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);
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);
158 this.driverFiles.addSelectionChangedListener(new ISelectionChangedListener() {
159 public void selectionChanged(SelectionChangedEvent event) {
160 removeJar.setEnabled(!event.getSelection().isEmpty());
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());
171 PageImpl.this.driverFiles.refresh();
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);
181 this.driverClassName.addModifyListener(new ModifyListener() {
182 public void modifyText(ModifyEvent event) {
183 AddDriverWizard.this.setDriverClassName(((Text) event.getSource()).getText());
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));
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(
204 AddDriverWizard.this.setDriverClassName(className);
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());
215 this.type.setLabelProvider(new ILabelProvider() {
216 public Image getImage(Object element) {
219 public String getText(Object element) {
220 if (element != null && element instanceof DatabaseAdapter) {
221 return ((DatabaseAdapter) element).getDisplayName();
226 public void addListener(ILabelProviderListener listener) {
228 public void dispose() {
230 public boolean isLabelProperty(Object element, String property) {
233 public void removeListener(ILabelProviderListener listener) {
237 type.setInput(this.adapters);
238 fullHorizontal = new GridData();
239 fullHorizontal.horizontalAlignment = GridData.FILL;
240 type.getControl().setLayoutData(fullHorizontal);
242 type.setSelection(new StructuredSelection(
243 AdapterFactory.getInstance().getAdapter(AdapterFactory.GENERIC)));
245 setControl(composite);
247 protected void updateButtons() {
248 Class driver = JarUtil.loadDriverClass(
249 getDriverFileNames(),
250 getDriverClassName());
251 setPageComplete(driver != null);
253 String adapterType = AdapterFactory.getInstance().getAdapterType(getDriverClassName());
254 if (adapterType != null) {
255 this.type.setSelection(new StructuredSelection(
256 AdapterFactory.getInstance().getAdapter(adapterType)));
259 protected String getDriverType() {
260 DatabaseAdapter driverInfo = (DatabaseAdapter)
261 ((IStructuredSelection) this.type.getSelection()).getFirstElement();
262 return (driverInfo == null) ? null : driverInfo.getType();
265 private PageImpl page;
267 private String driverClassName;
268 private String driverType;
269 private List driverFileNames = Collections.synchronizedList(new ArrayList());
272 public void addPages() {
273 this.page = new PageImpl();
277 public AddDriverWizard() {
279 setWindowTitle(Messages.getString(getClass(), "windowTitle"));
282 public boolean performFinish() {
283 JDBCDriver driver = new JDBCDriver(
284 getDriverClassName(),
285 getDriverFileNames(),
286 this.page.getDriverType());
287 BookmarkCollection.getInstance().addDriver(driver);
293 * @return Returns the driverClassName.
295 public String getDriverClassName() {
296 return this.driverClassName;
299 * @param driverClassName The driverClassName to set.
301 public void setDriverClassName(String driverClassName) {
302 this.driverClassName = driverClassName;
305 * @return Returns the driverType.
307 public String getDriverType() {
308 return this.driverType;
311 * @param driverType The driverType to set.
313 public void setDriverType(String driverType) {
314 this.driverType = driverType;
317 public String[] getDriverFileNames() {
318 return (String[]) this.driverFileNames.toArray(new String[this.driverFileNames.size()]);