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
.CdmPersistentDataSource
;
32 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
33 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
34 import eu
.etaxonomy
.taxeditor
.model
.NomenclaturalCodeHelper
;
35 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
45 public abstract class CdmDataSourceCredentialsWizardPage
extends WizardPage
implements ModifyListener
{
47 private ICdmDataSource dataSource
;
49 protected Text text_password
;
50 protected Text text_databaseName
;
51 protected Text text_username
;
53 protected Group authenticationGroup
;
54 protected Group locationGroup
;
55 protected Group nomenclaturalCodeGroup
;
57 protected Composite composite
;
59 protected Composite parent
;
61 protected String database
;
62 protected String username
;
63 protected String password
;
65 protected NomenclaturalCode nomenclaturalCode
;
68 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
70 * @param pageName a {@link java.lang.String} object.
72 protected CdmDataSourceCredentialsWizardPage(String pageName
) {
74 this.setPageComplete(false);
78 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
81 public void createControl(Composite parent
) {
84 // Create top-level composite
86 composite
= new Composite(parent
, SWT
.NONE
);
87 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 2, 1));
88 GridLayout formLayout
= new GridLayout();
89 formLayout
.numColumns
= 2;
90 composite
.setLayout(formLayout
);
92 // create a database specific form
95 // create the authentication input fields
96 createAuthenticationForm();
98 // create nomenclatural code combo
99 createNomenclaturalCodeForm();
101 // Create composite for buttons
102 Composite buttonComposite
= new Composite(composite
, SWT
.NONE
);
103 buttonComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
104 GridLayout buttonLayout
= new GridLayout();
105 buttonLayout
.numColumns
= 2;
106 buttonComposite
.setLayout(buttonLayout
);
108 // Create test connection button
109 Button testButton
= new Button(buttonComposite
, SWT
.NONE
);
110 testButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
111 testButton
.setText("Test connection");
113 // Test connection when button is pressed
114 testButton
.addSelectionListener(new SelectionAdapter() {
116 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
119 public void widgetSelected(SelectionEvent e
) {
120 testDbConfiguration();
124 setControl(composite
);
130 * <p>createAuthenticationForm</p>
132 protected void createAuthenticationForm(){
133 // Create group composite for authentication data
134 authenticationGroup
= new Group(composite
, SWT
.NONE
);
135 authenticationGroup
.setText("Authentication");
136 authenticationGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 1));
137 GridLayout authenticationLayout
= new GridLayout();
138 authenticationLayout
.numColumns
= 2;
139 authenticationGroup
.setLayout(authenticationLayout
);
141 // Create database name label
142 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
143 databaseNameLabel
.setText("Database Name:");
145 // Create database name input
146 text_databaseName
= new Text(authenticationGroup
, SWT
.BORDER
);
147 text_databaseName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
148 text_databaseName
.addModifyListener(this);
150 // Create username label
151 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
152 usernameLabel
.setText("User Name:");
154 // Create username input
155 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
156 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
157 text_username
.addModifyListener(this);
159 // Create password label
160 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
161 passwordLabel
.setText("Password:");
163 // Create password input
164 text_password
= new Text(authenticationGroup
, SWT
.BORDER
| SWT
.PASSWORD
);
165 text_password
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
166 text_password
.addModifyListener(this);
170 * Create a radio button group to select a nomenclatural code from
172 private void createNomenclaturalCodeForm() {
173 nomenclaturalCodeGroup
= new Group(composite
, SWT
.NONE
);
174 nomenclaturalCodeGroup
.setLayout(new GridLayout());
176 nomenclaturalCode
= dataSource
!= null ? dataSource
.getNomenclaturalCode() : PreferencesUtil
.getPreferredNomenclaturalCode();
178 for (final NomenclaturalCode code
: NomenclaturalCodeHelper
.getSupportedCodes()) {
179 Button button
= new Button(nomenclaturalCodeGroup
, SWT
.RADIO
);
180 button
.setText(NomenclaturalCodeHelper
.getDescription(code
));
181 button
.setData(code
);
182 if (nomenclaturalCode
!= null) {
183 button
.setSelection(nomenclaturalCode
.equals(code
));
185 button
.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent e
) {
187 nomenclaturalCode
= (NomenclaturalCode
) e
.widget
.getData();
197 * Tries to open a connection to the given dataSource. Generates a message on either
200 public void testDbConfiguration(){
202 getDataSource().testConnection();
203 MessageDialog
.openConfirm(parent
.getShell(), "Test successful", "Test successful!");
204 } catch(SQLException e
){
205 MessageDialog
.openWarning(parent
.getShell(), "Test unsuccessful", "Reason: " + e
.getMessage());
206 throw new RuntimeException(e
);
207 } catch (ClassNotFoundException e
) {
208 MessageDialog
.openWarning(parent
.getShell(), "Test unsuccessful", "Reason: " + e
.getMessage());
209 throw new RuntimeException(e
);
214 * Form implementation for the specific database
216 public abstract void createDatabaseForm();
219 * <p>updateLocation</p>
221 public abstract void updateLocation();
224 * <p>updateDataSource</p>
226 public abstract void updateDataSource();
229 * <p>checkPageComplete</p>
231 public void checkPageComplete(){
232 boolean complete
= database
.length() != 0;
233 // complete &= username.length() != 0;
235 this.setPageComplete(complete
);
239 * Initialize text fields
242 if(getDataSource() != null){
243 modifyTextWithoutTriggeringListeners(text_databaseName
, this, getDataSource().getDatabase());
244 modifyTextWithoutTriggeringListeners(text_username
, this, getDataSource().getUsername());
245 modifyTextWithoutTriggeringListeners(text_password
, this, getDataSource().getPassword());
250 * updates the current datasource with form values
252 public void updateAuthentication(){
254 database
= text_databaseName
.getText();
255 username
= text_username
.getText();
256 password
= text_password
.getText();
260 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
264 public IWizardPage
getNextPage() {
269 * <p>Setter for the field <code>dataSource</code>.</p>
271 * @param dataSource the dataSource to set
273 public void setDataSource(ICdmDataSource dataSource
) {
274 this.dataSource
= dataSource
;
278 * <p>Getter for the field <code>dataSource</code>.</p>
280 * @return the dataSource
282 public ICdmDataSource
getDataSource() {
287 * <p>getDataSourceName</p>
289 * @return a {@link java.lang.String} object.
291 public String
getDataSourceName() {
292 if(dataSource
instanceof CdmPersistentDataSource
){
293 return ((CdmPersistentDataSource
) dataSource
).getName();
299 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
302 public void modifyText(ModifyEvent e
) {
304 updateAuthentication();
310 * <p>modifyTextWithoutTriggeringListeners</p>
312 * @param text a {@link org.eclipse.swt.widgets.Text} object.
313 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
314 * @param string a {@link java.lang.String} object.
316 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
317 text
.removeModifyListener(listener
);
318 text
.setText(string
);
319 text
.addModifyListener(listener
);