Project

General

Profile

Download (13.4 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.CdmPersistentDataSource;
33
import eu.etaxonomy.cdm.database.ICdmDataSource;
34
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
35
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
36
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
37
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
38

    
39

    
40
/**
41
 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
42
 *
43
 * @author n.hoffmann
44
 * @created 19.05.2009
45
 * @version 1.0
46
 */
47
public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener {
48
	private ICdmDataSource dataSource;
49
	
50
	protected Text text_password;
51
	protected Text text_databaseName;
52
	protected Text text_dataSourceName;
53
	protected Text text_username;
54
	
55
	protected Group authenticationGroup;
56
	protected Group locationGroup;
57
	protected Group nomenclaturalCodeGroup;
58

    
59
	protected Composite composite;
60

    
61
	protected Composite parent;
62

    
63
	protected String name;
64
	protected String database;
65
	protected String username;
66
	protected String password;
67
	
68
	protected NomenclaturalCode nomenclaturalCode;
69
	
70
	CdmDataSourceWizard.Mode mode;
71
	
72
	/**
73
	 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
74
	 *
75
	 * @param pageName a {@link java.lang.String} object.
76
	 */
77
	protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) {
78
		super(pageName);
79
		this.setPageComplete(false);
80
		setDataSource(dataSource);		
81
		mode = CdmDataSourceWizard.Mode.CREATE;
82
	}
83
	
84
	/**
85
	 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
86
	 *
87
	 * @param pageName a {@link java.lang.String} object.
88
	 */
89
	protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
90
		super(pageName);
91
		this.setPageComplete(false);
92
		
93
		this.mode = mode;
94
		if(mode == CdmDataSourceWizard.Mode.CLONE) {
95
			setDataSource(CdmDataSource.NewInstance(dataSource));						
96
		} else {
97
			setDataSource(dataSource);	
98
		}
99
	}
100
	
101
	/* (non-Javadoc)
102
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
103
	 */
104
	/** {@inheritDoc} */
105
	public void createControl(Composite parent) {
106
		this.parent = parent;
107
		
108
		// Create top-level composite 
109
		parent.setLayout(new GridLayout());
110
		composite = new Composite(parent, SWT.NONE);
111
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5));
112
		GridLayout formLayout = new GridLayout();
113
		formLayout.numColumns = 2;
114
		composite.setLayout(formLayout);
115

    
116

    
117

    
118
		// Create composite for data source name
119
		Composite editDatasourceComposite = new Composite(composite, SWT.NONE);
120
		GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true,2,1);
121
		editDatasourceComposite.setLayoutData(datasourceGridData);
122
		GridLayout datasourceLayout = new GridLayout();
123
		datasourceLayout.numColumns = 2;
124
		editDatasourceComposite.setLayout(datasourceLayout);
125

    
126
		// Create label and input for dataSource name
127
		Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
128
		datasourceNameLabel.setText("Datasource Name:");
129
		text_dataSourceName = new Text(editDatasourceComposite, SWT.BORDER);
130
		text_dataSourceName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
131

    
132
		if(getDataSource() == null) {
133
			editDatasourceComposite.setVisible(false);
134
		}
135
		
136
		// create a database specific form
137
		createDatabaseForm();
138
		
139
		// create the authentication input fields
140
		createAuthenticationForm();
141
		
142
		// create nomenclatural code combo
143
		createNomenclaturalCodeForm();
144
		
145
		// Create composite for buttons
146
		Composite buttonComposite = new Composite(composite, SWT.NONE);
147
		buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
148
		GridLayout buttonLayout = new GridLayout();
149
		buttonLayout.numColumns = 1;
150
		buttonComposite.setLayout(buttonLayout);
151
		
152
		// Create test connection button
153
		Button testButton = new Button(buttonComposite, SWT.NONE);
154
		testButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
155
		testButton.setText("Test connection");
156
		
157
		// Test connection when button is pressed
158
		testButton.addSelectionListener(new SelectionAdapter() {
159
			/* (non-Javadoc)
160
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
161
			 */
162
			@Override
163
			public void widgetSelected(SelectionEvent e) {
164
				testDbConfiguration();
165
			}
166
		});
167
		
168
		setControl(composite);
169
				
170
		init();
171
		
172
		if(mode == CdmDataSourceWizard.Mode.CLONE) {
173
			getDataSource().setName("");
174
			getDataSource().setDatabase("");
175
			text_dataSourceName.setText("");
176
			text_databaseName.setText("");
177
		}
178
	}
179

    
180
	/**
181
	 * <p>createAuthenticationForm</p>
182
	 */
183
	protected void createAuthenticationForm(){
184
		// Create group composite for authentication data
185
		authenticationGroup = new Group(composite, SWT.NONE);
186
		authenticationGroup.setText("Authentication");
187
		authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 3));
188
		GridLayout authenticationLayout = new GridLayout();
189
		authenticationLayout.numColumns = 2;
190
		authenticationGroup.setLayout(authenticationLayout);
191
		
192
		// Create database name label
193
		Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
194
		databaseNameLabel.setText("Database Name:");
195

    
196
		// Create database name input
197
		text_databaseName = new Text(authenticationGroup, SWT.BORDER);
198
		text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
199
		
200

    
201
		// Create username label
202
		Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
203
		usernameLabel.setText("User Name:");
204
		
205
		// Create username input
206
		text_username = new Text(authenticationGroup, SWT.BORDER);
207
		text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
208
		
209
		
210
		// Create password label
211
		Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
212
		passwordLabel.setText("Password:");
213

    
214
		// Create password input
215
		text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD);
216
		text_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
217
		
218
	}
219
	
220
	/**
221
	 * Initialize text fields
222
	 */
223
	public void init() {		
224
		removeListeners();
225
		if(getDataSource() != null){					
226
			text_dataSourceName.setText(getDataSource().getName());
227
			text_databaseName.setText(getDataSource().getDatabase());
228
			text_username.setText(getDataSource().getUsername());
229
			text_password.setText(getDataSource().getPassword());
230
		}
231
		// add listeners after setting text to avoid the modify event being called
232
		// for the initial value
233
		addListeners();
234
	}
235
	
236
	private void addListeners() {
237
		text_dataSourceName.addModifyListener(this);		
238
		text_databaseName.addModifyListener(this);
239
		text_username.addModifyListener(this);
240
		text_password.addModifyListener(this);
241
	}
242
	
243
	private void removeListeners() {
244
		text_dataSourceName.removeModifyListener(this);
245
		text_databaseName.removeModifyListener(this);
246
		text_username.removeModifyListener(this);
247
		text_password.removeModifyListener(this);
248
	}
249
	/**
250
	 * Create a radio button group to select a nomenclatural code from
251
	 */
252
	private void createNomenclaturalCodeForm() {
253
		nomenclaturalCodeGroup = new Group(composite , SWT.NONE);
254
		nomenclaturalCodeGroup.setLayout(new GridLayout());
255
		
256
		nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode();
257
		
258
		for (final NomenclaturalCode code : NomenclaturalCodeHelper.getSupportedCodes()) {
259
			Button button = new Button(nomenclaturalCodeGroup, SWT.RADIO);
260
			button.setText(NomenclaturalCodeHelper.getDescription(code));
261
			button.setData(code);
262
			if (nomenclaturalCode != null) {
263
				button.setSelection(nomenclaturalCode.equals(code));
264
			}
265
			button.addSelectionListener(new SelectionAdapter() {
266
				public void widgetSelected(SelectionEvent e) {
267
					nomenclaturalCode = (NomenclaturalCode) e.widget.getData();
268
					modifyText(null);
269
				}
270
			});
271
		}		
272
	}
273
	
274
	public void testDbConfiguration(){
275
		testDbConfiguration(false);
276
	}
277
	
278
	/**
279
	 * Tries to open a connection to the given dataSource. Generates a message on either
280
	 * failure or success
281
	 */
282
	public void testDbConfiguration(boolean ignoreSuccess){
283
		try{
284
			updateAndCheck();
285
			updateDataSource();
286
			getDataSource().testConnection();
287
			if(!ignoreSuccess) {
288
				MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!");
289
			}
290
		} catch(SQLException e){
291
			MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() 
292
					+ System.getProperty("line.separator")    //we may use System.lineSeparator when migrated to Java 1.7
293
					+ System.getProperty("line.separator")
294
					+ "Please double check the connection fields");
295
			throw new RuntimeException(e);
296
		} catch (ClassNotFoundException e) {
297
			MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()					
298
					+ System.getProperty("line.separator")    //we may use System.lineSeparator when migrated to Java 1.7
299
					+ System.getProperty("line.separator")
300
					+ "Please double check the connection fields");
301
			throw new RuntimeException(e);
302
		} 
303
	}
304
	
305
	/**
306
	 * Form implementation for the specific database
307
	 */
308
	public abstract void createDatabaseForm(); 
309
	
310
	/**
311
	 * <p>updateLocation</p>
312
	 */
313
	public abstract void updateLocation();
314
	
315
	/**
316
	 * <p>updateDataSource</p>
317
	 */
318
	public abstract void updateDataSource();
319
	
320
	/**
321
	 * <p>checkPageComplete</p>
322
	 */
323
	public void checkPageComplete(){
324
		boolean complete = false;
325
		if(mode == CdmDataSourceWizard.Mode.CREATE) {
326
			complete = database.length() != 0;
327
		} else {			
328
			complete = name.length() != 0 && database.length() != 0;
329
		}	
330
		this.setPageComplete(complete);
331
	}
332
	
333

    
334
	
335
	/**
336
	 * updates the current datasource with form values
337
	 */
338
	public void updateAuthentication(){				
339
		database = text_databaseName.getText();
340
		username = text_username.getText();
341
		password = text_password.getText();
342
	}
343

    
344
	/* (non-Javadoc)
345
	 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
346
	 */
347
	/** {@inheritDoc} */
348
	@Override
349
	public IWizardPage getNextPage() {
350
		return null;
351
	}
352

    
353
	/**
354
	 * <p>Setter for the field <code>dataSource</code>.</p>
355
	 *
356
	 * @param dataSource the dataSource to set
357
	 */
358
	public void setDataSource(ICdmDataSource dataSource) {		
359
		this.dataSource = dataSource;
360
	}
361

    
362
	/**
363
	 * <p>Getter for the field <code>dataSource</code>.</p>
364
	 *
365
	 * @return the dataSource
366
	 */
367
	public ICdmDataSource getUpdatedDataSource() {
368
		updateDataSource();
369
		return dataSource;
370
	}
371
	
372
	protected ICdmDataSource getDataSource() {		
373
		return dataSource;
374
	}
375
	
376
	/**
377
	 * <p>getDataSourceName</p>
378
	 *
379
	 * @return a {@link java.lang.String} object.
380
	 */
381
	public String getDataSourceName() {
382
		return name;
383
	}
384
	
385
	/* (non-Javadoc)
386
	 * @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
387
	 */
388
	/** {@inheritDoc} */
389
	public void modifyText(ModifyEvent e) {
390
		//this.setPageComplete(false);
391
		name = text_dataSourceName.getText();
392
		database = text_databaseName.getText();
393

    
394
		switch(mode) {
395
		case EDIT:
396
			if(name.length() == 0){			
397
				name = "";
398
				setErrorMessage("DataSource name must not be empty.");
399
				this.setPageComplete(false);
400
				return;
401
			} else if ((!dataSource.getName().equals(name)) && (CdmDataSourceRepository.getDataSource(name) != null)) {
402
				name = "";
403
				setErrorMessage("DataSource with the same name already exists");
404
				this.setPageComplete(false);
405
				return;
406
			}
407
			break;
408
		case CLONE:
409
			if(name.length() == 0){			
410
				name = "";
411
				setErrorMessage("DataSource name must not be empty.");
412
				this.setPageComplete(false);
413
			} else if (CdmDataSourceRepository.getDataSource(name) != null) {
414
				name = "";
415
				setErrorMessage("DataSource with the same name already exists");
416
				this.setPageComplete(false);
417
				return;
418
			}
419
			break;
420
		default:
421
			break;
422
		}
423
		
424
		if(database.length() == 0){					
425
			setErrorMessage("Database name must not be empty.");
426
			this.setPageComplete(false);
427
		} else {			
428
			updateAndCheck();
429
			setErrorMessage(null);			
430
		}			
431
	}		
432

    
433
	
434
	private void updateAndCheck() {
435
		updateLocation();
436
		updateAuthentication();
437
		checkPageComplete();
438
	}
439
	/**
440
	 * <p>modifyTextWithoutTriggeringListeners</p>
441
	 *
442
	 * @param text a {@link org.eclipse.swt.widgets.Text} object.
443
	 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
444
	 * @param string a {@link java.lang.String} object.
445
	 */
446
	protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
447
		text.removeModifyListener(listener);
448
		text.setText(string);
449
		text.addModifyListener(listener);
450
	}
451
}
(1-1/7)