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
				
173
	}
174

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

    
191
		// Create database name input
192
		text_databaseName = new Text(authenticationGroup, SWT.BORDER);
193
		text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
194
		
195

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

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

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

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

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

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

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

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