Project

General

Profile

Download (8.18 KB) Statistics
| Branch: | Tag: | Revision:
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
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
34

    
35
/**
36
 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
37
 *
38
 * @author n.hoffmann
39
 * @created 18.05.2009
40
 * @version 1.0
41
 */
42
public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
43
	
44
	
45
	private ArrayList<DatabaseTypeEnum> databaseTypes;
46
	
47
	private Text datasourceNameText;
48
	private String dataSourceName;
49
	private Combo databaseTypeCombo;
50
	
51
	private Composite composite;
52
	private Composite editDatasourceComposite;
53

    
54
	private NomenclaturalCode nomenclaturalCode;
55

    
56
	private boolean dataBaseTypeSelected = false;
57
	private boolean dataSourceNameSet = false;
58
	
59
	private ICdmDataSource dataSource;
60

    
61
	private WizardPage nextPage;
62

    
63
	private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
64
	
65
	/**
66
	 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
67
	 *
68
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
69
	 */
70
	public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
71
		super("DataSourceWizardPage");
72
		
73
		this.dataSource = dataSource;
74
		
75
		String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
76
		
77
		setTitle(pageName);
78
	}
79

    
80
	/* (non-Javadoc)
81
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
82
	 */
83
	/** {@inheritDoc} */
84
	public void createControl(Composite parent) {
85

    
86
		setPageComplete(false);
87
		
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);
93

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

    
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();	
116
		
117
		// Create listener to display database type-specific config options
118
		databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
119
			public void widgetSelected(SelectionEvent e) {
120
				
121
				// Get database type at the selected index
122
				DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
123
				
124
				addDatabasePage(type);
125
				setDataBaseTypeSelected(true);
126
				checkPageComplete();
127
			}
128
		});
129
		
130
		// make the composite the wizard pages control
131
		setControl(composite);
132
	}
133
	
134
	private void populateComboBoxItems() {
135
		
136
		// Init DB types
137
		if (databaseTypes == null) {
138
			databaseTypes = new ArrayList<DatabaseTypeEnum>();
139
		}
140
		
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);
149
			}
150
		}
151
	}
152
	
153
	/**
154
	 * @param type
155
	 */
156
	private void addDatabasePage(DatabaseTypeEnum type) {
157
		// add credentials wizard page according to selection
158
		Wizard wizard = (Wizard) getWizard();
159
		credentialsWizardPage = null;
160
		
161
		
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);
168
		}
169
		
170
		if(wizard.getPage(credentialsWizardPage.getName()) != null){
171
			nextPage = (WizardPage) wizard.getPage(credentialsWizardPage.getName());
172
		}else{
173
			wizard.addPage(credentialsWizardPage);
174
			nextPage = credentialsWizardPage;
175
		}
176

    
177
		getContainer().updateButtons();
178
	}
179

    
180

    
181
	/* (non-Javadoc)
182
	 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
183
	 */
184
	/** {@inheritDoc} */
185
	@Override
186
	public IWizardPage getNextPage() {
187
		return nextPage;
188
	}
189

    
190
	/* (non-Javadoc)
191
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
192
	 */
193
	/** {@inheritDoc} */
194
	public void modifyText(ModifyEvent e) {
195
		String name = datasourceNameText.getText();
196
		
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");
203
		}else{
204
			setDataSourceNameSet(true);
205
			setErrorMessage(null);
206
		}
207
		dataSourceName = name;
208
		checkPageComplete();
209
	}
210

    
211
	/* (non-Javadoc)
212
	 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
213
	 */
214
	/**
215
	 * <p>checkPageComplete</p>
216
	 */
217
	public void checkPageComplete() {
218
		boolean complete = isDataBaseTypeSelected();
219
		complete &= isDataSourceNameSet();
220
		
221
		setPageComplete(complete);
222
	}
223
	
224
	/**
225
	 * <p>Getter for the field <code>dataSourceName</code>.</p>
226
	 *
227
	 * @return a {@link java.lang.String} object.
228
	 */
229
	public String getDataSourceName() {
230
		return dataSourceName;
231
	}
232

    
233
	/**
234
	 * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
235
	 *
236
	 * @return the credentialsWizardPage
237
	 */
238
	public CdmDataSourceCredentialsWizardPage getCredentialsWizardPage() {
239
		return credentialsWizardPage;
240
	}
241

    
242
	/**
243
	 * <p>isDataBaseTypeSelected</p>
244
	 *
245
	 * @return the dataBaseTypeSelected
246
	 */
247
	public boolean isDataBaseTypeSelected() {
248
		return dataBaseTypeSelected;
249
	}
250

    
251
	/**
252
	 * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
253
	 *
254
	 * @param dataBaseTypeSelected the dataBaseTypeSelected to set
255
	 */
256
	public void setDataBaseTypeSelected(boolean dataBaseTypeSelected) {
257
		this.dataBaseTypeSelected = dataBaseTypeSelected;
258
	}
259

    
260
	/**
261
	 * <p>isDataSourceNameSet</p>
262
	 *
263
	 * @return the dataSourceNameSet
264
	 */
265
	public boolean isDataSourceNameSet() {
266
		return dataSourceNameSet;
267
	}
268

    
269
	/**
270
	 * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
271
	 *
272
	 * @param dataSourceNameSet the dataSourceNameSet to set
273
	 */
274
	public void setDataSourceNameSet(boolean dataSourceNameSet) {
275
		this.dataSourceNameSet = dataSourceNameSet;
276
	}
277

    
278
	/**
279
	 * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
280
	 *
281
	 * @return the nomenclaturalCode
282
	 */
283
	public NomenclaturalCode getNomenclaturalCode() {
284
		return nomenclaturalCode;
285
	}
286
	
287
	
288
}
(5-5/6)