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