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
);
172 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
173 getDataSource().setName("");
174 getDataSource().setDatabase("");
175 text_dataSourceName
.setText("");
176 text_databaseName
.setText("");
181 * <p>createAuthenticationForm</p>
183 protected void createAuthenticationForm(){
184 // Create group composite for authentication data
185 authenticationGroup
= new Group(composite
, SWT
.NONE
);
186 authenticationGroup
.setText("Authentication");
187 authenticationGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 3));
188 GridLayout authenticationLayout
= new GridLayout();
189 authenticationLayout
.numColumns
= 2;
190 authenticationGroup
.setLayout(authenticationLayout
);
192 // Create database name label
193 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
194 databaseNameLabel
.setText("Database Name:");
196 // Create database name input
197 text_databaseName
= new Text(authenticationGroup
, SWT
.BORDER
);
198 text_databaseName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
201 // Create username label
202 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
203 usernameLabel
.setText("User Name:");
205 // Create username input
206 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
207 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
210 // Create password label
211 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
212 passwordLabel
.setText("Password:");
214 // Create password input
215 text_password
= new Text(authenticationGroup
, SWT
.BORDER
| SWT
.PASSWORD
);
216 text_password
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
221 * Initialize text fields
225 if(getDataSource() != null){
226 text_dataSourceName
.setText(getDataSource().getName());
227 text_databaseName
.setText(getDataSource().getDatabase());
228 text_username
.setText(getDataSource().getUsername());
229 text_password
.setText(getDataSource().getPassword());
231 // add listeners after setting text to avoid the modify event being called
232 // for the initial value
236 private void addListeners() {
237 text_dataSourceName
.addModifyListener(this);
238 text_databaseName
.addModifyListener(this);
239 text_username
.addModifyListener(this);
240 text_password
.addModifyListener(this);
243 private void removeListeners() {
244 text_dataSourceName
.removeModifyListener(this);
245 text_databaseName
.removeModifyListener(this);
246 text_username
.removeModifyListener(this);
247 text_password
.removeModifyListener(this);
250 * Create a radio button group to select a nomenclatural code from
252 private void createNomenclaturalCodeForm() {
253 nomenclaturalCodeGroup
= new Group(composite
, SWT
.NONE
);
254 nomenclaturalCodeGroup
.setLayout(new GridLayout());
256 nomenclaturalCode
= dataSource
!= null ? dataSource
.getNomenclaturalCode() : PreferencesUtil
.getPreferredNomenclaturalCode();
258 for (final NomenclaturalCode code
: NomenclaturalCodeHelper
.getSupportedCodes()) {
259 Button button
= new Button(nomenclaturalCodeGroup
, SWT
.RADIO
);
260 button
.setText(NomenclaturalCodeHelper
.getDescription(code
));
261 button
.setData(code
);
262 if (nomenclaturalCode
!= null) {
263 button
.setSelection(nomenclaturalCode
.equals(code
));
265 button
.addSelectionListener(new SelectionAdapter() {
266 public void widgetSelected(SelectionEvent e
) {
267 nomenclaturalCode
= (NomenclaturalCode
) e
.widget
.getData();
274 public void testDbConfiguration(){
275 testDbConfiguration(false);
279 * Tries to open a connection to the given dataSource. Generates a message on either
282 public void testDbConfiguration(boolean ignoreSuccess
){
286 getDataSource().testConnection();
288 MessageDialog
.openConfirm(parent
.getShell(), "Connection Test successful", "Test successful!");
290 } catch(SQLException e
){
291 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
292 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
293 + System
.getProperty("line.separator")
294 + "Please double check the connection fields");
295 throw new RuntimeException(e
);
296 } catch (ClassNotFoundException e
) {
297 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
298 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
299 + System
.getProperty("line.separator")
300 + "Please double check the connection fields");
301 throw new RuntimeException(e
);
306 * Form implementation for the specific database
308 public abstract void createDatabaseForm();
311 * <p>updateLocation</p>
313 public abstract void updateLocation();
316 * <p>updateDataSource</p>
318 public abstract void updateDataSource();
321 * <p>checkPageComplete</p>
323 public void checkPageComplete(){
324 boolean complete
= false;
325 if(mode
== CdmDataSourceWizard
.Mode
.CREATE
) {
326 complete
= database
.length() != 0;
328 complete
= name
.length() != 0 && database
.length() != 0;
330 this.setPageComplete(complete
);
336 * updates the current datasource with form values
338 public void updateAuthentication(){
339 database
= text_databaseName
.getText();
340 username
= text_username
.getText();
341 password
= text_password
.getText();
345 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
349 public IWizardPage
getNextPage() {
354 * <p>Setter for the field <code>dataSource</code>.</p>
356 * @param dataSource the dataSource to set
358 public void setDataSource(ICdmDataSource dataSource
) {
359 this.dataSource
= dataSource
;
363 * <p>Getter for the field <code>dataSource</code>.</p>
365 * @return the dataSource
367 public ICdmDataSource
getUpdatedDataSource() {
372 protected ICdmDataSource
getDataSource() {
377 * <p>getDataSourceName</p>
379 * @return a {@link java.lang.String} object.
381 public String
getDataSourceName() {
386 * @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
389 public void modifyText(ModifyEvent e
) {
390 //this.setPageComplete(false);
391 name
= text_dataSourceName
.getText();
392 database
= text_databaseName
.getText();
396 if(name
.length() == 0){
398 setErrorMessage("DataSource name must not be empty.");
399 this.setPageComplete(false);
401 } else if ((!dataSource
.getName().equals(name
)) && (CdmDataSourceRepository
.getDataSource(name
) != null)) {
403 setErrorMessage("DataSource with the same name already exists");
404 this.setPageComplete(false);
409 if(name
.length() == 0){
411 setErrorMessage("DataSource name must not be empty.");
412 this.setPageComplete(false);
413 } else if (CdmDataSourceRepository
.getDataSource(name
) != null) {
415 setErrorMessage("DataSource with the same name already exists");
416 this.setPageComplete(false);
424 if(database
.length() == 0){
425 setErrorMessage("Database name must not be empty.");
426 this.setPageComplete(false);
429 setErrorMessage(null);
434 private void updateAndCheck() {
436 updateAuthentication();
440 * <p>modifyTextWithoutTriggeringListeners</p>
442 * @param text a {@link org.eclipse.swt.widgets.Text} object.
443 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
444 * @param string a {@link java.lang.String} object.
446 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
447 text
.removeModifyListener(listener
);
448 text
.setText(string
);
449 text
.addModifyListener(listener
);