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
.local
.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
.local
.datasource
.common
.CdmDataSourceRepository
;
35 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
41 public abstract class CdmDataSourceCredentialsWizardPage
extends WizardPage
implements ModifyListener
{
42 private ICdmDataSource dataSource
;
44 protected Text text_password
;
45 protected Text text_databaseName
;
46 protected Text text_dataSourceName
;
47 protected Text text_username
;
49 protected Group authenticationGroup
;
50 protected Group locationGroup
;
52 protected Composite composite
;
54 protected Composite parent
;
56 protected String name
;
57 protected String database
;
58 protected String username
;
59 protected String password
;
61 CdmDataSourceWizard
.Mode mode
;
64 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
66 * @param pageName a {@link java.lang.String} object.
68 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
) {
70 this.setPageComplete(false);
71 setDataSource(dataSource
);
72 mode
= CdmDataSourceWizard
.Mode
.CREATE
;
76 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
78 * @param pageName a {@link java.lang.String} object.
80 protected CdmDataSourceCredentialsWizardPage(String pageName
, ICdmDataSource dataSource
, CdmDataSourceWizard
.Mode mode
) {
82 this.setPageComplete(false);
85 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
86 setDataSource(CdmDataSource
.NewInstance(dataSource
));
88 setDataSource(dataSource
);
94 public void createControl(Composite parent
) {
97 // Create top-level composite
98 parent
.setLayout(new GridLayout());
99 composite
= new Composite(parent
, SWT
.NONE
);
100 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false,2,5));
101 GridLayout formLayout
= new GridLayout();
102 formLayout
.numColumns
= 2;
103 composite
.setLayout(formLayout
);
107 // Create composite for data source name
108 Composite editDatasourceComposite
= new Composite(composite
, SWT
.NONE
);
109 GridData datasourceGridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, true,2,1);
110 editDatasourceComposite
.setLayoutData(datasourceGridData
);
111 GridLayout datasourceLayout
= new GridLayout();
112 datasourceLayout
.numColumns
= 2;
113 editDatasourceComposite
.setLayout(datasourceLayout
);
115 // Create label and input for dataSource name
116 Label datasourceNameLabel
= new Label(editDatasourceComposite
, SWT
.NONE
);
117 datasourceNameLabel
.setText("Datasource Name:");
118 text_dataSourceName
= new Text(editDatasourceComposite
, SWT
.BORDER
);
119 text_dataSourceName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
121 if(getDataSource() == null) {
122 editDatasourceComposite
.setVisible(false);
125 // create a database specific form
126 createDatabaseForm();
128 // create the authentication input fields
129 createAuthenticationForm();
131 // Create composite for buttons
132 Composite buttonComposite
= new Composite(composite
, SWT
.NONE
);
133 buttonComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
134 GridLayout buttonLayout
= new GridLayout();
135 buttonLayout
.numColumns
= 1;
136 buttonComposite
.setLayout(buttonLayout
);
138 // Create test connection button
139 Button testButton
= new Button(buttonComposite
, SWT
.NONE
);
140 testButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
141 testButton
.setText("Test connection");
143 // Test connection when button is pressed
144 testButton
.addSelectionListener(new SelectionAdapter() {
146 public void widgetSelected(SelectionEvent e
) {
147 testDbConfiguration();
151 setControl(composite
);
158 * <p>createAuthenticationForm</p>
160 protected void createAuthenticationForm(){
161 // Create group composite for authentication data
162 authenticationGroup
= new Group(composite
, SWT
.NONE
);
163 authenticationGroup
.setText("Authentication");
164 authenticationGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 3));
165 GridLayout authenticationLayout
= new GridLayout();
166 authenticationLayout
.numColumns
= 2;
167 authenticationGroup
.setLayout(authenticationLayout
);
169 // Create database name label
170 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
171 databaseNameLabel
.setText("Database Name:");
173 // Create database name input
174 text_databaseName
= new Text(authenticationGroup
, SWT
.BORDER
);
175 text_databaseName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
177 // Create username label
178 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
179 usernameLabel
.setText("User Name:");
181 // Create username input
182 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
183 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
185 // Create password label
186 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
187 passwordLabel
.setText("Password:");
189 // Create password input
190 text_password
= new Text(authenticationGroup
, SWT
.BORDER
| SWT
.PASSWORD
);
191 text_password
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
196 * Initialize text fields
200 if(getDataSource() != null){
201 text_dataSourceName
.setText(getDataSource().getName());
202 text_databaseName
.setText(getDataSource().getDatabase());
203 text_username
.setText(getDataSource().getUsername());
204 text_password
.setText(getDataSource().getPassword());
206 // add listeners after setting text to avoid the modify event being called
207 // for the initial value
210 // in the case of cloning we use the same datasource info
211 // except for the name
212 if(mode
== CdmDataSourceWizard
.Mode
.CLONE
) {
213 getDataSource().setName("");
214 text_dataSourceName
.setText("");
216 name
= text_dataSourceName
.getText();
221 private void addListeners() {
222 text_dataSourceName
.addModifyListener(this);
223 text_databaseName
.addModifyListener(this);
224 text_username
.addModifyListener(this);
225 text_password
.addModifyListener(this);
228 private void removeListeners() {
229 text_dataSourceName
.removeModifyListener(this);
230 text_databaseName
.removeModifyListener(this);
231 text_username
.removeModifyListener(this);
232 text_password
.removeModifyListener(this);
235 public void testDbConfiguration(){
236 testDbConfiguration(false);
240 * Tries to open a connection to the given dataSource. Generates a message on either
243 public void testDbConfiguration(boolean ignoreSuccess
){
247 getDataSource().testConnection();
249 MessageDialog
.openConfirm(parent
.getShell(), "Connection Test successful", "Test successful!");
251 } catch(SQLException e
){
252 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
253 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
254 + System
.getProperty("line.separator")
255 + "Please double check the connection fields");
256 } catch (ClassNotFoundException e
) {
257 MessageDialog
.openWarning(parent
.getShell(), "Connection Test unsuccessful", "Reason: " + e
.getMessage()
258 + System
.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
259 + System
.getProperty("line.separator")
260 + "Please double check the connection fields");
265 * Form implementation for the specific database
267 public abstract void createDatabaseForm();
270 * <p>updateLocation</p>
272 public abstract void updateLocation();
275 * <p>updateDataSource</p>
277 public abstract void updateDataSource();
280 * <p>checkPageComplete</p>
282 public void checkPageComplete(){
283 boolean complete
= false;
284 if(mode
== CdmDataSourceWizard
.Mode
.CREATE
) {
285 complete
= database
.length() != 0;
287 complete
= name
.length() != 0 && database
.length() != 0;
289 this.setPageComplete(complete
);
295 * updates the current datasource with form values
297 public void updateAuthentication(){
298 database
= text_databaseName
.getText();
299 username
= text_username
.getText();
300 password
= text_password
.getText();
305 public IWizardPage
getNextPage() {
310 * <p>Setter for the field <code>dataSource</code>.</p>
312 * @param dataSource the dataSource to set
314 public void setDataSource(ICdmDataSource dataSource
) {
315 this.dataSource
= dataSource
;
319 * <p>Getter for the field <code>dataSource</code>.</p>
321 * @return the dataSource
323 public ICdmDataSource
getUpdatedDataSource() {
328 protected ICdmDataSource
getDataSource() {
333 * <p>getDataSourceName</p>
335 * @return a {@link java.lang.String} object.
337 public String
getDataSourceName() {
343 public void modifyText(ModifyEvent e
) {
345 name
= text_dataSourceName
.getText();
346 database
= text_databaseName
.getText();
350 if(name
.length() == 0){
352 setErrorMessage("DataSource name must not be empty.");
353 this.setPageComplete(false);
355 } else if ((!dataSource
.getName().equals(name
)) && (CdmDataSourceRepository
.getDataSource(name
) != null)) {
357 setErrorMessage("DataSource with the same name already exists");
358 this.setPageComplete(false);
363 if(name
.length() == 0){
365 setErrorMessage("DataSource name must not be empty.");
366 this.setPageComplete(false);
368 } else if (CdmDataSourceRepository
.getDataSource(name
) != null) {
370 setErrorMessage("DataSource with the same name already exists");
371 this.setPageComplete(false);
379 if(database
.length() == 0){
380 setErrorMessage("Database name must not be empty.");
381 this.setPageComplete(false);
384 setErrorMessage(null);
389 private void updateAndCheck() {
391 updateAuthentication();
395 * <p>modifyTextWithoutTriggeringListeners</p>
397 * @param text a {@link org.eclipse.swt.widgets.Text} object.
398 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
399 * @param string a {@link java.lang.String} object.
401 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
402 text
.removeModifyListener(listener
);
403 text
.setText(string
);
404 text
.addModifyListener(listener
);