2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.datasource
.wizard
;
12 import java
.util
.ArrayList
;
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
;
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
;
35 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
41 public class CdmDataSourceTypeSelectionWizardPage
extends WizardPage
implements ModifyListener
{
43 public static final DatabaseTypeEnum
[] supportedDatabaseTypes
= new DatabaseTypeEnum
[]{
44 DatabaseTypeEnum
.MySQL
,
46 DatabaseTypeEnum
.PostgreSQL
47 /*DatabaseTypeEnum.SqlServer2005*/
51 private ArrayList
<DatabaseTypeEnum
> databaseTypes
;
53 private Text datasourceNameText
;
54 private String dataSourceName
;
55 private Combo databaseTypeCombo
;
57 private Composite composite
;
58 private Composite editDatasourceComposite
;
60 private NomenclaturalCode nomenclaturalCode
;
62 private boolean dataBaseTypeSelected
= false;
63 private boolean dataSourceNameSet
= false;
65 private ICdmDataSource dataSource
;
67 private WizardPage nextPage
;
69 private CdmDataSourceCredentialsWizardPage credentialsWizardPage
;
72 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
74 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
76 public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource
) {
77 super("DataSourceWizardPage");
79 this.dataSource
= dataSource
;
81 String pageName
= dataSource
== null ?
"Create New Datasource" : "Edit Existing Datasource";
87 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
90 public void createControl(Composite parent
) {
92 setPageComplete(false);
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
);
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
);
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);
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();
123 // Create listener to display database type-specific config options
124 databaseTypeCombo
.addSelectionListener(new SelectionAdapter() {
125 public void widgetSelected(SelectionEvent e
) {
127 // Get database type at the selected index
128 DatabaseTypeEnum type
= databaseTypes
.get(databaseTypeCombo
.getSelectionIndex());
130 addDatabasePage(type
);
131 setDataBaseTypeSelected(true);
136 // make the composite the wizard pages control
137 setControl(composite
);
140 private void populateComboBoxItems() {
143 if (databaseTypes
== null) {
144 databaseTypes
= new ArrayList
<DatabaseTypeEnum
>();
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
);
157 private void addDatabasePage(DatabaseTypeEnum type
) {
158 // add credentials wizard page according to selection
159 Wizard wizard
= (Wizard
) getWizard();
160 credentialsWizardPage
= null;
163 if(type
== DatabaseTypeEnum
.H2
){
164 credentialsWizardPage
= new CdmDataSourceH2WizardPage(dataSource
,CdmDataSourceWizard
.Mode
.CREATE
);
166 else if(type
== DatabaseTypeEnum
.MySQL
){
167 credentialsWizardPage
= new CdmDataSourceMySQLWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
169 else if(type
== DatabaseTypeEnum
.PostgreSQL
){
170 credentialsWizardPage
= new CdmDataSourcePostgreSQLServerWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
173 // else if(type == DatabaseTypeEnum.SqlServer2005){
174 // credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
177 if(wizard
.getPage(credentialsWizardPage
.getName()) != null){
178 nextPage
= (WizardPage
) wizard
.getPage(credentialsWizardPage
.getName());
180 wizard
.addPage(credentialsWizardPage
);
181 nextPage
= credentialsWizardPage
;
184 getContainer().updateButtons();
189 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
193 public IWizardPage
getNextPage() {
198 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
201 public void modifyText(ModifyEvent e
) {
202 String name
= datasourceNameText
.getText();
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");
211 setDataSourceNameSet(true);
212 setErrorMessage(null);
214 dataSourceName
= name
;
219 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
222 * <p>checkPageComplete</p>
224 public void checkPageComplete() {
225 boolean complete
= isDataBaseTypeSelected();
226 complete
&= isDataSourceNameSet();
228 setPageComplete(complete
);
232 * <p>Getter for the field <code>dataSourceName</code>.</p>
234 * @return a {@link java.lang.String} object.
236 public String
getDataSourceName() {
237 return dataSourceName
;
241 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
243 * @return the credentialsWizardPage
245 public CdmDataSourceCredentialsWizardPage
getCredentialsWizardPage() {
246 return credentialsWizardPage
;
250 * <p>isDataBaseTypeSelected</p>
252 * @return the dataBaseTypeSelected
254 public boolean isDataBaseTypeSelected() {
255 return dataBaseTypeSelected
;
259 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
261 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
263 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected
) {
264 this.dataBaseTypeSelected
= dataBaseTypeSelected
;
268 * <p>isDataSourceNameSet</p>
270 * @return the dataSourceNameSet
272 public boolean isDataSourceNameSet() {
273 return dataSourceNameSet
;
277 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
279 * @param dataSourceNameSet the dataSourceNameSet to set
281 public void setDataSourceNameSet(boolean dataSourceNameSet
) {
282 this.dataSourceNameSet
= dataSourceNameSet
;
286 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
288 * @return the nomenclaturalCode
290 public NomenclaturalCode
getNomenclaturalCode() {
291 return nomenclaturalCode
;