3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.datasource
.wizard
;
13 import java
.sql
.SQLException
;
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
;
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
;
41 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
47 public abstract class CdmDataSourceCredentialsWizardPage
extends WizardPage
implements ModifyListener
{
48 private ICdmDataSource dataSource
;
50 protected Text text_password
;
51 protected Text text_databaseName
;
52 protected Text text_dataSourceName
;
53 protected Text text_username
;
55 protected Group authenticationGroup
;
56 protected Group locationGroup
;
57 protected Group nomenclaturalCodeGroup
;
59 protected Composite composite
;
61 protected Composite parent
;
63 protected String name
;
64 protected String database
;
65 protected String username
;
66 protected String password
;
68 protected NomenclaturalCode nomenclaturalCode
;
70 CdmDataSourceWizard
.Mode mode
;
73 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
75 * @param pageName a {@link java.lang.String} object.
77 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
) {
79 this.setPageComplete(false);
80 setDataSource(dataSource
);
81 mode
= CdmDataSourceWizard
.Mode
.CREATE
;
85 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
87 * @param pageName a {@link java.lang.String} object.
89 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
, CdmDataSourceWizard
.Mode mode
) {
91 this.setPageComplete(false);
94 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
95 setDataSource(CdmDataSource
.NewInstance(dataSource
));
97 setDataSource(dataSource
);
102 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
105 public void createControl(Composite parent
) {
106 this.parent
= parent
;
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
);
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
);
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));
132 if(getDataSource() == null) {
133 editDatasourceComposite
.setVisible(false);
136 // create a database specific form
137 createDatabaseForm();
139 // create the authentication input fields
140 createAuthenticationForm();
142 // create nomenclatural code combo
143 createNomenclaturalCodeForm();
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
);
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");
157 // Test connection when button is pressed
158 testButton
.addSelectionListener(new SelectionAdapter() {
160 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
163 public void widgetSelected(SelectionEvent e
) {
164 testDbConfiguration();
168 setControl(composite
);
175 * <p>createAuthenticationForm</p>
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
);
186 // Create database name label
187 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
188 databaseNameLabel
.setText("Database Name:");
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));
195 // Create username label
196 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
197 usernameLabel
.setText("User Name:");
199 // Create username input
200 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
201 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
204 // Create password label
205 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
206 passwordLabel
.setText("Password:");
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));
215 * Initialize text fields
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());
225 // add listeners after setting text to avoid the modify event being called
226 // for the initial value
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("");
235 name
= text_dataSourceName
.getText();
240 private void addListeners() {
241 text_dataSourceName
.addModifyListener(this);
242 text_databaseName
.addModifyListener(this);
243 text_username
.addModifyListener(this);
244 text_password
.addModifyListener(this);
247 private void removeListeners() {
248 text_dataSourceName
.removeModifyListener(this);
249 text_databaseName
.removeModifyListener(this);
250 text_username
.removeModifyListener(this);
251 text_password
.removeModifyListener(this);
254 * Create a radio button group to select a nomenclatural code from
256 private void createNomenclaturalCodeForm() {
257 nomenclaturalCodeGroup
= new Group(composite
, SWT
.NONE
);
258 nomenclaturalCodeGroup
.setLayout(new GridLayout());
260 nomenclaturalCode
= dataSource
!= null ? dataSource
.getNomenclaturalCode() : PreferencesUtil
.getPreferredNomenclaturalCode();
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
));
269 button
.addSelectionListener(new SelectionAdapter() {
270 public void widgetSelected(SelectionEvent e
) {
271 nomenclaturalCode
= (NomenclaturalCode
) e
.widget
.getData();
278 public void testDbConfiguration(){
279 testDbConfiguration(false);
283 * Tries to open a connection to the given dataSource. Generates a message on either
286 public void testDbConfiguration(boolean ignoreSuccess
){
290 getDataSource().testConnection();
292 MessageDialog
.openConfirm(parent
.getShell(), "Connection Test successful", "Test successful!");
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
);
310 * Form implementation for the specific database
312 public abstract void createDatabaseForm();
315 * <p>updateLocation</p>
317 public abstract void updateLocation();
320 * <p>updateDataSource</p>
322 public abstract void updateDataSource();
325 * <p>checkPageComplete</p>
327 public void checkPageComplete(){
328 boolean complete
= false;
329 if(mode
== CdmDataSourceWizard
.Mode
.CREATE
) {
330 complete
= database
.length() != 0;
332 complete
= name
.length() != 0 && database
.length() != 0;
334 this.setPageComplete(complete
);
340 * updates the current datasource with form values
342 public void updateAuthentication(){
343 database
= text_databaseName
.getText();
344 username
= text_username
.getText();
345 password
= text_password
.getText();
349 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
353 public IWizardPage
getNextPage() {
358 * <p>Setter for the field <code>dataSource</code>.</p>
360 * @param dataSource the dataSource to set
362 public void setDataSource(ICdmDataSource dataSource
) {
363 this.dataSource
= dataSource
;
367 * <p>Getter for the field <code>dataSource</code>.</p>
369 * @return the dataSource
371 public ICdmDataSource
getUpdatedDataSource() {
376 protected ICdmDataSource
getDataSource() {
381 * <p>getDataSourceName</p>
383 * @return a {@link java.lang.String} object.
385 public String
getDataSourceName() {
390 * @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
393 public void modifyText(ModifyEvent e
) {
395 name
= text_dataSourceName
.getText();
396 database
= text_databaseName
.getText();
400 if(name
.length() == 0){
402 setErrorMessage("DataSource name must not be empty.");
403 this.setPageComplete(false);
405 } else if ((!dataSource
.getName().equals(name
)) && (CdmDataSourceRepository
.getDataSource(name
) != null)) {
407 setErrorMessage("DataSource with the same name already exists");
408 this.setPageComplete(false);
413 if(name
.length() == 0){
415 setErrorMessage("DataSource name must not be empty.");
416 this.setPageComplete(false);
418 } else if (CdmDataSourceRepository
.getDataSource(name
) != null) {
420 setErrorMessage("DataSource with the same name already exists");
421 this.setPageComplete(false);
429 if(database
.length() == 0){
430 setErrorMessage("Database name must not be empty.");
431 this.setPageComplete(false);
434 setErrorMessage(null);
439 private void updateAndCheck() {
441 updateAuthentication();
445 * <p>modifyTextWithoutTriggeringListeners</p>
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.
451 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
452 text
.removeModifyListener(listener
);
453 text
.setText(string
);
454 text
.addModifyListener(listener
);