package com.quantum.wizards; import com.quantum.ImageStore; import com.quantum.Messages; import com.quantum.QuantumPlugin; import com.quantum.adapters.AdapterFactory; import com.quantum.adapters.DatabaseAdapter; import com.quantum.model.BookmarkCollection; import com.quantum.model.JDBCDriver; import com.quantum.ui.dialog.SimpleSelectionDialog; import com.quantum.util.JarUtil; import com.quantum.view.widget.ComboViewer; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; /** * @author BC Holmes */ public class AddDriverWizard extends Wizard { public class PageImpl extends WizardPage { private FileDialog fileDialog; private Text driverFileName; private Text driverClassName; private ComboViewer type; private DatabaseAdapter[] adapters = AdapterFactory.getInstance().getDriverList(); public PageImpl() { super(""); } public void createControl(Composite parent) { setPageComplete(false); setTitle(Messages.getString(getClass(), "title")); setDescription(Messages.getString(getClass(), "description")); parent.setLayout(new GridLayout()); Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); composite.setLayout(layout); layout.numColumns = 3; GridData fullHorizontal = new GridData( GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); composite.setLayoutData(fullHorizontal); this.fileDialog = new FileDialog(composite.getShell(), SWT.OPEN); this.fileDialog.setFilterExtensions(new String[] { "*.jar", "*.zip", "*.*" }); this.fileDialog.setFilterNames(new String[] { Messages.getString("BookmarkWizard.JarFiles"), Messages.getString("BookmarkWizard.ZipFiles"), Messages.getString("BookmarkWizard.AllFiles") }); Label label = new Label(composite, SWT.NULL); label.setText(Messages.getString(getClass(), "fileName")); this.driverFileName = new Text(composite, SWT.BORDER | SWT.SINGLE); fullHorizontal = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); this.driverFileName.setLayoutData(fullHorizontal); this.driverFileName.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent event) { updateButtons(); } }); Button button = new Button(composite, SWT.PUSH); button.setText(Messages.getString(getClass(), "browse")); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { PageImpl.this.fileDialog.setFilterPath(QuantumPlugin.getDefault() .getPreferenceStore().getString( "quantum.dialogs.bookmarkwizard.path")); String filename = PageImpl.this.fileDialog.open(); if (filename != null) { PageImpl.this.driverFileName.setText(filename); QuantumPlugin.getDefault().getPreferenceStore().setValue( "quantum.dialogs.bookmarkwizard.path", filename); AddDriverWizard.this.setDriverFileName(filename); updateButtons(); } } }); label = new Label(composite, SWT.NULL); label.setText(Messages.getString(getClass(), "driverClassName")); this.driverClassName = new Text(composite, SWT.BORDER | SWT.SINGLE); fullHorizontal = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); this.driverClassName.setLayoutData(fullHorizontal); this.driverClassName.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent event) { AddDriverWizard.this.setDriverClassName(((Text) event.getSource()).getText()); updateButtons(); } }); button = new Button(composite, SWT.PUSH); button.setText(Messages.getString(getClass(), "browse")); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { SimpleSelectionDialog dialog = new SimpleSelectionDialog( getShell(), "Select a Driver", JarUtil.getAllDriverNames( getDriverFile()), ImageStore.getImage(ImageStore.CLASS)); if (dialog.open() == SimpleSelectionDialog.OK) { IStructuredSelection selection = dialog.getSelection(); if (!selection.isEmpty()) { String className = (String) selection.getFirstElement(); PageImpl.this.driverClassName.setText( className); AddDriverWizard.this.setDriverClassName(className); updateButtons(); } } } }); label = new Label(composite, SWT.NULL); label.setText(Messages.getString("BookmarkWizard.TypeAst")); //$NON-NLS-1$ this.type = new ComboViewer(composite); this.type.setContentProvider(new IStructuredContentProvider() { public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } public Object[] getElements(Object inputElement) { return PageImpl.this.adapters; } }); this.type.setLabelProvider(new ILabelProvider() { public Image getImage(Object element) { return null; } public String getText(Object element) { if (element != null && element instanceof DatabaseAdapter) { return ((DatabaseAdapter) element).getDisplayName(); } else { return ""; } } public void addListener(ILabelProviderListener listener) { } public void dispose() { } public boolean isLabelProperty(Object element, String property) { return false; } public void removeListener(ILabelProviderListener listener) { } }); type.setInput(this); fullHorizontal = new GridData(); fullHorizontal.horizontalAlignment = GridData.FILL; type.getControl().setLayoutData(fullHorizontal); type.setSelection(new StructuredSelection( AdapterFactory.getInstance().getAdapter(AdapterFactory.GENERIC))); setControl(composite); } protected void updateButtons() { Class driver = JarUtil.loadDriverClass(getDriverFile(), getDriverClassName()); setPageComplete(driver != null); String adapterType = AdapterFactory.getInstance().getAdapterType(getDriverClassName()); if (adapterType != null) { this.type.setSelection(new StructuredSelection( AdapterFactory.getInstance().getAdapter(adapterType))); } } protected String getDriverClassName() { return this.driverClassName == null ? null : this.driverClassName.getText(); } protected String getDriverFile() { return this.driverFileName == null ? null : this.driverFileName.getText(); } protected String getDriverType() { DatabaseAdapter driverInfo = (DatabaseAdapter) ((IStructuredSelection) this.type.getSelection()).getFirstElement(); return (driverInfo == null) ? null : driverInfo.getType(); } } private PageImpl page; private String driverClassName; private String driverFileName; private String driverType; public void addPages() { this.page = new PageImpl(); addPage(page); } public AddDriverWizard() { super(); setWindowTitle(Messages.getString(getClass(), "windowTitle")); } public boolean performFinish() { JDBCDriver driver = new JDBCDriver( getDriverClassName(), getDriverFileName(), this.page.getDriverType()); BookmarkCollection.getInstance().addDriver(driver); return true; } /** * @return Returns the driverClassName. */ public String getDriverClassName() { return this.driverClassName; } /** * @param driverClassName The driverClassName to set. */ public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } /** * @return Returns the driverType. */ public String getDriverType() { return this.driverType; } /** * @param driverType The driverType to set. */ public void setDriverType(String driverType) { this.driverType = driverType; } /** * @return Returns the driverFileName. */ public String getDriverFileName() { return this.driverFileName; } /** * @param driverFileName The driverFileName to set. */ public void setDriverFileName(String driverFileName) { this.driverFileName = driverFileName; } }