1 package com.quantum.wizards;
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;
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;
38 public class AddDriverWizard extends Wizard {
40 public class PageImpl extends WizardPage {
42 private FileDialog fileDialog;
44 private Text driverFileName;
45 private Text driverClassName;
46 private ComboViewer type;
47 private DatabaseAdapter[] adapters = AdapterFactory.getInstance().getDriverList();
52 public void createControl(Composite parent) {
53 setPageComplete(false);
55 setTitle(Messages.getString(getClass(), "title"));
56 setDescription(Messages.getString(getClass(), "description"));
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);
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") });
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);
83 this.driverFileName.addModifyListener(new ModifyListener() {
84 public void modifyText(ModifyEvent event) {
90 Button button = new Button(composite, SWT.PUSH);
91 button.setText(Messages.getString(getClass(), "browse"));
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);
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);
115 this.driverClassName.addModifyListener(new ModifyListener() {
116 public void modifyText(ModifyEvent event) {
117 AddDriverWizard.this.setDriverClassName(((Text) event.getSource()).getText());
122 button = new Button(composite, SWT.PUSH);
123 button.setText(Messages.getString(getClass(), "browse"));
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(
136 AddDriverWizard.this.setDriverClassName(className);
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() {
150 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
152 public Object[] getElements(Object inputElement) {
153 return PageImpl.this.adapters;
157 this.type.setLabelProvider(new ILabelProvider() {
158 public Image getImage(Object element) {
161 public String getText(Object element) {
162 if (element != null && element instanceof DatabaseAdapter) {
163 return ((DatabaseAdapter) element).getDisplayName();
168 public void addListener(ILabelProviderListener listener) {
170 public void dispose() {
172 public boolean isLabelProperty(Object element, String property) {
175 public void removeListener(ILabelProviderListener listener) {
180 fullHorizontal = new GridData();
181 fullHorizontal.horizontalAlignment = GridData.FILL;
182 type.getControl().setLayoutData(fullHorizontal);
184 type.setSelection(new StructuredSelection(
185 AdapterFactory.getInstance().getAdapter(AdapterFactory.GENERIC)));
187 setControl(composite);
189 protected void updateButtons() {
190 Class driver = JarUtil.loadDriverClass(getDriverFile(), getDriverClassName());
191 setPageComplete(driver != null);
193 String adapterType = AdapterFactory.getInstance().getAdapterType(getDriverClassName());
194 if (adapterType != null) {
195 this.type.setSelection(new StructuredSelection(
196 AdapterFactory.getInstance().getAdapter(adapterType)));
199 protected String getDriverClassName() {
200 return this.driverClassName == null ? null : this.driverClassName.getText();
202 protected String getDriverFile() {
203 return this.driverFileName == null ? null : this.driverFileName.getText();
206 protected String getDriverType() {
207 DatabaseAdapter driverInfo = (DatabaseAdapter)
208 ((IStructuredSelection) this.type.getSelection()).getFirstElement();
209 return (driverInfo == null) ? null : driverInfo.getType();
212 private PageImpl page;
214 private String driverClassName;
215 private String driverFileName;
216 private String driverType;
219 public void addPages() {
220 this.page = new PageImpl();
224 public AddDriverWizard() {
226 setWindowTitle(Messages.getString(getClass(), "windowTitle"));
229 public boolean performFinish() {
230 JDBCDriver driver = new JDBCDriver(
231 getDriverClassName(),
233 this.page.getDriverType());
234 BookmarkCollection.getInstance().addDriver(driver);
240 * @return Returns the driverClassName.
242 public String getDriverClassName() {
243 return this.driverClassName;
246 * @param driverClassName The driverClassName to set.
248 public void setDriverClassName(String driverClassName) {
249 this.driverClassName = driverClassName;
252 * @return Returns the driverType.
254 public String getDriverType() {
255 return this.driverType;
258 * @param driverType The driverType to set.
260 public void setDriverType(String driverType) {
261 this.driverType = driverType;
264 * @return Returns the driverFileName.
266 public String getDriverFileName() {
267 return this.driverFileName;
270 * @param driverFileName The driverFileName to set.
272 public void setDriverFileName(String driverFileName) {
273 this.driverFileName = driverFileName;