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
.ICdmDataSource
;
33 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
37 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
43 public abstract class CdmDataSourceCredentialsWizardPage
extends WizardPage
implements ModifyListener
{
44 private ICdmDataSource dataSource
;
46 protected Text text_password
;
47 protected Text text_databaseName
;
48 protected Text text_dataSourceName
;
49 protected Text text_username
;
51 protected Group authenticationGroup
;
52 protected Group locationGroup
;
54 protected Composite composite
;
56 protected Composite parent
;
58 protected String name
;
59 protected String database
;
60 protected String username
;
61 protected String password
;
63 CdmDataSourceWizard
.Mode mode
;
66 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
68 * @param pageName a {@link java.lang.String} object.
70 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
) {
72 this.setPageComplete(false);
73 setDataSource(dataSource
);
74 mode
= CdmDataSourceWizard
.Mode
.CREATE
;
78 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
80 * @param pageName a {@link java.lang.String} object.
82 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
, CdmDataSourceWizard
.Mode mode
) {
84 this.setPageComplete(false);
87 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
88 setDataSource(CdmDataSource
.NewInstance(dataSource
));
90 setDataSource(dataSource
);
96 public void createControl(Composite parent
) {
99 // Create top-level composite
100 parent
.setLayout(new GridLayout());
101 composite
= new Composite(parent
, SWT
.NONE
);
102 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false,2,5));
103 GridLayout formLayout
= new GridLayout();
104 formLayout
.numColumns
= 2;
105 composite
.setLayout(formLayout
);
109 // Create composite for data source name
110 Composite editDatasourceComposite
= new Composite(composite
, SWT
.NONE
);
111 GridData datasourceGridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, true,2,1);
112 editDatasourceComposite
.setLayoutData(datasourceGridData
);
113 GridLayout datasourceLayout
= new GridLayout();
114 datasourceLayout
.numColumns
= 2;
115 editDatasourceComposite
.setLayout(datasourceLayout
);
117 // Create label and input for dataSource name
118 Label datasourceNameLabel
= new Label(editDatasourceComposite
, SWT
.NONE
);
119 datasourceNameLabel
.setText("Datasource Name:");
120 text_dataSourceName
= new Text(editDatasourceComposite
, SWT
.BORDER
);
121 text_dataSourceName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
123 if(getDataSource() == null) {
124 editDatasourceComposite
.setVisible(false);
127 // create a database specific form
128 createDatabaseForm();
130 // create the authentication input fields
131 createAuthenticationForm();
133 // Create composite for buttons
134 Composite buttonComposite
= new Composite(composite
, SWT
.NONE
);
135 buttonComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
136 GridLayout buttonLayout
= new GridLayout();
137 buttonLayout
.numColumns
= 1;
138 buttonComposite
.setLayout(buttonLayout
);
140 // Create test connection button
141 Button testButton
= new Button(buttonComposite
, SWT
.NONE
);
142 testButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
143 testButton
.setText("Test connection");
145 // Test connection when button is pressed
146 testButton
.addSelectionListener(new SelectionAdapter() {
148 public void widgetSelected(SelectionEvent e
) {
149 testDbConfiguration();
153 setControl(composite
);
160 * <p>createAuthenticationForm</p>
162 protected void createAuthenticationForm(){
163 // Create group composite for authentication data
164 authenticationGroup
= new Group(composite
, SWT
.NONE
);
165 authenticationGroup
.setText("Authentication");
166 authenticationGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 3));
167 GridLayout authenticationLayout
= new GridLayout();
168 authenticationLayout
.numColumns
= 2;
169 authenticationGroup
.setLayout(authenticationLayout
);
171 // Create database name label
172 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
173 databaseNameLabel
.setText("Database Name:");
175 // Create database name input
176 text_databaseName
= new Text(authenticationGroup
, SWT
.BORDER
);
177 text_databaseName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
179 // Create username label
180 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
181 usernameLabel
.setText("User Name:");
183 // Create username input
184 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
185 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
187 // Create password label
188 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
189 passwordLabel
.setText("Password:");
191 // Create password input
192 text_password
= new Text(authenticationGroup
, SWT
.BORDER
| SWT
.PASSWORD
);
193 text_password
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
198 * Initialize text fields
202 if(getDataSource() != null){
203 text_dataSourceName
.setText(getDataSource().getName());
204 text_databaseName
.setText(getDataSource().getDatabase());
205 text_username
.setText(getDataSource().getUsername());
206 text_password
.setText(getDataSource().getPassword());
208 // add listeners after setting text to avoid the modify event being called
209 // for the initial value
212 // in the case of cloning we use the same datasource info
213 // except for the name
214 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
215 getDataSource().setName("");
216 text_dataSourceName
.setText("");
218 name
= text_dataSourceName
.getText();
223 private void addListeners() {
224 text_dataSourceName
.addModifyListener(this);
225 text_databaseName
.addModifyListener(this);
226 text_username
.addModifyListener(this);
227 text_password
.addModifyListener(this);
230 private void removeListeners() {
231 text_dataSourceName
.removeModifyListener(this);
232 text_databaseName
.removeModifyListener(this);
233 text_username
.removeModifyListener(this);
234 text_password
.removeModifyListener(this);
237 public void testDbConfiguration(){
238 testDbConfiguration(false);
242 * Tries to open a connection to the given dataSource. Generates a message on either
245 public void testDbConfiguration(boolean ignoreSuccess
){
249 getDataSource().testConnection();
251 MessageDialog
.openConfirm(parent
.getShell(), "Connection Test successful", "Test successful!");
253 } catch(SQLException e
){
254 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
255 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
256 + System
.getProperty("line.separator")
257 + "Please double check the connection fields");
258 } catch (ClassNotFoundException e
) {
259 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
260 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
261 + System
.getProperty("line.separator")
262 + "Please double check the connection fields");
267 * Form implementation for the specific database
269 public abstract void createDatabaseForm();
272 * <p>updateLocation</p>
274 public abstract void updateLocation();
277 * <p>updateDataSource</p>
279 public abstract void updateDataSource();
282 * <p>checkPageComplete</p>
284 public void checkPageComplete(){
285 boolean complete
= false;
286 if(mode
== CdmDataSourceWizard
.Mode
.CREATE
) {
287 complete
= database
.length() != 0;
289 complete
= name
.length() != 0 && database
.length() != 0;
291 this.setPageComplete(complete
);
297 * updates the current datasource with form values
299 public void updateAuthentication(){
300 database
= text_databaseName
.getText();
301 username
= text_username
.getText();
302 password
= text_password
.getText();
307 public IWizardPage
getNextPage() {
312 * <p>Setter for the field <code>dataSource</code>.</p>
314 * @param dataSource the dataSource to set
316 public void setDataSource(ICdmDataSource dataSource
) {
317 this.dataSource
= dataSource
;
321 * <p>Getter for the field <code>dataSource</code>.</p>
323 * @return the dataSource
325 public ICdmDataSource
getUpdatedDataSource() {
330 protected ICdmDataSource
getDataSource() {
335 * <p>getDataSourceName</p>
337 * @return a {@link java.lang.String} object.
339 public String
getDataSourceName() {
345 public void modifyText(ModifyEvent e
) {
347 name
= text_dataSourceName
.getText();
348 database
= text_databaseName
.getText();
352 if(name
.length() == 0){
354 setErrorMessage("DataSource name must not be empty.");
355 this.setPageComplete(false);
357 } else if ((!dataSource
.getName().equals(name
)) && (CdmDataSourceRepository
.getDataSource(name
) != null)) {
359 setErrorMessage("DataSource with the same name already exists");
360 this.setPageComplete(false);
365 if(name
.length() == 0){
367 setErrorMessage("DataSource name must not be empty.");
368 this.setPageComplete(false);
370 } else if (CdmDataSourceRepository
.getDataSource(name
) != null) {
372 setErrorMessage("DataSource with the same name already exists");
373 this.setPageComplete(false);
381 if(database
.length() == 0){
382 setErrorMessage("Database name must not be empty.");
383 this.setPageComplete(false);
386 setErrorMessage(null);
391 private void updateAndCheck() {
393 updateAuthentication();
397 * <p>modifyTextWithoutTriggeringListeners</p>
399 * @param text a {@link org.eclipse.swt.widgets.Text} object.
400 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
401 * @param string a {@link java.lang.String} object.
403 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
404 text
.removeModifyListener(listener
);
405 text
.setText(string
);
406 text
.addModifyListener(listener
);