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