added new constructor with mode argument for wizard pages and deprecated the old...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / datasource / wizard / CdmDataSourceTypeSelectionWizardPage.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.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
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 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
34
35 /**
36 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
37 *
38 * @author n.hoffmann
39 * @created 18.05.2009
40 * @version 1.0
41 */
42 public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
43
44 public static final DatabaseTypeEnum[] supportedDatabaseTypes = new DatabaseTypeEnum[]{
45 DatabaseTypeEnum.MySQL,
46 DatabaseTypeEnum.H2,
47 DatabaseTypeEnum.PostgreSQL
48 /*DatabaseTypeEnum.SqlServer2005*/
49 };
50
51
52 private ArrayList<DatabaseTypeEnum> databaseTypes;
53
54 private Text datasourceNameText;
55 private String dataSourceName;
56 private Combo databaseTypeCombo;
57
58 private Composite composite;
59 private Composite editDatasourceComposite;
60
61 private NomenclaturalCode nomenclaturalCode;
62
63 private boolean dataBaseTypeSelected = false;
64 private boolean dataSourceNameSet = false;
65
66 private ICdmDataSource dataSource;
67
68 private WizardPage nextPage;
69
70 private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
71
72 /**
73 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
74 *
75 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
76 */
77 public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
78 super("DataSourceWizardPage");
79
80 this.dataSource = dataSource;
81
82 String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
83
84 setTitle(pageName);
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
89 */
90 /** {@inheritDoc} */
91 public void createControl(Composite parent) {
92
93 setPageComplete(false);
94
95 // Create top-level composite
96 composite = new Composite(parent, SWT.NONE);
97 GridLayout gridLayout = new GridLayout();
98 gridLayout.numColumns = 1;
99 composite.setLayout(gridLayout);
100
101 // Create editDatasourceComposite to display a dataSource's name and type
102 editDatasourceComposite = new Composite(composite, SWT.NONE);
103 GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true);
104 editDatasourceComposite.setLayoutData(datasourceGridData);
105 GridLayout datasourceLayout = new GridLayout();
106 datasourceLayout.numColumns = 2;
107 editDatasourceComposite.setLayout(datasourceLayout);
108
109 // Create label and input for dataSource name
110 Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
111 datasourceNameLabel.setText("Datasource Name:");
112 datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
113 datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
114 datasourceNameText.addModifyListener(this);
115
116 // Create label and dropdown for database type
117 Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
118 databaseTypeLabel.setText("Database Type:");
119 databaseTypeCombo = new Combo(editDatasourceComposite, SWT.BORDER | SWT.READ_ONLY);
120 GridData comboLayout = new GridData(SWT.FILL, SWT.CENTER, false, false);
121 databaseTypeCombo.setLayoutData(comboLayout);
122 populateComboBoxItems();
123
124 // Create listener to display database type-specific config options
125 databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
126 public void widgetSelected(SelectionEvent e) {
127
128 // Get database type at the selected index
129 DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
130
131 addDatabasePage(type);
132 setDataBaseTypeSelected(true);
133 checkPageComplete();
134 }
135 });
136
137 // make the composite the wizard pages control
138 setControl(composite);
139 }
140
141 private void populateComboBoxItems() {
142
143 // Init DB types
144 if (databaseTypes == null) {
145 databaseTypes = new ArrayList<DatabaseTypeEnum>();
146 }
147
148 // Add types to the type drop-down and to the types collection
149 for (DatabaseTypeEnum type : supportedDatabaseTypes) {
150 databaseTypeCombo.add(type.getName());
151 databaseTypes.add(type);
152 }
153 }
154
155 /**
156 * @param type
157 */
158 private void addDatabasePage(DatabaseTypeEnum type) {
159 // add credentials wizard page according to selection
160 Wizard wizard = (Wizard) getWizard();
161 credentialsWizardPage = null;
162
163
164 if(type == DatabaseTypeEnum.H2){
165 credentialsWizardPage = new CdmDataSourceH2WizardPage(dataSource,CdmDataSourceWizard.Mode.CREATE);
166 }
167 else if(type == DatabaseTypeEnum.MySQL){
168 credentialsWizardPage = new CdmDataSourceMySQLWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
169 }
170 else if(type == DatabaseTypeEnum.PostgreSQL){
171 credentialsWizardPage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
172 }
173
174 // else if(type == DatabaseTypeEnum.SqlServer2005){
175 // credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
176 // }
177
178 if(wizard.getPage(credentialsWizardPage.getName()) != null){
179 nextPage = (WizardPage) wizard.getPage(credentialsWizardPage.getName());
180 }else{
181 wizard.addPage(credentialsWizardPage);
182 nextPage = credentialsWizardPage;
183 }
184
185 getContainer().updateButtons();
186 }
187
188
189 /* (non-Javadoc)
190 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
191 */
192 /** {@inheritDoc} */
193 @Override
194 public IWizardPage getNextPage() {
195 return nextPage;
196 }
197
198 /* (non-Javadoc)
199 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
200 */
201 /** {@inheritDoc} */
202 public void modifyText(ModifyEvent e) {
203 String name = datasourceNameText.getText();
204
205 if(name.length() == 0){
206 setDataSourceNameSet(false);
207 setErrorMessage("DataSource name must not be empty.");
208 }else if(CdmDataSourceRepository.getDataSource(name) != null){
209 setDataSourceNameSet(false);
210 setErrorMessage("DataSource with the same name already exists");
211 }else{
212 setDataSourceNameSet(true);
213 setErrorMessage(null);
214 }
215 dataSourceName = name;
216 checkPageComplete();
217 }
218
219 /* (non-Javadoc)
220 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
221 */
222 /**
223 * <p>checkPageComplete</p>
224 */
225 public void checkPageComplete() {
226 boolean complete = isDataBaseTypeSelected();
227 complete &= isDataSourceNameSet();
228
229 setPageComplete(complete);
230 }
231
232 /**
233 * <p>Getter for the field <code>dataSourceName</code>.</p>
234 *
235 * @return a {@link java.lang.String} object.
236 */
237 public String getDataSourceName() {
238 return dataSourceName;
239 }
240
241 /**
242 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
243 *
244 * @return the credentialsWizardPage
245 */
246 public CdmDataSourceCredentialsWizardPage getCredentialsWizardPage() {
247 return credentialsWizardPage;
248 }
249
250 /**
251 * <p>isDataBaseTypeSelected</p>
252 *
253 * @return the dataBaseTypeSelected
254 */
255 public boolean isDataBaseTypeSelected() {
256 return dataBaseTypeSelected;
257 }
258
259 /**
260 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
261 *
262 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
263 */
264 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected) {
265 this.dataBaseTypeSelected = dataBaseTypeSelected;
266 }
267
268 /**
269 * <p>isDataSourceNameSet</p>
270 *
271 * @return the dataSourceNameSet
272 */
273 public boolean isDataSourceNameSet() {
274 return dataSourceNameSet;
275 }
276
277 /**
278 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
279 *
280 * @param dataSourceNameSet the dataSourceNameSet to set
281 */
282 public void setDataSourceNameSet(boolean dataSourceNameSet) {
283 this.dataSourceNameSet = dataSourceNameSet;
284 }
285
286 /**
287 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
288 *
289 * @return the nomenclaturalCode
290 */
291 public NomenclaturalCode getNomenclaturalCode() {
292 return nomenclaturalCode;
293 }
294
295
296 }