Project

General

Profile

Download (8.47 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.webapp.datasource.wizard;
11

    
12
import java.util.ArrayList;
13

    
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;
28

    
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.webapp.datasource.common.CdmDataSourceRepository;
33

    
34
/**
35
 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
36
 *
37
 * @author n.hoffmann
38
 * @created 18.05.2009
39
 * @version 1.0
40
 */
41
public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
42

    
43
	public static final DatabaseTypeEnum[] supportedDatabaseTypes = new DatabaseTypeEnum[]{
44
		DatabaseTypeEnum.MySQL,
45
		DatabaseTypeEnum.H2,
46
		DatabaseTypeEnum.PostgreSQL
47
		/*DatabaseTypeEnum.SqlServer2005*/
48
	};
49

    
50

    
51
	private ArrayList<DatabaseTypeEnum> databaseTypes;
52

    
53
	private Text datasourceNameText;
54
	private String dataSourceName;
55
	private Combo databaseTypeCombo;
56

    
57
	private Composite composite;
58
	private Composite editDatasourceComposite;
59

    
60
	private NomenclaturalCode nomenclaturalCode;
61

    
62
	private boolean dataBaseTypeSelected = false;
63
	private boolean dataSourceNameSet = false;
64

    
65
	private ICdmDataSource dataSource;
66

    
67
	private WizardPage nextPage;
68

    
69
	private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
70

    
71
	/**
72
	 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
73
	 *
74
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
75
	 */
76
	public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
77
		super("DataSourceWizardPage");
78

    
79
		this.dataSource = dataSource;
80

    
81
		String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
82

    
83
		setTitle(pageName);
84
	}
85

    
86
	/* (non-Javadoc)
87
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
88
	 */
89
	/** {@inheritDoc} */
90
	@Override
91
    public void createControl(Composite parent) {
92

    
93
		setPageComplete(false);
94

    
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);
100

    
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);
108

    
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);
115

    
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();
123

    
124
		// Create listener to display database type-specific config options
125
		databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
126
			@Override
127
            public void widgetSelected(SelectionEvent e) {
128

    
129
				// Get database type at the selected index
130
				DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
131

    
132
				addDatabasePage(type);
133
				setDataBaseTypeSelected(true);
134
				checkPageComplete();
135
			}
136
		});
137

    
138
		// make the composite the wizard pages control
139
		setControl(composite);
140
	}
141

    
142
	private void populateComboBoxItems() {
143

    
144
		// Init DB types
145
		if (databaseTypes == null) {
146
			databaseTypes = new ArrayList<DatabaseTypeEnum>();
147
		}
148

    
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);
153
		}
154
	}
155

    
156
	/**
157
	 * @param type
158
	 */
159
	private void addDatabasePage(DatabaseTypeEnum type) {
160
		// add credentials wizard page according to selection
161
		Wizard wizard = (Wizard) getWizard();
162
		credentialsWizardPage = null;
163

    
164

    
165
		if(type == DatabaseTypeEnum.H2){
166
			credentialsWizardPage = new CdmDataSourceH2WizardPage(dataSource,CdmDataSourceWizard.Mode.CREATE);
167
		}
168
		else if(type == DatabaseTypeEnum.MySQL){
169
			credentialsWizardPage = new CdmDataSourceMySQLWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
170
		}
171
		else if(type == DatabaseTypeEnum.PostgreSQL){
172
			credentialsWizardPage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
173
		}
174

    
175
//		else if(type == DatabaseTypeEnum.SqlServer2005){
176
//			credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
177
//		}
178

    
179
		if(credentialsWizardPage != null && wizard.getPage(credentialsWizardPage.getName()) != null){
180
			nextPage = (WizardPage) wizard.getPage(credentialsWizardPage.getName());
181
		}else{
182
			wizard.addPage(credentialsWizardPage);
183
			nextPage = credentialsWizardPage;
184
		}
185

    
186
		getContainer().updateButtons();
187
	}
188

    
189

    
190
	/* (non-Javadoc)
191
	 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
192
	 */
193
	/** {@inheritDoc} */
194
	@Override
195
	public IWizardPage getNextPage() {
196
		return nextPage;
197
	}
198

    
199
	/* (non-Javadoc)
200
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
201
	 */
202
	/** {@inheritDoc} */
203
	@Override
204
    public void modifyText(ModifyEvent e) {
205
		String name = datasourceNameText.getText();
206

    
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");
213
		}else{
214
			setDataSourceNameSet(true);
215
			setErrorMessage(null);
216
		}
217
		dataSourceName = name;
218
		checkPageComplete();
219
	}
220

    
221
	/* (non-Javadoc)
222
	 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
223
	 */
224
	/**
225
	 * <p>checkPageComplete</p>
226
	 */
227
	public void checkPageComplete() {
228
		boolean complete = isDataBaseTypeSelected();
229
		complete &= isDataSourceNameSet();
230

    
231
		setPageComplete(complete);
232
	}
233

    
234
	/**
235
	 * <p>Getter for the field <code>dataSourceName</code>.</p>
236
	 *
237
	 * @return a {@link java.lang.String} object.
238
	 */
239
	public String getDataSourceName() {
240
		return dataSourceName;
241
	}
242

    
243
	/**
244
	 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
245
	 *
246
	 * @return the credentialsWizardPage
247
	 */
248
	public CdmDataSourceCredentialsWizardPage getCredentialsWizardPage() {
249
		return credentialsWizardPage;
250
	}
251

    
252
	/**
253
	 * <p>isDataBaseTypeSelected</p>
254
	 *
255
	 * @return the dataBaseTypeSelected
256
	 */
257
	public boolean isDataBaseTypeSelected() {
258
		return dataBaseTypeSelected;
259
	}
260

    
261
	/**
262
	 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
263
	 *
264
	 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
265
	 */
266
	public void setDataBaseTypeSelected(boolean dataBaseTypeSelected) {
267
		this.dataBaseTypeSelected = dataBaseTypeSelected;
268
	}
269

    
270
	/**
271
	 * <p>isDataSourceNameSet</p>
272
	 *
273
	 * @return the dataSourceNameSet
274
	 */
275
	public boolean isDataSourceNameSet() {
276
		return dataSourceNameSet;
277
	}
278

    
279
	/**
280
	 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
281
	 *
282
	 * @param dataSourceNameSet the dataSourceNameSet to set
283
	 */
284
	public void setDataSourceNameSet(boolean dataSourceNameSet) {
285
		this.dataSourceNameSet = dataSourceNameSet;
286
	}
287

    
288
	/**
289
	 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
290
	 *
291
	 * @return the nomenclaturalCode
292
	 */
293
	public NomenclaturalCode getNomenclaturalCode() {
294
		return nomenclaturalCode;
295
	}
296

    
297

    
298
}
(6-6/7)