Project

General

Profile

Download (11.5 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.sql.SQLException;
14

    
15
import org.eclipse.jface.dialogs.MessageDialog;
16
import org.eclipse.jface.wizard.IWizardPage;
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.Button;
26
import org.eclipse.swt.widgets.Composite;
27
import org.eclipse.swt.widgets.Group;
28
import org.eclipse.swt.widgets.Label;
29
import org.eclipse.swt.widgets.Text;
30

    
31
import eu.etaxonomy.cdm.database.CdmDataSource;
32
import eu.etaxonomy.cdm.database.ICdmDataSource;
33
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
34

    
35

    
36
/**
37
 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
38
 *
39
 * @author n.hoffmann
40
 * @created 19.05.2009
41
 * @version 1.0
42
 */
43
public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener {
44
	private ICdmDataSource dataSource;
45

    
46
	protected Text text_password;
47
	protected Text text_databaseName;
48
	protected Text text_dataSourceName;
49
	protected Text text_username;
50

    
51
	protected Group authenticationGroup;
52
	protected Group locationGroup;
53

    
54
	protected Composite composite;
55

    
56
	protected Composite parent;
57

    
58
	protected String name;
59
	protected String database;
60
	protected String username;
61
	protected String password;
62

    
63
	CdmDataSourceWizard.Mode mode;
64

    
65
	/**
66
	 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
67
	 *
68
	 * @param pageName a {@link java.lang.String} object.
69
	 */
70
	protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) {
71
		super(pageName);
72
		this.setPageComplete(false);
73
		setDataSource(dataSource);
74
		mode = CdmDataSourceWizard.Mode.CREATE;
75
	}
76

    
77
	/**
78
	 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
79
	 *
80
	 * @param pageName a {@link java.lang.String} object.
81
	 */
82
	protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
83
		super(pageName);
84
		this.setPageComplete(false);
85

    
86
		this.mode = mode;
87
		if(mode == CdmDataSourceWizard.Mode.CLONE) {
88
			setDataSource(CdmDataSource.NewInstance(dataSource));
89
		} else {
90
			setDataSource(dataSource);
91
		}
92
	}
93

    
94
	/** {@inheritDoc} */
95
	@Override
96
    public void createControl(Composite parent) {
97
		this.parent = parent;
98

    
99
		// Create top-level composite
100
		parent.setLayout(new GridLayout());
101
		composite = new Composite(parent, SWT.NONE);
102
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5));
103
		GridLayout formLayout = new GridLayout();
104
		formLayout.numColumns = 2;
105
		composite.setLayout(formLayout);
106

    
107

    
108

    
109
		// Create composite for data source name
110
		Composite editDatasourceComposite = new Composite(composite, SWT.NONE);
111
		GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true,2,1);
112
		editDatasourceComposite.setLayoutData(datasourceGridData);
113
		GridLayout datasourceLayout = new GridLayout();
114
		datasourceLayout.numColumns = 2;
115
		editDatasourceComposite.setLayout(datasourceLayout);
116

    
117
		// Create label and input for dataSource name
118
		Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
119
		datasourceNameLabel.setText("Datasource Name:");
120
		text_dataSourceName = new Text(editDatasourceComposite, SWT.BORDER);
121
		text_dataSourceName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
122

    
123
		if(getDataSource() == null) {
124
			editDatasourceComposite.setVisible(false);
125
		}
126

    
127
		// create a database specific form
128
		createDatabaseForm();
129

    
130
		// create the authentication input fields
131
		createAuthenticationForm();
132

    
133
		// Create composite for buttons
134
		Composite buttonComposite = new Composite(composite, SWT.NONE);
135
		buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
136
		GridLayout buttonLayout = new GridLayout();
137
		buttonLayout.numColumns = 1;
138
		buttonComposite.setLayout(buttonLayout);
139

    
140
		// Create test connection button
141
		Button testButton = new Button(buttonComposite, SWT.NONE);
142
		testButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
143
		testButton.setText("Test connection");
144

    
145
		// Test connection when button is pressed
146
		testButton.addSelectionListener(new SelectionAdapter() {
147
			@Override
148
			public void widgetSelected(SelectionEvent e) {
149
				testDbConfiguration();
150
			}
151
		});
152

    
153
		setControl(composite);
154

    
155
		init();
156

    
157
	}
158

    
159
	/**
160
	 * <p>createAuthenticationForm</p>
161
	 */
162
	protected void createAuthenticationForm(){
163
		// Create group composite for authentication data
164
		authenticationGroup = new Group(composite, SWT.NONE);
165
		authenticationGroup.setText("Authentication");
166
		authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 3));
167
		GridLayout authenticationLayout = new GridLayout();
168
		authenticationLayout.numColumns = 2;
169
		authenticationGroup.setLayout(authenticationLayout);
170

    
171
		// Create database name label
172
		Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
173
		databaseNameLabel.setText("Database Name:");
174

    
175
		// Create database name input
176
		text_databaseName = new Text(authenticationGroup, SWT.BORDER);
177
		text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
178

    
179
		// Create username label
180
		Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
181
		usernameLabel.setText("User Name:");
182

    
183
		// Create username input
184
		text_username = new Text(authenticationGroup, SWT.BORDER);
185
		text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
186

    
187
		// Create password label
188
		Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
189
		passwordLabel.setText("Password:");
190

    
191
		// Create password input
192
		text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD);
193
		text_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
194

    
195
	}
196

    
197
	/**
198
	 * Initialize text fields
199
	 */
200
	public void init() {
201
		removeListeners();
202
		if(getDataSource() != null){
203
			text_dataSourceName.setText(getDataSource().getName());
204
			text_databaseName.setText(getDataSource().getDatabase());
205
			text_username.setText(getDataSource().getUsername());
206
			text_password.setText(getDataSource().getPassword());
207
		}
208
		// add listeners after setting text to avoid the modify event being called
209
		// for the initial value
210
		addListeners();
211

    
212
		// in the case of cloning we use the same datasource info
213
		// except for the name
214
		if(mode == CdmDataSourceWizard.Mode.CLONE) {
215
			getDataSource().setName("");
216
			text_dataSourceName.setText("");
217
		} else {
218
			name = text_dataSourceName.getText();
219
		}
220

    
221
	}
222

    
223
	private void addListeners() {
224
		text_dataSourceName.addModifyListener(this);
225
		text_databaseName.addModifyListener(this);
226
		text_username.addModifyListener(this);
227
		text_password.addModifyListener(this);
228
	}
229

    
230
	private void removeListeners() {
231
		text_dataSourceName.removeModifyListener(this);
232
		text_databaseName.removeModifyListener(this);
233
		text_username.removeModifyListener(this);
234
		text_password.removeModifyListener(this);
235
	}
236

    
237
	public void testDbConfiguration(){
238
		testDbConfiguration(false);
239
	}
240

    
241
	/**
242
	 * Tries to open a connection to the given dataSource. Generates a message on either
243
	 * failure or success
244
	 */
245
	public void testDbConfiguration(boolean ignoreSuccess){
246
		try{
247
			updateAndCheck();
248
			updateDataSource();
249
			getDataSource().testConnection();
250
			if(!ignoreSuccess) {
251
				MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!");
252
			}
253
		} catch(SQLException e){
254
			MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
255
					+ System.getProperty("line.separator")    //we may use System.lineSeparator when migrated to Java 1.7
256
					+ System.getProperty("line.separator")
257
					+ "Please double check the connection fields");
258
		} catch (ClassNotFoundException e) {
259
			MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
260
					+ System.getProperty("line.separator")    //we may use System.lineSeparator when migrated to Java 1.7
261
					+ System.getProperty("line.separator")
262
					+ "Please double check the connection fields");
263
		}
264
	}
265

    
266
	/**
267
	 * Form implementation for the specific database
268
	 */
269
	public abstract void createDatabaseForm();
270

    
271
	/**
272
	 * <p>updateLocation</p>
273
	 */
274
	public abstract void updateLocation();
275

    
276
	/**
277
	 * <p>updateDataSource</p>
278
	 */
279
	public abstract void updateDataSource();
280

    
281
	/**
282
	 * <p>checkPageComplete</p>
283
	 */
284
	public void checkPageComplete(){
285
		boolean complete = false;
286
		if(mode == CdmDataSourceWizard.Mode.CREATE) {
287
			complete = database.length() != 0;
288
		} else {
289
			complete = name.length() != 0 && database.length() != 0;
290
		}
291
		this.setPageComplete(complete);
292
	}
293

    
294

    
295

    
296
	/**
297
	 * updates the current datasource with form values
298
	 */
299
	public void updateAuthentication(){
300
		database = text_databaseName.getText();
301
		username = text_username.getText();
302
		password = text_password.getText();
303
	}
304

    
305
	/** {@inheritDoc} */
306
	@Override
307
	public IWizardPage getNextPage() {
308
		return null;
309
	}
310

    
311
	/**
312
	 * <p>Setter for the field <code>dataSource</code>.</p>
313
	 *
314
	 * @param dataSource the dataSource to set
315
	 */
316
	public void setDataSource(ICdmDataSource dataSource) {
317
		this.dataSource = dataSource;
318
	}
319

    
320
	/**
321
	 * <p>Getter for the field <code>dataSource</code>.</p>
322
	 *
323
	 * @return the dataSource
324
	 */
325
	public ICdmDataSource getUpdatedDataSource() {
326
		updateDataSource();
327
		return dataSource;
328
	}
329

    
330
	protected ICdmDataSource getDataSource() {
331
		return dataSource;
332
	}
333

    
334
	/**
335
	 * <p>getDataSourceName</p>
336
	 *
337
	 * @return a {@link java.lang.String} object.
338
	 */
339
	public String getDataSourceName() {
340
		return name;
341
	}
342

    
343
	/** {@inheritDoc} */
344
	@Override
345
    public void modifyText(ModifyEvent e) {
346

    
347
		name = text_dataSourceName.getText();
348
		database = text_databaseName.getText();
349

    
350
		switch(mode) {
351
		case EDIT:
352
			if(name.length() == 0){
353
				name = "";
354
				setErrorMessage("DataSource name must not be empty.");
355
				this.setPageComplete(false);
356
				return;
357
			} else if ((!dataSource.getName().equals(name)) && (CdmDataSourceRepository.getDataSource(name) != null)) {
358
				name = "";
359
				setErrorMessage("DataSource with the same name already exists");
360
				this.setPageComplete(false);
361
				return;
362
			}
363
			break;
364
		case CLONE:
365
			if(name.length() == 0){
366
				name = "";
367
				setErrorMessage("DataSource name must not be empty.");
368
				this.setPageComplete(false);
369
				return;
370
			} else if (CdmDataSourceRepository.getDataSource(name) != null) {
371
				name = "";
372
				setErrorMessage("DataSource with the same name already exists");
373
				this.setPageComplete(false);
374
				return;
375
			}
376
			break;
377
		default:
378
			break;
379
		}
380

    
381
		if(database.length() == 0){
382
			setErrorMessage("Database name must not be empty.");
383
			this.setPageComplete(false);
384
		} else {
385
			updateAndCheck();
386
			setErrorMessage(null);
387
		}
388
	}
389

    
390

    
391
	private void updateAndCheck() {
392
		updateLocation();
393
		updateAuthentication();
394
		checkPageComplete();
395
	}
396
	/**
397
	 * <p>modifyTextWithoutTriggeringListeners</p>
398
	 *
399
	 * @param text a {@link org.eclipse.swt.widgets.Text} object.
400
	 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
401
	 * @param string a {@link java.lang.String} object.
402
	 */
403
	protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
404
		text.removeModifyListener(listener);
405
		text.setText(string);
406
		text.addModifyListener(listener);
407
	}
408
}
(1-1/7)