Revision 5ed160dc
Added by Cherian Mathew almost 10 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java | ||
---|---|---|
28 | 28 |
import org.eclipse.swt.widgets.Label; |
29 | 29 |
import org.eclipse.swt.widgets.Text; |
30 | 30 |
|
31 |
import eu.etaxonomy.cdm.database.CdmDataSource; |
|
31 | 32 |
import eu.etaxonomy.cdm.database.CdmPersistentDataSource; |
32 | 33 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
33 | 34 |
import eu.etaxonomy.cdm.model.name.NomenclaturalCode; |
35 |
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository; |
|
34 | 36 |
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper; |
35 | 37 |
import eu.etaxonomy.taxeditor.preference.PreferencesUtil; |
36 | 38 |
|
... | ... | |
42 | 44 |
* @created 19.05.2009 |
43 | 45 |
* @version 1.0 |
44 | 46 |
*/ |
45 |
public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener{ |
|
46 |
|
|
47 |
public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener { |
|
47 | 48 |
private ICdmDataSource dataSource; |
48 | 49 |
|
49 | 50 |
protected Text text_password; |
50 | 51 |
protected Text text_databaseName; |
52 |
protected Text text_dataSourceName; |
|
51 | 53 |
protected Text text_username; |
52 | 54 |
|
53 | 55 |
protected Group authenticationGroup; |
... | ... | |
58 | 60 |
|
59 | 61 |
protected Composite parent; |
60 | 62 |
|
63 |
protected String name; |
|
61 | 64 |
protected String database; |
62 | 65 |
protected String username; |
63 | 66 |
protected String password; |
64 | 67 |
|
65 | 68 |
protected NomenclaturalCode nomenclaturalCode; |
66 | 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 |
|
|
67 | 83 |
/** |
68 | 84 |
* <p>Constructor for CdmDataSourceCredentialsWizardPage.</p> |
69 | 85 |
* |
70 | 86 |
* @param pageName a {@link java.lang.String} object. |
71 | 87 |
*/ |
72 |
protected CdmDataSourceCredentialsWizardPage(String pageName) { |
|
88 |
protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
|
|
73 | 89 |
super(pageName); |
74 | 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 |
} |
|
75 | 98 |
} |
76 | 99 |
|
77 | 100 |
/* (non-Javadoc) |
... | ... | |
82 | 105 |
this.parent = parent; |
83 | 106 |
|
84 | 107 |
// Create top-level composite |
85 |
|
|
108 |
parent.setLayout(new GridLayout()); |
|
86 | 109 |
composite = new Composite(parent, SWT.NONE); |
87 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
|
110 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5));
|
|
88 | 111 |
GridLayout formLayout = new GridLayout(); |
89 | 112 |
formLayout.numColumns = 2; |
90 | 113 |
composite.setLayout(formLayout); |
91 | 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 |
|
|
92 | 135 |
// create a database specific form |
93 | 136 |
createDatabaseForm(); |
94 | 137 |
|
... | ... | |
102 | 145 |
Composite buttonComposite = new Composite(composite, SWT.NONE); |
103 | 146 |
buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false)); |
104 | 147 |
GridLayout buttonLayout = new GridLayout(); |
105 |
buttonLayout.numColumns = 2;
|
|
148 |
buttonLayout.numColumns = 1;
|
|
106 | 149 |
buttonComposite.setLayout(buttonLayout); |
107 | 150 |
|
108 | 151 |
// Create test connection button |
... | ... | |
122 | 165 |
}); |
123 | 166 |
|
124 | 167 |
setControl(composite); |
125 |
|
|
168 |
|
|
126 | 169 |
init(); |
170 |
|
|
171 |
if(mode == CdmDataSourceWizard.Mode.CLONE) { |
|
172 |
getDataSource().setName(""); |
|
173 |
getDataSource().setDatabase(""); |
|
174 |
text_dataSourceName.setText(""); |
|
175 |
text_databaseName.setText(""); |
|
176 |
} |
|
127 | 177 |
} |
128 | 178 |
|
129 | 179 |
/** |
... | ... | |
133 | 183 |
// Create group composite for authentication data |
134 | 184 |
authenticationGroup = new Group(composite, SWT.NONE); |
135 | 185 |
authenticationGroup.setText("Authentication"); |
136 |
authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
|
|
186 |
authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 3));
|
|
137 | 187 |
GridLayout authenticationLayout = new GridLayout(); |
138 | 188 |
authenticationLayout.numColumns = 2; |
139 | 189 |
authenticationGroup.setLayout(authenticationLayout); |
... | ... | |
145 | 195 |
// Create database name input |
146 | 196 |
text_databaseName = new Text(authenticationGroup, SWT.BORDER); |
147 | 197 |
text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
148 |
text_databaseName.addModifyListener(this); |
|
198 |
|
|
149 | 199 |
|
150 | 200 |
// Create username label |
151 | 201 |
Label usernameLabel = new Label(authenticationGroup, SWT.NONE); |
... | ... | |
154 | 204 |
// Create username input |
155 | 205 |
text_username = new Text(authenticationGroup, SWT.BORDER); |
156 | 206 |
text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
157 |
text_username.addModifyListener(this); |
|
207 |
|
|
158 | 208 |
|
159 | 209 |
// Create password label |
160 | 210 |
Label passwordLabel = new Label(authenticationGroup, SWT.NONE); |
... | ... | |
163 | 213 |
// Create password input |
164 | 214 |
text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD); |
165 | 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); |
|
166 | 239 |
text_password.addModifyListener(this); |
167 | 240 |
} |
168 | 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 |
} |
|
169 | 248 |
/** |
170 | 249 |
* Create a radio button group to select a nomenclatural code from |
171 | 250 |
*/ |
... | ... | |
191 | 270 |
} |
192 | 271 |
} |
193 | 272 |
|
194 |
|
|
273 |
public void testDbConfiguration(){ |
|
274 |
testDbConfiguration(false); |
|
275 |
} |
|
195 | 276 |
|
196 | 277 |
/** |
197 | 278 |
* Tries to open a connection to the given dataSource. Generates a message on either |
198 | 279 |
* failure or success |
199 | 280 |
*/ |
200 |
public void testDbConfiguration(){ |
|
281 |
public void testDbConfiguration(boolean ignoreSuccess){
|
|
201 | 282 |
try{ |
283 |
updateAndCheck(); |
|
284 |
updateDataSource(); |
|
202 | 285 |
getDataSource().testConnection(); |
203 |
MessageDialog.openConfirm(parent.getShell(), "Test successful", "Test successful!"); |
|
286 |
if(!ignoreSuccess) { |
|
287 |
MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!"); |
|
288 |
} |
|
204 | 289 |
} catch(SQLException e){ |
205 |
MessageDialog.openWarning(parent.getShell(), "Test unsuccessful", "Reason: " + e.getMessage()); |
|
290 |
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() |
|
291 |
+ System.lineSeparator() |
|
292 |
+ System.lineSeparator() |
|
293 |
+ "Please double check the connection fields"); |
|
206 | 294 |
throw new RuntimeException(e); |
207 | 295 |
} catch (ClassNotFoundException e) { |
208 |
MessageDialog.openWarning(parent.getShell(), "Test unsuccessful", "Reason: " + e.getMessage()); |
|
296 |
MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() |
|
297 |
+ System.lineSeparator() |
|
298 |
+ System.lineSeparator() |
|
299 |
+ "Please double check the connection fields"); |
|
209 | 300 |
throw new RuntimeException(e); |
210 | 301 |
} |
211 | 302 |
} |
... | ... | |
229 | 320 |
* <p>checkPageComplete</p> |
230 | 321 |
*/ |
231 | 322 |
public void checkPageComplete(){ |
232 |
boolean complete = database.length() != 0; |
|
233 |
// complete &= username.length() != 0; |
|
234 |
|
|
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 |
} |
|
235 | 329 |
this.setPageComplete(complete); |
236 | 330 |
} |
237 | 331 |
|
238 |
/** |
|
239 |
* Initialize text fields |
|
240 |
*/ |
|
241 |
public void init() { |
|
242 |
if(getDataSource() != null){ |
|
243 |
modifyTextWithoutTriggeringListeners(text_databaseName, this, getDataSource().getDatabase()); |
|
244 |
modifyTextWithoutTriggeringListeners(text_username, this, getDataSource().getUsername()); |
|
245 |
modifyTextWithoutTriggeringListeners(text_password, this, getDataSource().getPassword()); |
|
246 |
} |
|
247 |
} |
|
332 |
|
|
248 | 333 |
|
249 | 334 |
/** |
250 | 335 |
* updates the current datasource with form values |
251 | 336 |
*/ |
252 |
public void updateAuthentication(){ |
|
253 |
|
|
337 |
public void updateAuthentication(){ |
|
254 | 338 |
database = text_databaseName.getText(); |
255 | 339 |
username = text_username.getText(); |
256 | 340 |
password = text_password.getText(); |
... | ... | |
270 | 354 |
* |
271 | 355 |
* @param dataSource the dataSource to set |
272 | 356 |
*/ |
273 |
public void setDataSource(ICdmDataSource dataSource) { |
|
357 |
public void setDataSource(ICdmDataSource dataSource) {
|
|
274 | 358 |
this.dataSource = dataSource; |
275 | 359 |
} |
276 | 360 |
|
... | ... | |
279 | 363 |
* |
280 | 364 |
* @return the dataSource |
281 | 365 |
*/ |
282 |
public ICdmDataSource getDataSource() { |
|
366 |
public ICdmDataSource getUpdatedDataSource() { |
|
367 |
updateDataSource(); |
|
368 |
return dataSource; |
|
369 |
} |
|
370 |
|
|
371 |
protected ICdmDataSource getDataSource() { |
|
283 | 372 |
return dataSource; |
284 | 373 |
} |
285 | 374 |
|
... | ... | |
289 | 378 |
* @return a {@link java.lang.String} object. |
290 | 379 |
*/ |
291 | 380 |
public String getDataSourceName() { |
292 |
if(dataSource instanceof CdmPersistentDataSource){ |
|
293 |
return ((CdmPersistentDataSource) dataSource).getName(); |
|
294 |
} |
|
295 |
return null; |
|
381 |
return name; |
|
296 | 382 |
} |
297 | 383 |
|
298 | 384 |
/* (non-Javadoc) |
299 |
* @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) |
|
385 |
* @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
|
|
300 | 386 |
*/ |
301 | 387 |
/** {@inheritDoc} */ |
302 | 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() { |
|
303 | 434 |
updateLocation(); |
304 | 435 |
updateAuthentication(); |
305 |
updateDataSource(); |
|
306 | 436 |
checkPageComplete(); |
307 |
} |
|
308 |
|
|
437 |
} |
|
309 | 438 |
/** |
310 | 439 |
* <p>modifyTextWithoutTriggeringListeners</p> |
311 | 440 |
* |
Also available in: Unified diff
refactored wizard dialog and pages to allow for easy in place editing and cloning of datasources.
CdmDataSourceRepository : added methods for (I)PersistentRemoteSource