3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.datasource
.wizard
;
13 import java
.util
.ArrayList
;
15 import org
.eclipse
.jface
.wizard
.IWizardPage
;
16 import org
.eclipse
.jface
.wizard
.Wizard
;
17 import org
.eclipse
.jface
.wizard
.WizardPage
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.events
.ModifyEvent
;
20 import org
.eclipse
.swt
.events
.ModifyListener
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Combo
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
30 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
31 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
33 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
36 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
42 public class CdmDataSourceTypeSelectionWizardPage
extends WizardPage
implements ModifyListener
{
45 private ArrayList
<DatabaseTypeEnum
> databaseTypes
;
47 private Text datasourceNameText
;
48 private String dataSourceName
;
49 private Combo databaseTypeCombo
;
51 private Composite composite
;
52 private Composite editDatasourceComposite
;
54 private NomenclaturalCode nomenclaturalCode
;
56 private boolean dataBaseTypeSelected
= false;
57 private boolean dataSourceNameSet
= false;
59 private ICdmDataSource dataSource
;
61 private WizardPage nextPage
;
63 private CdmDataSourceCredentialsWizardPage credentialsWizardPage
;
66 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
68 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
70 public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource
) {
71 super("DataSourceWizardPage");
73 this.dataSource
= dataSource
;
75 String pageName
= dataSource
== null ?
"Create New Datasource" : "Edit Existing Datasource";
81 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
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() {
119 public void widgetSelected(SelectionEvent e
) {
121 // Get database type at the selected index
122 DatabaseTypeEnum type
= databaseTypes
.get(databaseTypeCombo
.getSelectionIndex());
124 addDatabasePage(type
);
125 setDataBaseTypeSelected(true);
130 // make the composite the wizard pages control
131 setControl(composite
);
134 private void populateComboBoxItems() {
137 if (databaseTypes
== null) {
138 databaseTypes
= new ArrayList
<DatabaseTypeEnum
>();
141 // Add types to the type drop-down and to the types collection
142 for (DatabaseTypeEnum type
: DatabaseTypeEnum
.getAllTypes()) {
143 // FIXME right now we filter supported types
144 if(type
.equals(DatabaseTypeEnum
.MySQL
)
145 || type
.equals(DatabaseTypeEnum
.H2
)
146 || type
.equals(DatabaseTypeEnum
.SqlServer2005
)){
147 databaseTypeCombo
.add(type
.getName());
148 databaseTypes
.add(type
);
156 private void addDatabasePage(DatabaseTypeEnum type
) {
157 // add credentials wizard page according to selection
158 Wizard wizard
= (Wizard
) getWizard();
159 credentialsWizardPage
= null;
162 if(type
== DatabaseTypeEnum
.H2
){
163 credentialsWizardPage
= new CdmDataSourceH2WizardPage(dataSource
);
164 }else if(type
== DatabaseTypeEnum
.MySQL
){
165 credentialsWizardPage
= new CdmDataSourceMySQLWizardPage(dataSource
);
166 }else if(type
== DatabaseTypeEnum
.SqlServer2005
){
167 credentialsWizardPage
= new CdmDataSourceSQLServerWizardPage(dataSource
);
170 if(wizard
.getPage(credentialsWizardPage
.getName()) != null){
171 nextPage
= (WizardPage
) wizard
.getPage(credentialsWizardPage
.getName());
173 wizard
.addPage(credentialsWizardPage
);
174 nextPage
= credentialsWizardPage
;
177 getContainer().updateButtons();
182 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
186 public IWizardPage
getNextPage() {
191 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
194 public void modifyText(ModifyEvent e
) {
195 String name
= datasourceNameText
.getText();
197 if(name
.length() == 0){
198 setDataSourceNameSet(false);
199 setErrorMessage("DataSource name must not be empty.");
200 }else if(CdmDataSourceRepository
.getDataSource(name
) != null){
201 setDataSourceNameSet(false);
202 setErrorMessage("DataSource with the same name already exists");
204 setDataSourceNameSet(true);
205 setErrorMessage(null);
207 dataSourceName
= name
;
212 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
215 * <p>checkPageComplete</p>
217 public void checkPageComplete() {
218 boolean complete
= isDataBaseTypeSelected();
219 complete
&= isDataSourceNameSet();
221 setPageComplete(complete
);
225 * <p>Getter for the field <code>dataSourceName</code>.</p>
227 * @return a {@link java.lang.String} object.
229 public String
getDataSourceName() {
230 return dataSourceName
;
234 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
236 * @return the credentialsWizardPage
238 public CdmDataSourceCredentialsWizardPage
getCredentialsWizardPage() {
239 return credentialsWizardPage
;
243 * <p>isDataBaseTypeSelected</p>
245 * @return the dataBaseTypeSelected
247 public boolean isDataBaseTypeSelected() {
248 return dataBaseTypeSelected
;
252 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
254 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
256 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected
) {
257 this.dataBaseTypeSelected
= dataBaseTypeSelected
;
261 * <p>isDataSourceNameSet</p>
263 * @return the dataSourceNameSet
265 public boolean isDataSourceNameSet() {
266 return dataSourceNameSet
;
270 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
272 * @param dataSourceNameSet the dataSourceNameSet to set
274 public void setDataSourceNameSet(boolean dataSourceNameSet
) {
275 this.dataSourceNameSet
= dataSourceNameSet
;
279 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
281 * @return the nomenclaturalCode
283 public NomenclaturalCode
getNomenclaturalCode() {
284 return nomenclaturalCode
;