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)
91 public void createControl(Composite parent
) {
93 setPageComplete(false);
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
);
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
);
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);
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();
124 // Create listener to display database type-specific config options
125 databaseTypeCombo
.addSelectionListener(new SelectionAdapter() {
127 public void widgetSelected(SelectionEvent e
) {
129 // Get database type at the selected index
130 DatabaseTypeEnum type
= databaseTypes
.get(databaseTypeCombo
.getSelectionIndex());
132 addDatabasePage(type
);
133 setDataBaseTypeSelected(true);
138 // make the composite the wizard pages control
139 setControl(composite
);
142 private void populateComboBoxItems() {
145 if (databaseTypes
== null) {
146 databaseTypes
= new ArrayList
<DatabaseTypeEnum
>();
149 // Add types to the type drop-down and to the types collection
150 for (DatabaseTypeEnum type
: supportedDatabaseTypes
) {
151 databaseTypeCombo
.add(type
.getName());
152 databaseTypes
.add(type
);
159 private void addDatabasePage(DatabaseTypeEnum type
) {
160 // add credentials wizard page according to selection
161 Wizard wizard
= (Wizard
) getWizard();
162 credentialsWizardPage
= null;
165 if(type
== DatabaseTypeEnum
.H2
){
166 credentialsWizardPage
= new CdmDataSourceH2WizardPage(dataSource
,CdmDataSourceWizard
.Mode
.CREATE
);
168 else if(type
== DatabaseTypeEnum
.MySQL
){
169 credentialsWizardPage
= new CdmDataSourceMySQLWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
171 else if(type
== DatabaseTypeEnum
.PostgreSQL
){
172 credentialsWizardPage
= new CdmDataSourcePostgreSQLServerWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
175 // else if(type == DatabaseTypeEnum.SqlServer2005){
176 // credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
179 if(credentialsWizardPage
!= null && wizard
.getPage(credentialsWizardPage
.getName()) != null){
180 nextPage
= (WizardPage
) wizard
.getPage(credentialsWizardPage
.getName());
182 wizard
.addPage(credentialsWizardPage
);
183 nextPage
= credentialsWizardPage
;
186 getContainer().updateButtons();
191 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
195 public IWizardPage
getNextPage() {
200 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
204 public void modifyText(ModifyEvent e
) {
205 String name
= datasourceNameText
.getText();
207 if(name
.length() == 0){
208 setDataSourceNameSet(false);
209 setErrorMessage("DataSource name must not be empty.");
210 }else if(CdmDataSourceRepository
.getDataSource(name
) != null){
211 setDataSourceNameSet(false);
212 setErrorMessage("DataSource with the same name already exists");
214 setDataSourceNameSet(true);
215 setErrorMessage(null);
217 dataSourceName
= name
;
222 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
225 * <p>checkPageComplete</p>
227 public void checkPageComplete() {
228 boolean complete
= isDataBaseTypeSelected();
229 complete
&= isDataSourceNameSet();
231 setPageComplete(complete
);
235 * <p>Getter for the field <code>dataSourceName</code>.</p>
237 * @return a {@link java.lang.String} object.
239 public String
getDataSourceName() {
240 return dataSourceName
;
244 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
246 * @return the credentialsWizardPage
248 public CdmDataSourceCredentialsWizardPage
getCredentialsWizardPage() {
249 return credentialsWizardPage
;
253 * <p>isDataBaseTypeSelected</p>
255 * @return the dataBaseTypeSelected
257 public boolean isDataBaseTypeSelected() {
258 return dataBaseTypeSelected
;
262 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
264 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
266 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected
) {
267 this.dataBaseTypeSelected
= dataBaseTypeSelected
;
271 * <p>isDataSourceNameSet</p>
273 * @return the dataSourceNameSet
275 public boolean isDataSourceNameSet() {
276 return dataSourceNameSet
;
280 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
282 * @param dataSourceNameSet the dataSourceNameSet to set
284 public void setDataSourceNameSet(boolean dataSourceNameSet
) {
285 this.dataSourceNameSet
= dataSourceNameSet
;
289 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
291 * @return the nomenclaturalCode
293 public NomenclaturalCode
getNomenclaturalCode() {
294 return nomenclaturalCode
;