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
{
44 public static final DatabaseTypeEnum
[] supportedDatabaseTypes
= new DatabaseTypeEnum
[]{
45 DatabaseTypeEnum
.MySQL
,
47 DatabaseTypeEnum
.PostgreSQL
48 /*DatabaseTypeEnum.SqlServer2005*/
52 private ArrayList
<DatabaseTypeEnum
> databaseTypes
;
54 private Text datasourceNameText
;
55 private String dataSourceName
;
56 private Combo databaseTypeCombo
;
58 private Composite composite
;
59 private Composite editDatasourceComposite
;
61 private NomenclaturalCode nomenclaturalCode
;
63 private boolean dataBaseTypeSelected
= false;
64 private boolean dataSourceNameSet
= false;
66 private ICdmDataSource dataSource
;
68 private WizardPage nextPage
;
70 private CdmDataSourceCredentialsWizardPage credentialsWizardPage
;
73 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
75 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
77 public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource
) {
78 super("DataSourceWizardPage");
80 this.dataSource
= dataSource
;
82 String pageName
= dataSource
== null ?
"Create New Datasource" : "Edit Existing Datasource";
88 * @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() {
126 public void widgetSelected(SelectionEvent e
) {
128 // Get database type at the selected index
129 DatabaseTypeEnum type
= databaseTypes
.get(databaseTypeCombo
.getSelectionIndex());
131 addDatabasePage(type
);
132 setDataBaseTypeSelected(true);
137 // make the composite the wizard pages control
138 setControl(composite
);
141 private void populateComboBoxItems() {
144 if (databaseTypes
== null) {
145 databaseTypes
= new ArrayList
<DatabaseTypeEnum
>();
148 // Add types to the type drop-down and to the types collection
149 for (DatabaseTypeEnum type
: supportedDatabaseTypes
) {
150 databaseTypeCombo
.add(type
.getName());
151 databaseTypes
.add(type
);
158 private void addDatabasePage(DatabaseTypeEnum type
) {
159 // add credentials wizard page according to selection
160 Wizard wizard
= (Wizard
) getWizard();
161 credentialsWizardPage
= null;
164 if(type
== DatabaseTypeEnum
.H2
){
165 credentialsWizardPage
= new CdmDataSourceH2WizardPage(dataSource
,CdmDataSourceWizard
.Mode
.CREATE
);
167 else if(type
== DatabaseTypeEnum
.MySQL
){
168 credentialsWizardPage
= new CdmDataSourceMySQLWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
170 else if(type
== DatabaseTypeEnum
.PostgreSQL
){
171 credentialsWizardPage
= new CdmDataSourcePostgreSQLServerWizardPage(dataSource
, CdmDataSourceWizard
.Mode
.CREATE
);
174 // else if(type == DatabaseTypeEnum.SqlServer2005){
175 // credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
178 if(wizard
.getPage(credentialsWizardPage
.getName()) != null){
179 nextPage
= (WizardPage
) wizard
.getPage(credentialsWizardPage
.getName());
181 wizard
.addPage(credentialsWizardPage
);
182 nextPage
= credentialsWizardPage
;
185 getContainer().updateButtons();
190 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
194 public IWizardPage
getNextPage() {
199 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
202 public void modifyText(ModifyEvent e
) {
203 String name
= datasourceNameText
.getText();
205 if(name
.length() == 0){
206 setDataSourceNameSet(false);
207 setErrorMessage("DataSource name must not be empty.");
208 }else if(CdmDataSourceRepository
.getDataSource(name
) != null){
209 setDataSourceNameSet(false);
210 setErrorMessage("DataSource with the same name already exists");
212 setDataSourceNameSet(true);
213 setErrorMessage(null);
215 dataSourceName
= name
;
220 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
223 * <p>checkPageComplete</p>
225 public void checkPageComplete() {
226 boolean complete
= isDataBaseTypeSelected();
227 complete
&= isDataSourceNameSet();
229 setPageComplete(complete
);
233 * <p>Getter for the field <code>dataSourceName</code>.</p>
235 * @return a {@link java.lang.String} object.
237 public String
getDataSourceName() {
238 return dataSourceName
;
242 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
244 * @return the credentialsWizardPage
246 public CdmDataSourceCredentialsWizardPage
getCredentialsWizardPage() {
247 return credentialsWizardPage
;
251 * <p>isDataBaseTypeSelected</p>
253 * @return the dataBaseTypeSelected
255 public boolean isDataBaseTypeSelected() {
256 return dataBaseTypeSelected
;
260 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
262 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
264 public void setDataBaseTypeSelected(boolean dataBaseTypeSelected
) {
265 this.dataBaseTypeSelected
= dataBaseTypeSelected
;
269 * <p>isDataSourceNameSet</p>
271 * @return the dataSourceNameSet
273 public boolean isDataSourceNameSet() {
274 return dataSourceNameSet
;
278 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
280 * @param dataSourceNameSet the dataSourceNameSet to set
282 public void setDataSourceNameSet(boolean dataSourceNameSet
) {
283 this.dataSourceNameSet
= dataSourceNameSet
;
287 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
289 * @return the nomenclaturalCode
291 public NomenclaturalCode
getNomenclaturalCode() {
292 return nomenclaturalCode
;