2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.datasource
.wizard
;
12 import java
.sql
.SQLException
;
14 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
15 import org
.eclipse
.jface
.wizard
.IWizardPage
;
16 import org
.eclipse
.jface
.wizard
.WizardPage
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.ModifyEvent
;
19 import org
.eclipse
.swt
.events
.ModifyListener
;
20 import org
.eclipse
.swt
.events
.SelectionAdapter
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Group
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
30 import eu
.etaxonomy
.cdm
.database
.CdmDataSource
;
31 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
32 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
36 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
42 public abstract class CdmDataSourceCredentialsWizardPage
extends WizardPage
implements ModifyListener
{
43 private ICdmDataSource dataSource
;
45 protected Text text_password
;
46 protected Text text_databaseName
;
47 protected Text text_dataSourceName
;
48 protected Text text_username
;
50 protected Group authenticationGroup
;
51 protected Group locationGroup
;
53 protected Composite composite
;
55 protected Composite parent
;
57 protected String name
;
58 protected String database
;
59 protected String username
;
60 protected String password
;
62 CdmDataSourceWizard
.Mode mode
;
65 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
67 * @param pageName a {@link java.lang.String} object.
69 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
) {
71 this.setPageComplete(false);
72 setDataSource(dataSource
);
73 mode
= CdmDataSourceWizard
.Mode
.CREATE
;
77 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
79 * @param pageName a {@link java.lang.String} object.
81 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
, CdmDataSourceWizard
.Mode mode
) {
83 this.setPageComplete(false);
86 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
87 setDataSource(CdmDataSource
.NewInstance(dataSource
));
89 setDataSource(dataSource
);
95 public void createControl(Composite parent
) {
98 // Create top-level composite
99 parent
.setLayout(new GridLayout());
100 composite
= new Composite(parent
, SWT
.NONE
);
101 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false,2,5));
102 GridLayout formLayout
= new GridLayout();
103 formLayout
.numColumns
= 2;
104 composite
.setLayout(formLayout
);
108 // Create composite for data source name
109 Composite editDatasourceComposite
= new Composite(composite
, SWT
.NONE
);
110 GridData datasourceGridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, true,2,1);
111 editDatasourceComposite
.setLayoutData(datasourceGridData
);
112 GridLayout datasourceLayout
= new GridLayout();
113 datasourceLayout
.numColumns
= 2;
114 editDatasourceComposite
.setLayout(datasourceLayout
);
116 // Create label and input for dataSource name
117 Label datasourceNameLabel
= new Label(editDatasourceComposite
, SWT
.NONE
);
118 datasourceNameLabel
.setText("Datasource Name:");
119 text_dataSourceName
= new Text(editDatasourceComposite
, SWT
.BORDER
);
120 text_dataSourceName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
122 if(getDataSource() == null) {
123 editDatasourceComposite
.setVisible(false);
126 // create a database specific form
127 createDatabaseForm();
129 // create the authentication input fields
130 createAuthenticationForm();
132 // Create composite for buttons
133 Composite buttonComposite
= new Composite(composite
, SWT
.NONE
);
134 buttonComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
135 GridLayout buttonLayout
= new GridLayout();
136 buttonLayout
.numColumns
= 1;
137 buttonComposite
.setLayout(buttonLayout
);
139 // Create test connection button
140 Button testButton
= new Button(buttonComposite
, SWT
.NONE
);
141 testButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
142 testButton
.setText("Test connection");
144 // Test connection when button is pressed
145 testButton
.addSelectionListener(new SelectionAdapter() {
147 public void widgetSelected(SelectionEvent e
) {
148 testDbConfiguration();
152 setControl(composite
);
159 * <p>createAuthenticationForm</p>
161 protected void createAuthenticationForm(){
162 // Create group composite for authentication data
163 authenticationGroup
= new Group(composite
, SWT
.NONE
);
164 authenticationGroup
.setText("Authentication");
165 authenticationGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 3));
166 GridLayout authenticationLayout
= new GridLayout();
167 authenticationLayout
.numColumns
= 2;
168 authenticationGroup
.setLayout(authenticationLayout
);
170 // Create database name label
171 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
172 databaseNameLabel
.setText("Database Name:");
174 // Create database name input
175 text_databaseName
= new Text(authenticationGroup
, SWT
.BORDER
);
176 text_databaseName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
178 // Create username label
179 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
180 usernameLabel
.setText("User Name:");
182 // Create username input
183 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
184 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
186 // Create password label
187 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
188 passwordLabel
.setText("Password:");
190 // Create password input
191 text_password
= new Text(authenticationGroup
, SWT
.BORDER
| SWT
.PASSWORD
);
192 text_password
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
197 * Initialize text fields
201 if(getDataSource() != null){
202 text_dataSourceName
.setText(getDataSource().getName());
203 text_databaseName
.setText(getDataSource().getDatabase());
204 text_username
.setText(getDataSource().getUsername());
205 text_password
.setText(getDataSource().getPassword());
207 // add listeners after setting text to avoid the modify event being called
208 // for the initial value
211 // in the case of cloning we use the same datasource info
212 // except for the name
213 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
214 getDataSource().setName("");
215 text_dataSourceName
.setText("");
217 name
= text_dataSourceName
.getText();
222 private void addListeners() {
223 text_dataSourceName
.addModifyListener(this);
224 text_databaseName
.addModifyListener(this);
225 text_username
.addModifyListener(this);
226 text_password
.addModifyListener(this);
229 private void removeListeners() {
230 text_dataSourceName
.removeModifyListener(this);
231 text_databaseName
.removeModifyListener(this);
232 text_username
.removeModifyListener(this);
233 text_password
.removeModifyListener(this);
236 public void testDbConfiguration(){
237 testDbConfiguration(false);
241 * Tries to open a connection to the given dataSource. Generates a message on either
244 public void testDbConfiguration(boolean ignoreSuccess
){
248 getDataSource().testConnection();
250 MessageDialog
.openConfirm(parent
.getShell(), "Connection Test successful", "Test successful!");
252 } catch(SQLException e
){
253 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
254 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
255 + System
.getProperty("line.separator")
256 + "Please double check the connection fields");
257 } catch (ClassNotFoundException e
) {
258 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
259 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
260 + System
.getProperty("line.separator")
261 + "Please double check the connection fields");
266 * Form implementation for the specific database
268 public abstract void createDatabaseForm();
271 * <p>updateLocation</p>
273 public abstract void updateLocation();
276 * <p>updateDataSource</p>
278 public abstract void updateDataSource();
281 * <p>checkPageComplete</p>
283 public void checkPageComplete(){
284 boolean complete
= false;
285 if(mode
== CdmDataSourceWizard
.Mode
.CREATE
) {
286 complete
= database
.length() != 0;
288 complete
= name
.length() != 0 && database
.length() != 0;
290 this.setPageComplete(complete
);
296 * updates the current datasource with form values
298 public void updateAuthentication(){
299 database
= text_databaseName
.getText();
300 username
= text_username
.getText();
301 password
= text_password
.getText();
306 public IWizardPage
getNextPage() {
311 * <p>Setter for the field <code>dataSource</code>.</p>
313 * @param dataSource the dataSource to set
315 public void setDataSource(ICdmDataSource dataSource
) {
316 this.dataSource
= dataSource
;
320 * <p>Getter for the field <code>dataSource</code>.</p>
322 * @return the dataSource
324 public ICdmDataSource
getUpdatedDataSource() {
329 protected ICdmDataSource
getDataSource() {
334 * <p>getDataSourceName</p>
336 * @return a {@link java.lang.String} object.
338 public String
getDataSourceName() {
344 public void modifyText(ModifyEvent e
) {
346 name
= text_dataSourceName
.getText();
347 database
= text_databaseName
.getText();
351 if(name
.length() == 0){
353 setErrorMessage("DataSource name must not be empty.");
354 this.setPageComplete(false);
356 } else if ((!dataSource
.getName().equals(name
)) && (CdmDataSourceRepository
.getDataSource(name
) != null)) {
358 setErrorMessage("DataSource with the same name already exists");
359 this.setPageComplete(false);
364 if(name
.length() == 0){
366 setErrorMessage("DataSource name must not be empty.");
367 this.setPageComplete(false);
369 } else if (CdmDataSourceRepository
.getDataSource(name
) != null) {
371 setErrorMessage("DataSource with the same name already exists");
372 this.setPageComplete(false);
380 if(database
.length() == 0){
381 setErrorMessage("Database name must not be empty.");
382 this.setPageComplete(false);
385 setErrorMessage(null);
390 private void updateAndCheck() {
392 updateAuthentication();
396 * <p>modifyTextWithoutTriggeringListeners</p>
398 * @param text a {@link org.eclipse.swt.widgets.Text} object.
399 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
400 * @param string a {@link java.lang.String} object.
402 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
403 text
.removeModifyListener(listener
);
404 text
.setText(string
);
405 text
.addModifyListener(listener
);