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
|
* <p>createAuthenticationForm</p>
|
176
|
*/
|
177
|
protected void createAuthenticationForm(){
|
178
|
// Create group composite for authentication data
|
179
|
authenticationGroup = new Group(composite, SWT.NONE);
|
180
|
authenticationGroup.setText("Authentication");
|
181
|
authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 3));
|
182
|
GridLayout authenticationLayout = new GridLayout();
|
183
|
authenticationLayout.numColumns = 2;
|
184
|
authenticationGroup.setLayout(authenticationLayout);
|
185
|
|
186
|
// Create database name label
|
187
|
Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
|
188
|
databaseNameLabel.setText("Database Name:");
|
189
|
|
190
|
// Create database name input
|
191
|
text_databaseName = new Text(authenticationGroup, SWT.BORDER);
|
192
|
text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
193
|
|
194
|
|
195
|
// Create username label
|
196
|
Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
|
197
|
usernameLabel.setText("User Name:");
|
198
|
|
199
|
// Create username input
|
200
|
text_username = new Text(authenticationGroup, SWT.BORDER);
|
201
|
text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
202
|
|
203
|
|
204
|
// Create password label
|
205
|
Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
|
206
|
passwordLabel.setText("Password:");
|
207
|
|
208
|
// Create password input
|
209
|
text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD);
|
210
|
text_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
211
|
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Initialize text fields
|
216
|
*/
|
217
|
public void init() {
|
218
|
removeListeners();
|
219
|
if(getDataSource() != null){
|
220
|
text_dataSourceName.setText(getDataSource().getName());
|
221
|
text_databaseName.setText(getDataSource().getDatabase());
|
222
|
text_username.setText(getDataSource().getUsername());
|
223
|
text_password.setText(getDataSource().getPassword());
|
224
|
}
|
225
|
// add listeners after setting text to avoid the modify event being called
|
226
|
// for the initial value
|
227
|
addListeners();
|
228
|
|
229
|
// in the case of cloning we use the same datasource info
|
230
|
// except for the name
|
231
|
if(mode == CdmDataSourceWizard.Mode.CLONE) {
|
232
|
getDataSource().setName("");
|
233
|
text_dataSourceName.setText("");
|
234
|
} else {
|
235
|
name = text_dataSourceName.getText();
|
236
|
}
|
237
|
|
238
|
}
|
239
|
|
240
|
private void addListeners() {
|
241
|
text_dataSourceName.addModifyListener(this);
|
242
|
text_databaseName.addModifyListener(this);
|
243
|
text_username.addModifyListener(this);
|
244
|
text_password.addModifyListener(this);
|
245
|
}
|
246
|
|
247
|
private void removeListeners() {
|
248
|
text_dataSourceName.removeModifyListener(this);
|
249
|
text_databaseName.removeModifyListener(this);
|
250
|
text_username.removeModifyListener(this);
|
251
|
text_password.removeModifyListener(this);
|
252
|
}
|
253
|
/**
|
254
|
* Create a radio button group to select a nomenclatural code from
|
255
|
*/
|
256
|
private void createNomenclaturalCodeForm() {
|
257
|
nomenclaturalCodeGroup = new Group(composite , SWT.NONE);
|
258
|
nomenclaturalCodeGroup.setLayout(new GridLayout());
|
259
|
|
260
|
nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode();
|
261
|
|
262
|
for (final NomenclaturalCode code : NomenclaturalCodeHelper.getSupportedCodes()) {
|
263
|
Button button = new Button(nomenclaturalCodeGroup, SWT.RADIO);
|
264
|
button.setText(NomenclaturalCodeHelper.getDescription(code));
|
265
|
button.setData(code);
|
266
|
if (nomenclaturalCode != null) {
|
267
|
button.setSelection(nomenclaturalCode.equals(code));
|
268
|
}
|
269
|
button.addSelectionListener(new SelectionAdapter() {
|
270
|
public void widgetSelected(SelectionEvent e) {
|
271
|
nomenclaturalCode = (NomenclaturalCode) e.widget.getData();
|
272
|
modifyText(null);
|
273
|
}
|
274
|
});
|
275
|
}
|
276
|
}
|
277
|
|
278
|
public void testDbConfiguration(){
|
279
|
testDbConfiguration(false);
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Tries to open a connection to the given dataSource. Generates a message on either
|
284
|
* failure or success
|
285
|
*/
|
286
|
public void testDbConfiguration(boolean ignoreSuccess){
|
287
|
try{
|
288
|
updateAndCheck();
|
289
|
updateDataSource();
|
290
|
getDataSource().testConnection();
|
291
|
if(!ignoreSuccess) {
|
292
|
MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!");
|
293
|
}
|
294
|
} catch(SQLException e){
|
295
|
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
|
296
|
+ System.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
|
297
|
+ System.getProperty("line.separator")
|
298
|
+ "Please double check the connection fields");
|
299
|
throw new RuntimeException(e);
|
300
|
} catch (ClassNotFoundException e) {
|
301
|
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
|
302
|
+ System.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
|
303
|
+ System.getProperty("line.separator")
|
304
|
+ "Please double check the connection fields");
|
305
|
throw new RuntimeException(e);
|
306
|
}
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Form implementation for the specific database
|
311
|
*/
|
312
|
public abstract void createDatabaseForm();
|
313
|
|
314
|
/**
|
315
|
* <p>updateLocation</p>
|
316
|
*/
|
317
|
public abstract void updateLocation();
|
318
|
|
319
|
/**
|
320
|
* <p>updateDataSource</p>
|
321
|
*/
|
322
|
public abstract void updateDataSource();
|
323
|
|
324
|
/**
|
325
|
* <p>checkPageComplete</p>
|
326
|
*/
|
327
|
public void checkPageComplete(){
|
328
|
boolean complete = false;
|
329
|
if(mode == CdmDataSourceWizard.Mode.CREATE) {
|
330
|
complete = database.length() != 0;
|
331
|
} else {
|
332
|
complete = name.length() != 0 && database.length() != 0;
|
333
|
}
|
334
|
this.setPageComplete(complete);
|
335
|
}
|
336
|
|
337
|
|
338
|
|
339
|
/**
|
340
|
* updates the current datasource with form values
|
341
|
*/
|
342
|
public void updateAuthentication(){
|
343
|
database = text_databaseName.getText();
|
344
|
username = text_username.getText();
|
345
|
password = text_password.getText();
|
346
|
}
|
347
|
|
348
|
/* (non-Javadoc)
|
349
|
* @see org.eclipse.jface.wizard.WizardPage#getNextPage()
|
350
|
*/
|
351
|
/** {@inheritDoc} */
|
352
|
@Override
|
353
|
public IWizardPage getNextPage() {
|
354
|
return null;
|
355
|
}
|
356
|
|
357
|
/**
|
358
|
* <p>Setter for the field <code>dataSource</code>.</p>
|
359
|
*
|
360
|
* @param dataSource the dataSource to set
|
361
|
*/
|
362
|
public void setDataSource(ICdmDataSource dataSource) {
|
363
|
this.dataSource = dataSource;
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
* <p>Getter for the field <code>dataSource</code>.</p>
|
368
|
*
|
369
|
* @return the dataSource
|
370
|
*/
|
371
|
public ICdmDataSource getUpdatedDataSource() {
|
372
|
updateDataSource();
|
373
|
return dataSource;
|
374
|
}
|
375
|
|
376
|
protected ICdmDataSource getDataSource() {
|
377
|
return dataSource;
|
378
|
}
|
379
|
|
380
|
/**
|
381
|
* <p>getDataSourceName</p>
|
382
|
*
|
383
|
* @return a {@link java.lang.String} object.
|
384
|
*/
|
385
|
public String getDataSourceName() {
|
386
|
return name;
|
387
|
}
|
388
|
|
389
|
/* (non-Javadoc)
|
390
|
* @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
|
391
|
*/
|
392
|
/** {@inheritDoc} */
|
393
|
public void modifyText(ModifyEvent e) {
|
394
|
|
395
|
name = text_dataSourceName.getText();
|
396
|
database = text_databaseName.getText();
|
397
|
|
398
|
switch(mode) {
|
399
|
case EDIT:
|
400
|
if(name.length() == 0){
|
401
|
name = "";
|
402
|
setErrorMessage("DataSource name must not be empty.");
|
403
|
this.setPageComplete(false);
|
404
|
return;
|
405
|
} else if ((!dataSource.getName().equals(name)) && (CdmDataSourceRepository.getDataSource(name) != null)) {
|
406
|
name = "";
|
407
|
setErrorMessage("DataSource with the same name already exists");
|
408
|
this.setPageComplete(false);
|
409
|
return;
|
410
|
}
|
411
|
break;
|
412
|
case CLONE:
|
413
|
if(name.length() == 0){
|
414
|
name = "";
|
415
|
setErrorMessage("DataSource name must not be empty.");
|
416
|
this.setPageComplete(false);
|
417
|
return;
|
418
|
} else if (CdmDataSourceRepository.getDataSource(name) != null) {
|
419
|
name = "";
|
420
|
setErrorMessage("DataSource with the same name already exists");
|
421
|
this.setPageComplete(false);
|
422
|
return;
|
423
|
}
|
424
|
break;
|
425
|
default:
|
426
|
break;
|
427
|
}
|
428
|
|
429
|
if(database.length() == 0){
|
430
|
setErrorMessage("Database name must not be empty.");
|
431
|
this.setPageComplete(false);
|
432
|
} else {
|
433
|
updateAndCheck();
|
434
|
setErrorMessage(null);
|
435
|
}
|
436
|
}
|
437
|
|
438
|
|
439
|
private void updateAndCheck() {
|
440
|
updateLocation();
|
441
|
updateAuthentication();
|
442
|
checkPageComplete();
|
443
|
}
|
444
|
/**
|
445
|
* <p>modifyTextWithoutTriggeringListeners</p>
|
446
|
*
|
447
|
* @param text a {@link org.eclipse.swt.widgets.Text} object.
|
448
|
* @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
|
449
|
* @param string a {@link java.lang.String} object.
|
450
|
*/
|
451
|
protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
|
452
|
text.removeModifyListener(listener);
|
453
|
text.setText(string);
|
454
|
text.addModifyListener(listener);
|
455
|
}
|
456
|
}
|