Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.local / src / main / java / eu / etaxonomy / taxeditor / local / 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 package eu.etaxonomy.taxeditor.local.datasource.wizard;
10
11 import java.util.ArrayList;
12
13 import org.eclipse.jface.wizard.IWizardPage;
14 import org.eclipse.jface.wizard.Wizard;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Text;
27
28 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
29 import eu.etaxonomy.cdm.database.ICdmDataSource;
30 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
31 import eu.etaxonomy.taxeditor.local.datasource.common.CdmDataSourceRepository;
32
33 /**
34 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
35 *
36 * @author n.hoffmann
37 * @created 18.05.2009
38 */
39 public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
40
41 public static final DatabaseTypeEnum[] supportedDatabaseTypes = new DatabaseTypeEnum[]{
42 DatabaseTypeEnum.MySQL,
43 DatabaseTypeEnum.H2,
44 DatabaseTypeEnum.PostgreSQL
45 /*DatabaseTypeEnum.SqlServer2005*/
46 };
47
48 private ArrayList<DatabaseTypeEnum> databaseTypes;
49
50 private Text datasourceNameText;
51 private String dataSourceName;
52 private Combo databaseTypeCombo;
53
54 private Composite composite;
55 private Composite editDatasourceComposite;
56
57 private NomenclaturalCode nomenclaturalCode;
58
59 private boolean dataBaseTypeSelected = false;
60 private boolean dataSourceNameSet = false;
61
62 private ICdmDataSource dataSource;
63
64 private WizardPage nextPage;
65
66 private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
67
68 /**
69 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
70 *
71 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
72 */
73 public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
74 super("DataSourceWizardPage");
75
76 this.dataSource = dataSource;
77
78 String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
79
80 setTitle(pageName);
81 }
82
83 @Override
84 public void createControl(Composite parent) {
85
86 setPageComplete(false);
87
88 // Create top-level composite
89 composite = new Composite(parent, SWT.NONE);
90 GridLayout gridLayout = new GridLayout();
91 gridLayout.numColumns = 1;
92 composite.setLayout(gridLayout);
93
94 // Create editDatasourceComposite to display a dataSource's name and type
95 editDatasourceComposite = new Composite(composite, SWT.NONE);
96 GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true);
97 editDatasourceComposite.setLayoutData(datasourceGridData);
98 GridLayout datasourceLayout = new GridLayout();
99 datasourceLayout.numColumns = 2;
100 editDatasourceComposite.setLayout(datasourceLayout);
101
102 // Create label and input for dataSource name
103 Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
104 datasourceNameLabel.setText("Datasource Name:");
105 datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
106 datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
107 datasourceNameText.addModifyListener(this);
108
109 // Create label and dropdown for database type
110 Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
111 databaseTypeLabel.setText("Database Type:");
112 databaseTypeCombo = new Combo(editDatasourceComposite, SWT.BORDER | SWT.READ_ONLY);
113 GridData comboLayout = new GridData(SWT.FILL, SWT.CENTER, false, false);
114 databaseTypeCombo.setLayoutData(comboLayout);
115 populateComboBoxItems();
116
117 // Create listener to display database type-specific config options
118 databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
119 @Override
120 public void widgetSelected(SelectionEvent e) {
121
122 // Get database type at the selected index
123 DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
124
125 addDatabasePage(type);
126 setDataBaseTypeSelected(true);
127 checkPageComplete();
128 }
129 });
130
131 // make the composite the wizard pages control
132 setControl(composite);
133 }
134
135 private void populateComboBoxItems() {
136
137 // Init DB types
138 if (databaseTypes == null) {
139 databaseTypes = new ArrayList<DatabaseTypeEnum>();
140 }
141
142 // Add types to the type drop-down and to the types collection
143 for (DatabaseTypeEnum type : supportedDatabaseTypes) {
144 databaseTypeCombo.add(type.getName());
145 databaseTypes.add(type);
146 }
147 }
148
149 private void addDatabasePage(DatabaseTypeEnum type) {
150 // add credentials wizard page according to selection
151 Wizard wizard = (Wizard) getWizard();
152 credentialsWizardPage = null;
153
154
155 if(type == DatabaseTypeEnum.H2){
156 credentialsWizardPage = new CdmDataSourceH2WizardPage(dataSource,CdmDataSourceWizard.Mode.CREATE);
157 }
158 else if(type == DatabaseTypeEnum.MySQL){
159 credentialsWizardPage = new CdmDataSourceMySQLWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
160 }
161 else if(type == DatabaseTypeEnum.PostgreSQL){
162 credentialsWizardPage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
163 }
164
165 // else if(type == DatabaseTypeEnum.SqlServer2005){
166 // credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
167 // }
168
169 if(credentialsWizardPage != null && wizard.getPage(credentialsWizardPage.getName()) != null){
170 nextPage = (WizardPage) wizard.getPage(credentialsWizardPage.getName());
171 }else{
172 wizard.addPage(credentialsWizardPage);
173 nextPage = credentialsWizardPage;
174 }
175
176 getContainer().updateButtons();
177 }
178
179 @Override
180 public IWizardPage getNextPage() {
181 return nextPage;
182 }
183
184 @Override
185 public void modifyText(ModifyEvent e) {
186 String name = datasourceNameText.getText();
187
188 if(name.length() == 0){
189 setDataSourceNameSet(false);
190 setErrorMessage("DataSource name must not be empty.");
191 }else if(CdmDataSourceRepository.getDataSource(name) != null){
192 setDataSourceNameSet(false);
193 setErrorMessage("DataSource with the same name already exists");
194 }else{
195 setDataSourceNameSet(true);
196 setErrorMessage(null);
197 }
198 dataSourceName = name;
199 checkPageComplete();
200 }
201
202 public void checkPageComplete() {
203 boolean complete = isDataBaseTypeSelected();
204 complete &= isDataSourceNameSet();
205
206 setPageComplete(complete);
207 }
208
209 public String getDataSourceName() {
210 return dataSourceName;
211 }
212
213 public CdmDataSourceCredentialsWizardPage getCredentialsWizardPage() {
214 return credentialsWizardPage;
215 }
216
217 public boolean isDataBaseTypeSelected() {
218 return dataBaseTypeSelected;
219 }
220
221 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected) {
222 this.dataBaseTypeSelected = dataBaseTypeSelected;
223 }
224
225 public boolean isDataSourceNameSet() {
226 return dataSourceNameSet;
227 }
228
229 public void setDataSourceNameSet(boolean dataSourceNameSet) {
230 this.dataSourceNameSet = dataSourceNameSet;
231 }
232
233 public NomenclaturalCode getNomenclaturalCode() {
234 return nomenclaturalCode;
235 }
236 }