added berlin model export wizard. added a wizard to create taxonomic trees. refactore...
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / datasource / wizard / CdmDataSourceTypeSelectionWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.datasource.wizard;
12
13 import java.util.ArrayList;
14
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;
29
30 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
31 import eu.etaxonomy.cdm.database.ICdmDataSource;
32
33 /**
34 * @author n.hoffmann
35 * @created 18.05.2009
36 * @version 1.0
37 */
38 public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
39
40
41 private ArrayList<DatabaseTypeEnum> databaseTypes;
42
43 private Text datasourceNameText;
44 private String dataSourceName;
45 private Combo databaseTypeCombo;
46
47 private Composite composite;
48 private Composite editDatasourceComposite;
49
50
51 private ICdmDataSource dataSource;
52
53 private WizardPage nextPage;
54
55 private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
56
57 protected CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
58 super("DataSourceWizardPage");
59
60 this.dataSource = dataSource;
61
62 String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
63
64 setTitle(pageName);
65 }
66
67 /* (non-Javadoc)
68 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
69 */
70 public void createControl(Composite parent) {
71
72 // Create top-level composite
73 composite = new Composite(parent, SWT.NONE);
74 GridLayout gridLayout = new GridLayout();
75 gridLayout.numColumns = 1;
76 composite.setLayout(gridLayout);
77
78 // Create editDatasourceComposite to display a dataSource's name and type
79 editDatasourceComposite = new Composite(composite, SWT.NONE);
80 GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true);
81 editDatasourceComposite.setLayoutData(datasourceGridData);
82 GridLayout datasourceLayout = new GridLayout();
83 datasourceLayout.numColumns = 2;
84 editDatasourceComposite.setLayout(datasourceLayout);
85
86 // Create label and input for dataSource name
87 Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
88 datasourceNameLabel.setText("Datasource Name:");
89 datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
90 datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
91 datasourceNameText.addModifyListener(this);
92
93 // Create label and dropdown for database type
94 Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
95 databaseTypeLabel.setText("Database Type:");
96 databaseTypeCombo = new Combo(editDatasourceComposite, SWT.BORDER);
97 GridData comboLayout = new GridData(SWT.FILL, SWT.CENTER, false, false);
98 databaseTypeCombo.setLayoutData(comboLayout);
99 populateComboBoxItems();
100
101 // Create listener to display database type-specific config options
102 databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
103 public void widgetSelected(SelectionEvent e) {
104
105 // Get database type at the selected index
106 DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
107
108 addDatabasePage(type);
109 }
110 });
111
112 // make the composite the wizard pages control
113 setControl(composite);
114 }
115
116 private void populateComboBoxItems() {
117
118 // Init DB types
119 if (databaseTypes == null) {
120 databaseTypes = new ArrayList<DatabaseTypeEnum>();
121 }
122
123 // Add types to the type drop-down and to the types collection
124 for (DatabaseTypeEnum type : DatabaseTypeEnum.getAllTypes()) {
125 // FIXME right now we filter supported types
126 if(type.equals(DatabaseTypeEnum.MySQL)
127 || type.equals(DatabaseTypeEnum.H2)
128 || type.equals(DatabaseTypeEnum.SqlServer2005)){
129 databaseTypeCombo.add(type.getName());
130 databaseTypes.add(type);
131 }
132 }
133 }
134
135 /**
136 * @param type
137 */
138 private void addDatabasePage(DatabaseTypeEnum type) {
139 // add credentials wizard page according to selection
140 Wizard wizard = (Wizard) getWizard();
141 credentialsWizardPage = null;
142
143 if(type == DatabaseTypeEnum.H2){
144 credentialsWizardPage = new CdmDataSourceH2WizardPage(dataSource);
145 }else if(type == DatabaseTypeEnum.MySQL){
146 credentialsWizardPage = new CdmDataSourceMySQLWizardPage(dataSource);
147 }else if(type == DatabaseTypeEnum.SqlServer2005){
148 credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
149 }
150
151 if(wizard.getPage(credentialsWizardPage.getName()) != null){
152 nextPage = (WizardPage) wizard.getPage(credentialsWizardPage.getName());
153 }else{
154 wizard.addPage(credentialsWizardPage);
155 nextPage = credentialsWizardPage;
156 }
157
158 getContainer().updateButtons();
159 }
160
161
162 /* (non-Javadoc)
163 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
164 */
165 @Override
166 public IWizardPage getNextPage() {
167 return nextPage;
168 }
169
170 /* (non-Javadoc)
171 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
172 */
173 public void modifyText(ModifyEvent e) {
174 dataSourceName = datasourceNameText.getText();
175 }
176
177 /**
178 * @return
179 */
180 public String getDataSourceName() {
181 return dataSourceName;
182 }
183
184 /**
185 * @return the credentialsWizardPage
186 */
187 public CdmDataSourceCredentialsWizardPage getCredentialsWizardPage() {
188 return credentialsWizardPage;
189 }
190
191
192 }