Project

General

Profile

Download (13.1 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
	}
82
	
83
	/**
84
	 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
85
	 *
86
	 * @param pageName a {@link java.lang.String} object.
87
	 */
88
	protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
89
		super(pageName);
90
		this.setPageComplete(false);
91
		
92
		this.mode = mode;
93
		if(mode == CdmDataSourceWizard.Mode.CLONE) {
94
			setDataSource(CdmDataSource.NewInstance(dataSource));						
95
		} else {
96
			setDataSource(dataSource);	
97
		}
98
	}
99
	
100
	/* (non-Javadoc)
101
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
102
	 */
103
	/** {@inheritDoc} */
104
	public void createControl(Composite parent) {
105
		this.parent = parent;
106
		
107
		// Create top-level composite 
108
		parent.setLayout(new GridLayout());
109
		composite = new Composite(parent, SWT.NONE);
110
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5));
111
		GridLayout formLayout = new GridLayout();
112
		formLayout.numColumns = 2;
113
		composite.setLayout(formLayout);
114

    
115

    
116

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

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

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

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

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

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

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

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

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

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

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

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

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