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.
9 package eu
.etaxonomy
.taxeditor
.local
.datasource
.wizard
;
11 import java
.util
.ArrayList
;
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
;
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
;
34 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
39 public class CdmDataSourceTypeSelectionWizardPage
extends WizardPage
implements ModifyListener
{
41 public static final DatabaseTypeEnum
[] supportedDatabaseTypes
= new DatabaseTypeEnum
[]{
42 DatabaseTypeEnum
.MySQL
,
44 DatabaseTypeEnum
.PostgreSQL
45 /*DatabaseTypeEnum.SqlServer2005*/
48 private ArrayList
<DatabaseTypeEnum
> databaseTypes
;
50 private Text datasourceNameText
;
51 private String dataSourceName
;
52 private Combo databaseTypeCombo
;
54 private Composite composite
;
55 private Composite editDatasourceComposite
;
57 private NomenclaturalCode nomenclaturalCode
;
59 private boolean dataBaseTypeSelected
= false;
60 private boolean dataSourceNameSet
= false;
62 private ICdmDataSource dataSource
;
64 private WizardPage nextPage
;
66 private CdmDataSourceCredentialsWizardPage credentialsWizardPage
;
69 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
71 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
73 public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource
) {
74 super("DataSourceWizardPage");
76 this.dataSource
= dataSource
;
78 String pageName
= dataSource
== null ?
"Create New Datasource" : "Edit Existing Datasource";
84 public void createControl(Composite parent
) {
86 setPageComplete(false);
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
);
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
);
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);
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();
117 // Create listener to display database type-specific config options
118 databaseTypeCombo
.addSelectionListener(new SelectionAdapter() {
120 public void widgetSelected(SelectionEvent e
) {
122 // Get database type at the selected index
123 DatabaseTypeEnum type
= databaseTypes
.get(databaseTypeCombo
.getSelectionIndex());
125 addDatabasePage(type
);
126 setDataBaseTypeSelected(true);
131 // make the composite the wizard pages control
132 setControl(composite
);
135 private void populateComboBoxItems() {
138 if (databaseTypes
== null) {
139 databaseTypes
= new ArrayList
<DatabaseTypeEnum
>();
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
);
149 private void addDatabasePage(DatabaseTypeEnum type
) {
150 // add credentials wizard page according to selection
151 Wizard wizard
= (Wizard
) getWizard();
152 credentialsWizardPage
= null;
155 if(type
== DatabaseTypeEnum
.H2
){
156 credentialsWizardPage
= new CdmDataSourceH2WizardPage(dataSource
,CdmDataSourceWizard
.Mode
.CREATE
);
158 else if(type
== DatabaseTypeEnum
.MySQL
){
159 credentialsWizardPage
= new CdmDataSourceMySQLWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
161 else if(type
== DatabaseTypeEnum
.PostgreSQL
){
162 credentialsWizardPage
= new CdmDataSourcePostgreSQLServerWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
165 // else if(type == DatabaseTypeEnum.SqlServer2005){
166 // credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
169 if(credentialsWizardPage
!= null && wizard
.getPage(credentialsWizardPage
.getName()) != null){
170 nextPage
= (WizardPage
) wizard
.getPage(credentialsWizardPage
.getName());
172 wizard
.addPage(credentialsWizardPage
);
173 nextPage
= credentialsWizardPage
;
176 getContainer().updateButtons();
180 public IWizardPage
getNextPage() {
185 public void modifyText(ModifyEvent e
) {
186 String name
= datasourceNameText
.getText();
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");
195 setDataSourceNameSet(true);
196 setErrorMessage(null);
198 dataSourceName
= name
;
202 public void checkPageComplete() {
203 boolean complete
= isDataBaseTypeSelected();
204 complete
&= isDataSourceNameSet();
206 setPageComplete(complete
);
209 public String
getDataSourceName() {
210 return dataSourceName
;
213 public CdmDataSourceCredentialsWizardPage
getCredentialsWizardPage() {
214 return credentialsWizardPage
;
217 public boolean isDataBaseTypeSelected() {
218 return dataBaseTypeSelected
;
221 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected
) {
222 this.dataBaseTypeSelected
= dataBaseTypeSelected
;
225 public boolean isDataSourceNameSet() {
226 return dataSourceNameSet
;
229 public void setDataSourceNameSet(boolean dataSourceNameSet
) {
230 this.dataSourceNameSet
= dataSourceNameSet
;
233 public NomenclaturalCode
getNomenclaturalCode() {
234 return nomenclaturalCode
;