1 package com.quantum.wizards;
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;
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;
39 public class AddDriverWizard extends Wizard {
41 public class PageImpl extends WizardPage {
43 private FileDialog fileDialog;
45 private Text driverFileName;
46 private Text driverClassName;
47 private ComboViewer type;
48 private DatabaseAdapter[] adapters = AdapterFactory.getInstance().getDriverList();
53 public void createControl(Composite parent) {
54 setPageComplete(false);
56 setTitle(Messages.getString(getClass(), "title"));
57 setDescription(Messages.getString(getClass(), "description"));
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);
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") });
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);
84 this.driverFileName.addModifyListener(new ModifyListener() {
85 public void modifyText(ModifyEvent event) {
91 Button button = new Button(composite, SWT.PUSH);
92 button.setText(Messages.getString(getClass(), "browse"));
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);
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);
116 this.driverClassName.addModifyListener(new ModifyListener() {
117 public void modifyText(ModifyEvent event) {
118 AddDriverWizard.this.setDriverClassName(((Text) event.getSource()).getText());
123 button = new Button(composite, SWT.PUSH);
124 button.setText(Messages.getString(getClass(), "browse"));
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(
137 AddDriverWizard.this.setDriverClassName(className);
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() {
151 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
153 public Object[] getElements(Object inputElement) {
154 return PageImpl.this.adapters;
158 this.type.setLabelProvider(new ILabelProvider() {
159 public Image getImage(Object element) {
162 public String getText(Object element) {
163 if (element != null && element instanceof DatabaseAdapter) {
164 return ((DatabaseAdapter) element).getDisplayName();
169 public void addListener(ILabelProviderListener listener) {
171 public void dispose() {
173 public boolean isLabelProperty(Object element, String property) {
176 public void removeListener(ILabelProviderListener listener) {
181 fullHorizontal = new GridData();
182 fullHorizontal.horizontalAlignment = GridData.FILL;
183 type.getControl().setLayoutData(fullHorizontal);
185 type.setSelection(new StructuredSelection(
186 AdapterFactory.getInstance().getAdapter(AdapterFactory.GENERIC)));
188 setControl(composite);
190 protected void updateButtons() {
191 Class driver = JarUtil.loadDriverClass(getDriverFile(), getDriverClassName());
192 setPageComplete(driver != null);
194 String adapterType = AdapterFactory.getInstance().getAdapterType(getDriverClassName());
195 if (adapterType != null) {
196 this.type.setSelection(new StructuredSelection(
197 AdapterFactory.getInstance().getAdapter(adapterType)));
200 protected String getDriverClassName() {
201 return this.driverClassName == null ? null : this.driverClassName.getText();
203 protected String getDriverFile() {
204 return this.driverFileName == null ? null : this.driverFileName.getText();
207 protected String getDriverType() {
208 DatabaseAdapter driverInfo = (DatabaseAdapter)
209 ((IStructuredSelection) this.type.getSelection()).getFirstElement();
210 return (driverInfo == null) ? null : driverInfo.getType();
213 private PageImpl page;
215 private String driverClassName;
216 private String driverFileName;
217 private String driverType;
220 public void addPages() {
221 this.page = new PageImpl();
225 public AddDriverWizard() {
227 setWindowTitle(Messages.getString(getClass(), "windowTitle"));
230 public boolean performFinish() {
231 JDBCDriver driver = new JDBCDriver(
232 getDriverClassName(),
234 this.page.getDriverType());
235 BookmarkCollection.getInstance().addDriver(driver);
241 * @return Returns the driverClassName.
243 public String getDriverClassName() {
244 return this.driverClassName;
247 * @param driverClassName The driverClassName to set.
249 public void setDriverClassName(String driverClassName) {
250 this.driverClassName = driverClassName;
253 * @return Returns the driverType.
255 public String getDriverType() {
256 return this.driverType;
259 * @param driverType The driverType to set.
261 public void setDriverType(String driverType) {
262 this.driverType = driverType;
265 * @return Returns the driverFileName.
267 public String getDriverFileName() {
268 return this.driverFileName;
271 * @param driverFileName The driverFileName to set.
273 public void setDriverFileName(String driverFileName) {
274 this.driverFileName = driverFileName;