Re-implemented taxonomic tree using Common Navigator Framework.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / datasource / wizard / CdmDataSourceWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.store.datasource.wizard;
12
13 import java.util.ArrayList;
14
15 import org.eclipse.jface.wizard.Wizard;
16 import org.eclipse.jface.wizard.WizardPage;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.KeyListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Text;
28
29 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
30 import eu.etaxonomy.cdm.database.ICdmDataSource;
31
32 /**
33 * @author n.hoffmann
34 * @created 18.05.2009
35 * @version 1.0
36 */
37 public class CdmDataSourceWizardPage extends WizardPage implements KeyListener{
38
39
40 private ArrayList<DatabaseTypeEnum> databaseTypes;
41
42 private Text datasourceNameText;
43 private Combo databaseTypeCombo;
44
45 private Composite composite;
46 private Composite editDatasourceComposite;
47
48
49 private ICdmDataSource dataSource;
50
51 protected CdmDataSourceWizardPage(ICdmDataSource dataSource) {
52 super("DataSourceWizardPage");
53
54 this.dataSource = dataSource;
55
56 String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
57
58 setTitle(pageName);
59 }
60
61 /* (non-Javadoc)
62 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
63 */
64 public void createControl(Composite parent) {
65
66 // Create top-level composite
67 composite = new Composite(parent, SWT.NONE);
68 GridLayout gridLayout = new GridLayout();
69 gridLayout.numColumns = 1;
70 composite.setLayout(gridLayout);
71
72 // Create editDatasourceComposite to display a dataSource's name and type
73 editDatasourceComposite = new Composite(composite, SWT.NONE);
74 GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true);
75 editDatasourceComposite.setLayoutData(datasourceGridData);
76 GridLayout datasourceLayout = new GridLayout();
77 datasourceLayout.numColumns = 2;
78 editDatasourceComposite.setLayout(datasourceLayout);
79
80 // Create label and input for dataSource name
81 Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
82 datasourceNameLabel.setText("Datasource Name:");
83 datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
84 datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
85 datasourceNameText.addKeyListener(this);
86
87 // Create label and dropdown for database type
88 Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
89 databaseTypeLabel.setText("Database Type:");
90 databaseTypeCombo = new Combo(editDatasourceComposite, SWT.BORDER);
91 GridData comboLayout = new GridData(SWT.FILL, SWT.CENTER, false, false);
92 databaseTypeCombo.setLayoutData(comboLayout);
93 populateComboBoxItems();
94
95 // Create listener to display database type-specific config options
96 databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
97 public void widgetSelected(SelectionEvent e) {
98
99 // Get database type at the selected index
100 DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
101
102 ((CdmDataSourceWizard) getWizard()).setDatabaseType(type);
103
104
105 addDatabasePage(type);
106 }
107 });
108
109 // make the composite the wizard pages control
110 setControl(composite);
111 }
112
113 private void populateComboBoxItems() {
114
115 // Init DB types
116 if (databaseTypes == null) {
117 databaseTypes = new ArrayList<DatabaseTypeEnum>();
118 }
119
120 // Add types to the type drop-down and to the types collection
121 for (DatabaseTypeEnum type : DatabaseTypeEnum.getAllTypes()) {
122 // FIXME right now we filter supported types
123 if(type.equals(DatabaseTypeEnum.MySQL)
124 || type.equals(DatabaseTypeEnum.H2)){
125 databaseTypeCombo.add(type.getName());
126 databaseTypes.add(type);
127 }
128 }
129 }
130
131 /**
132 * @param type
133 */
134 private void addDatabasePage(DatabaseTypeEnum type) {
135 // add credentials wizard page according to selection
136 Wizard wizard = (Wizard) getWizard();
137 WizardPage wizardPage = null;
138 if(type == DatabaseTypeEnum.H2){
139 wizardPage = new CdmDataSourceH2WizardPage(dataSource);
140 }else if(type == DatabaseTypeEnum.MySQL){
141 wizardPage = new CdmDataSourceMySQLWizardPage(dataSource);
142 }
143
144 wizard.addPage(wizardPage);
145 getContainer().updateButtons();
146 }
147
148
149 /* (non-Javadoc)
150 * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
151 */
152 public void keyPressed(KeyEvent e) {}
153
154 /* (non-Javadoc)
155 * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
156 */
157 public void keyReleased(KeyEvent e) {
158 ((CdmDataSourceWizard) getWizard()).setDataSourceName(datasourceNameText.getText());
159 }
160
161 }