Revision aeeab9d0
Added by Cherian Mathew over 9 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java | ||
---|---|---|
1 | 1 |
// $Id$ |
2 | 2 |
/** |
3 | 3 |
* Copyright (C) 2007 EDIT |
4 |
* European Distributed Institute of Taxonomy
|
|
4 |
* European Distributed Institute of Taxonomy |
|
5 | 5 |
* http://www.e-taxonomy.eu |
6 |
*
|
|
6 |
* |
|
7 | 7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
8 | 8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
9 | 9 |
*/ |
... | ... | |
46 | 46 |
*/ |
47 | 47 |
public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener { |
48 | 48 |
private ICdmDataSource dataSource; |
49 |
|
|
49 |
|
|
50 | 50 |
protected Text text_password; |
51 | 51 |
protected Text text_databaseName; |
52 | 52 |
protected Text text_dataSourceName; |
53 | 53 |
protected Text text_username; |
54 |
|
|
54 |
|
|
55 | 55 |
protected Group authenticationGroup; |
56 | 56 |
protected Group locationGroup; |
57 | 57 |
protected Group nomenclaturalCodeGroup; |
... | ... | |
64 | 64 |
protected String database; |
65 | 65 |
protected String username; |
66 | 66 |
protected String password; |
67 |
|
|
67 |
|
|
68 | 68 |
protected NomenclaturalCode nomenclaturalCode; |
69 |
|
|
69 |
|
|
70 | 70 |
CdmDataSourceWizard.Mode mode; |
71 |
|
|
71 |
|
|
72 | 72 |
/** |
73 | 73 |
* <p>Constructor for CdmDataSourceCredentialsWizardPage.</p> |
74 | 74 |
* |
... | ... | |
77 | 77 |
protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) { |
78 | 78 |
super(pageName); |
79 | 79 |
this.setPageComplete(false); |
80 |
setDataSource(dataSource);
|
|
80 |
setDataSource(dataSource); |
|
81 | 81 |
mode = CdmDataSourceWizard.Mode.CREATE; |
82 | 82 |
} |
83 |
|
|
83 |
|
|
84 | 84 |
/** |
85 | 85 |
* <p>Constructor for CdmDataSourceCredentialsWizardPage.</p> |
86 | 86 |
* |
... | ... | |
89 | 89 |
protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) { |
90 | 90 |
super(pageName); |
91 | 91 |
this.setPageComplete(false); |
92 |
|
|
92 |
|
|
93 | 93 |
this.mode = mode; |
94 | 94 |
if(mode == CdmDataSourceWizard.Mode.CLONE) { |
95 |
setDataSource(CdmDataSource.NewInstance(dataSource));
|
|
95 |
setDataSource(CdmDataSource.NewInstance(dataSource)); |
|
96 | 96 |
} else { |
97 |
setDataSource(dataSource);
|
|
97 |
setDataSource(dataSource); |
|
98 | 98 |
} |
99 | 99 |
} |
100 |
|
|
100 |
|
|
101 | 101 |
/* (non-Javadoc) |
102 | 102 |
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) |
103 | 103 |
*/ |
104 | 104 |
/** {@inheritDoc} */ |
105 |
public void createControl(Composite parent) { |
|
105 |
@Override |
|
106 |
public void createControl(Composite parent) { |
|
106 | 107 |
this.parent = parent; |
107 |
|
|
108 |
// Create top-level composite
|
|
108 |
|
|
109 |
// Create top-level composite |
|
109 | 110 |
parent.setLayout(new GridLayout()); |
110 | 111 |
composite = new Composite(parent, SWT.NONE); |
111 | 112 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5)); |
... | ... | |
132 | 133 |
if(getDataSource() == null) { |
133 | 134 |
editDatasourceComposite.setVisible(false); |
134 | 135 |
} |
135 |
|
|
136 |
|
|
136 | 137 |
// create a database specific form |
137 | 138 |
createDatabaseForm(); |
138 |
|
|
139 |
|
|
139 | 140 |
// create the authentication input fields |
140 | 141 |
createAuthenticationForm(); |
141 |
|
|
142 |
|
|
142 | 143 |
// create nomenclatural code combo |
143 | 144 |
createNomenclaturalCodeForm(); |
144 |
|
|
145 |
|
|
145 | 146 |
// Create composite for buttons |
146 | 147 |
Composite buttonComposite = new Composite(composite, SWT.NONE); |
147 | 148 |
buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false)); |
148 | 149 |
GridLayout buttonLayout = new GridLayout(); |
149 | 150 |
buttonLayout.numColumns = 1; |
150 | 151 |
buttonComposite.setLayout(buttonLayout); |
151 |
|
|
152 |
|
|
152 | 153 |
// Create test connection button |
153 | 154 |
Button testButton = new Button(buttonComposite, SWT.NONE); |
154 | 155 |
testButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
155 | 156 |
testButton.setText("Test connection"); |
156 |
|
|
157 |
|
|
157 | 158 |
// Test connection when button is pressed |
158 | 159 |
testButton.addSelectionListener(new SelectionAdapter() { |
159 | 160 |
/* (non-Javadoc) |
... | ... | |
164 | 165 |
testDbConfiguration(); |
165 | 166 |
} |
166 | 167 |
}); |
167 |
|
|
168 |
|
|
168 | 169 |
setControl(composite); |
169 |
|
|
170 |
init();
|
|
171 |
|
|
170 |
|
|
171 |
init(); |
|
172 |
|
|
172 | 173 |
} |
173 | 174 |
|
174 | 175 |
/** |
... | ... | |
182 | 183 |
GridLayout authenticationLayout = new GridLayout(); |
183 | 184 |
authenticationLayout.numColumns = 2; |
184 | 185 |
authenticationGroup.setLayout(authenticationLayout); |
185 |
|
|
186 |
|
|
186 | 187 |
// Create database name label |
187 | 188 |
Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE); |
188 | 189 |
databaseNameLabel.setText("Database Name:"); |
... | ... | |
190 | 191 |
// Create database name input |
191 | 192 |
text_databaseName = new Text(authenticationGroup, SWT.BORDER); |
192 | 193 |
text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
193 |
|
|
194 |
|
|
194 | 195 |
|
195 | 196 |
// Create username label |
196 | 197 |
Label usernameLabel = new Label(authenticationGroup, SWT.NONE); |
197 | 198 |
usernameLabel.setText("User Name:"); |
198 |
|
|
199 |
|
|
199 | 200 |
// Create username input |
200 | 201 |
text_username = new Text(authenticationGroup, SWT.BORDER); |
201 | 202 |
text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
202 |
|
|
203 |
|
|
203 |
|
|
204 |
|
|
204 | 205 |
// Create password label |
205 | 206 |
Label passwordLabel = new Label(authenticationGroup, SWT.NONE); |
206 | 207 |
passwordLabel.setText("Password:"); |
... | ... | |
208 | 209 |
// Create password input |
209 | 210 |
text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD); |
210 | 211 |
text_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
211 |
|
|
212 |
|
|
212 | 213 |
} |
213 |
|
|
214 |
|
|
214 | 215 |
/** |
215 | 216 |
* Initialize text fields |
216 | 217 |
*/ |
217 |
public void init() {
|
|
218 |
public void init() { |
|
218 | 219 |
removeListeners(); |
219 |
if(getDataSource() != null){
|
|
220 |
if(getDataSource() != null){ |
|
220 | 221 |
text_dataSourceName.setText(getDataSource().getName()); |
221 | 222 |
text_databaseName.setText(getDataSource().getDatabase()); |
222 | 223 |
text_username.setText(getDataSource().getUsername()); |
... | ... | |
225 | 226 |
// add listeners after setting text to avoid the modify event being called |
226 | 227 |
// for the initial value |
227 | 228 |
addListeners(); |
228 |
|
|
229 |
|
|
229 | 230 |
// in the case of cloning we use the same datasource info |
230 | 231 |
// except for the name |
231 | 232 |
if(mode == CdmDataSourceWizard.Mode.CLONE) { |
232 |
getDataSource().setName("");
|
|
233 |
text_dataSourceName.setText("");
|
|
233 |
getDataSource().setName(""); |
|
234 |
text_dataSourceName.setText(""); |
|
234 | 235 |
} else { |
235 | 236 |
name = text_dataSourceName.getText(); |
236 | 237 |
} |
237 |
|
|
238 |
|
|
238 | 239 |
} |
239 |
|
|
240 |
|
|
240 | 241 |
private void addListeners() { |
241 |
text_dataSourceName.addModifyListener(this);
|
|
242 |
text_dataSourceName.addModifyListener(this); |
|
242 | 243 |
text_databaseName.addModifyListener(this); |
243 | 244 |
text_username.addModifyListener(this); |
244 | 245 |
text_password.addModifyListener(this); |
245 | 246 |
} |
246 |
|
|
247 |
|
|
247 | 248 |
private void removeListeners() { |
248 | 249 |
text_dataSourceName.removeModifyListener(this); |
249 | 250 |
text_databaseName.removeModifyListener(this); |
... | ... | |
256 | 257 |
private void createNomenclaturalCodeForm() { |
257 | 258 |
nomenclaturalCodeGroup = new Group(composite , SWT.NONE); |
258 | 259 |
nomenclaturalCodeGroup.setLayout(new GridLayout()); |
259 |
|
|
260 |
|
|
260 | 261 |
nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode(); |
261 |
|
|
262 |
|
|
262 | 263 |
for (final NomenclaturalCode code : NomenclaturalCodeHelper.getSupportedCodes()) { |
263 | 264 |
Button button = new Button(nomenclaturalCodeGroup, SWT.RADIO); |
264 | 265 |
button.setText(NomenclaturalCodeHelper.getDescription(code)); |
... | ... | |
267 | 268 |
button.setSelection(nomenclaturalCode.equals(code)); |
268 | 269 |
} |
269 | 270 |
button.addSelectionListener(new SelectionAdapter() { |
270 |
public void widgetSelected(SelectionEvent e) { |
|
271 |
@Override |
|
272 |
public void widgetSelected(SelectionEvent e) { |
|
271 | 273 |
nomenclaturalCode = (NomenclaturalCode) e.widget.getData(); |
272 | 274 |
modifyText(null); |
273 | 275 |
} |
274 | 276 |
}); |
275 |
}
|
|
277 |
} |
|
276 | 278 |
} |
277 |
|
|
279 |
|
|
278 | 280 |
public void testDbConfiguration(){ |
279 | 281 |
testDbConfiguration(false); |
280 | 282 |
} |
281 |
|
|
283 |
|
|
282 | 284 |
/** |
283 | 285 |
* Tries to open a connection to the given dataSource. Generates a message on either |
284 | 286 |
* failure or success |
... | ... | |
292 | 294 |
MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!"); |
293 | 295 |
} |
294 | 296 |
} catch(SQLException e){ |
295 |
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
|
|
297 |
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() |
|
296 | 298 |
+ System.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7 |
297 | 299 |
+ System.getProperty("line.separator") |
298 | 300 |
+ "Please double check the connection fields"); |
299 |
throw new RuntimeException(e); |
|
300 | 301 |
} catch (ClassNotFoundException e) { |
301 |
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
|
|
302 |
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() |
|
302 | 303 |
+ System.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7 |
303 | 304 |
+ System.getProperty("line.separator") |
304 | 305 |
+ "Please double check the connection fields"); |
305 |
throw new RuntimeException(e); |
|
306 |
} |
|
306 |
} |
|
307 | 307 |
} |
308 |
|
|
308 |
|
|
309 | 309 |
/** |
310 | 310 |
* Form implementation for the specific database |
311 | 311 |
*/ |
312 |
public abstract void createDatabaseForm();
|
|
313 |
|
|
312 |
public abstract void createDatabaseForm(); |
|
313 |
|
|
314 | 314 |
/** |
315 | 315 |
* <p>updateLocation</p> |
316 | 316 |
*/ |
317 | 317 |
public abstract void updateLocation(); |
318 |
|
|
318 |
|
|
319 | 319 |
/** |
320 | 320 |
* <p>updateDataSource</p> |
321 | 321 |
*/ |
322 | 322 |
public abstract void updateDataSource(); |
323 |
|
|
323 |
|
|
324 | 324 |
/** |
325 | 325 |
* <p>checkPageComplete</p> |
326 | 326 |
*/ |
... | ... | |
328 | 328 |
boolean complete = false; |
329 | 329 |
if(mode == CdmDataSourceWizard.Mode.CREATE) { |
330 | 330 |
complete = database.length() != 0; |
331 |
} else {
|
|
331 |
} else { |
|
332 | 332 |
complete = name.length() != 0 && database.length() != 0; |
333 |
}
|
|
333 |
} |
|
334 | 334 |
this.setPageComplete(complete); |
335 | 335 |
} |
336 |
|
|
337 | 336 |
|
338 |
|
|
337 |
|
|
338 |
|
|
339 | 339 |
/** |
340 | 340 |
* updates the current datasource with form values |
341 | 341 |
*/ |
342 |
public void updateAuthentication(){
|
|
342 |
public void updateAuthentication(){ |
|
343 | 343 |
database = text_databaseName.getText(); |
344 | 344 |
username = text_username.getText(); |
345 | 345 |
password = text_password.getText(); |
... | ... | |
359 | 359 |
* |
360 | 360 |
* @param dataSource the dataSource to set |
361 | 361 |
*/ |
362 |
public void setDataSource(ICdmDataSource dataSource) {
|
|
362 |
public void setDataSource(ICdmDataSource dataSource) { |
|
363 | 363 |
this.dataSource = dataSource; |
364 | 364 |
} |
365 | 365 |
|
... | ... | |
372 | 372 |
updateDataSource(); |
373 | 373 |
return dataSource; |
374 | 374 |
} |
375 |
|
|
376 |
protected ICdmDataSource getDataSource() {
|
|
375 |
|
|
376 |
protected ICdmDataSource getDataSource() { |
|
377 | 377 |
return dataSource; |
378 | 378 |
} |
379 |
|
|
379 |
|
|
380 | 380 |
/** |
381 | 381 |
* <p>getDataSourceName</p> |
382 | 382 |
* |
... | ... | |
385 | 385 |
public String getDataSourceName() { |
386 | 386 |
return name; |
387 | 387 |
} |
388 |
|
|
388 |
|
|
389 | 389 |
/* (non-Javadoc) |
390 | 390 |
* @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) |
391 | 391 |
*/ |
392 | 392 |
/** {@inheritDoc} */ |
393 |
public void modifyText(ModifyEvent e) { |
|
394 |
|
|
393 |
@Override |
|
394 |
public void modifyText(ModifyEvent e) { |
|
395 |
|
|
395 | 396 |
name = text_dataSourceName.getText(); |
396 | 397 |
database = text_databaseName.getText(); |
397 | 398 |
|
398 | 399 |
switch(mode) { |
399 | 400 |
case EDIT: |
400 |
if(name.length() == 0){
|
|
401 |
if(name.length() == 0){ |
|
401 | 402 |
name = ""; |
402 | 403 |
setErrorMessage("DataSource name must not be empty."); |
403 | 404 |
this.setPageComplete(false); |
... | ... | |
410 | 411 |
} |
411 | 412 |
break; |
412 | 413 |
case CLONE: |
413 |
if(name.length() == 0){
|
|
414 |
if(name.length() == 0){ |
|
414 | 415 |
name = ""; |
415 | 416 |
setErrorMessage("DataSource name must not be empty."); |
416 | 417 |
this.setPageComplete(false); |
... | ... | |
425 | 426 |
default: |
426 | 427 |
break; |
427 | 428 |
} |
428 |
|
|
429 |
if(database.length() == 0){
|
|
429 |
|
|
430 |
if(database.length() == 0){ |
|
430 | 431 |
setErrorMessage("Database name must not be empty."); |
431 | 432 |
this.setPageComplete(false); |
432 |
} else {
|
|
433 |
} else { |
|
433 | 434 |
updateAndCheck(); |
434 |
setErrorMessage(null); |
|
435 |
} |
|
436 |
} |
|
435 |
setErrorMessage(null); |
|
436 |
} |
|
437 |
} |
|
438 |
|
|
437 | 439 |
|
438 |
|
|
439 | 440 |
private void updateAndCheck() { |
440 | 441 |
updateLocation(); |
441 | 442 |
updateAuthentication(); |
Also available in: Unified diff
removed throwing of runtime exception since we already have a warning (#4365)