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 org
.eclipse
.jface
.dialogs
.MessageDialog
;
14 import org
.eclipse
.jface
.wizard
.IWizardPage
;
15 import org
.eclipse
.jface
.wizard
.WizardPage
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.events
.ModifyEvent
;
18 import org
.eclipse
.swt
.events
.ModifyListener
;
19 import org
.eclipse
.swt
.events
.SelectionAdapter
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Group
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Text
;
29 import eu
.etaxonomy
.cdm
.database
.CdmPersistentDataSource
;
30 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
31 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
32 import eu
.etaxonomy
.taxeditor
.model
.NomenclaturalCodeHelper
;
33 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 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_username
;
49 protected Group authenticationGroup
;
50 protected Group locationGroup
;
51 protected Group nomenclaturalCodeGroup
;
53 protected Composite composite
;
55 protected Composite parent
;
57 protected String database
;
58 protected String username
;
59 protected String password
;
61 protected NomenclaturalCode nomenclaturalCode
;
66 protected CdmDataSourceCredentialsWizardPage(String pageName
) {
68 this.setPageComplete(false);
72 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
74 public void createControl(Composite parent
) {
77 // Create top-level composite
79 composite
= new Composite(parent
, SWT
.NONE
);
80 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 2, 1));
81 GridLayout formLayout
= new GridLayout();
82 formLayout
.numColumns
= 2;
83 composite
.setLayout(formLayout
);
85 // create a database specific form
88 // create the authentication input fields
89 createAuthenticationForm();
91 // create nomenclatural code combo
92 createNomenclaturalCodeForm();
94 // Create composite for buttons
95 Composite buttonComposite
= new Composite(composite
, SWT
.NONE
);
96 buttonComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
97 GridLayout buttonLayout
= new GridLayout();
98 buttonLayout
.numColumns
= 2;
99 buttonComposite
.setLayout(buttonLayout
);
101 // Create test connection button
102 Button testButton
= new Button(buttonComposite
, SWT
.NONE
);
103 testButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
104 testButton
.setText("Test connection");
106 // Test connection when button is pressed
107 testButton
.addSelectionListener(new SelectionAdapter() {
109 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
112 public void widgetSelected(SelectionEvent e
) {
113 testDbConfiguration();
117 setControl(composite
);
122 protected void createAuthenticationForm(){
123 // Create group composite for authentication data
124 authenticationGroup
= new Group(composite
, SWT
.NONE
);
125 authenticationGroup
.setText("Authentication");
126 authenticationGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 1));
127 GridLayout authenticationLayout
= new GridLayout();
128 authenticationLayout
.numColumns
= 2;
129 authenticationGroup
.setLayout(authenticationLayout
);
131 // Create database name label
132 Label databaseNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
133 databaseNameLabel
.setText("Database Name:");
135 // Create database name input
136 text_databaseName
= new Text(authenticationGroup
, SWT
.BORDER
);
137 text_databaseName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
138 text_databaseName
.addModifyListener(this);
140 // Create username label
141 Label usernameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
142 usernameLabel
.setText("User Name:");
144 // Create username input
145 text_username
= new Text(authenticationGroup
, SWT
.BORDER
);
146 text_username
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
147 text_username
.addModifyListener(this);
149 // Create password label
150 Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
151 passwordLabel
.setText("Password:");
153 // Create password input
154 text_password
= new Text(authenticationGroup
, SWT
.BORDER
);
155 text_password
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
156 text_password
.addModifyListener(this);
160 * Create a radio button group to select a nomenclatural code from
162 private void createNomenclaturalCodeForm() {
163 nomenclaturalCodeGroup
= new Group(composite
, SWT
.NONE
);
164 nomenclaturalCodeGroup
.setLayout(new GridLayout());
166 nomenclaturalCode
= dataSource
!= null ? dataSource
.getNomenclaturalCode() : PreferencesUtil
.getPreferredNomenclaturalCode();
168 for (final NomenclaturalCode code
: NomenclaturalCodeHelper
.getSupportedCodes()) {
169 Button button
= new Button(nomenclaturalCodeGroup
, SWT
.RADIO
);
170 button
.setText(NomenclaturalCodeHelper
.getDescription(code
));
171 button
.setData(code
);
172 if (nomenclaturalCode
!= null) {
173 button
.setSelection(nomenclaturalCode
.equals(code
));
175 button
.addSelectionListener(new SelectionAdapter() {
176 public void widgetSelected(SelectionEvent e
) {
177 nomenclaturalCode
= (NomenclaturalCode
) e
.widget
.getData();
187 * Tries to open a connection to the given dataSource. Generates a message on either
192 public void testDbConfiguration(){
194 getDataSource().testConnection();
195 MessageDialog
.openConfirm(parent
.getShell(), "Test successful", "Test successful!");
196 } catch(Exception e
){
197 MessageDialog
.openWarning(parent
.getShell(), "Test unsuccessful", "Test unsuccessful! \n\nSee error.log for details.");
198 throw new RuntimeException(e
);
203 * Form implementation for the specific database
205 public abstract void createDatabaseForm();
207 public abstract void updateLocation();
209 public abstract void updateDataSource();
211 public void checkPageComplete(){
212 boolean complete
= database
.length() != 0;
213 // complete &= username.length() != 0;
215 this.setPageComplete(complete
);
219 * Initialize text fields
222 if(getDataSource() != null){
223 modifyTextWithoutTriggeringListeners(text_databaseName
, this, getDataSource().getDatabase());
224 modifyTextWithoutTriggeringListeners(text_username
, this, getDataSource().getUsername());
225 modifyTextWithoutTriggeringListeners(text_password
, this, getDataSource().getPassword());
230 * updates the current datasource with form values
232 public void updateAuthentication(){
234 database
= text_databaseName
.getText();
235 username
= text_username
.getText();
236 password
= text_password
.getText();
240 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
243 public IWizardPage
getNextPage() {
248 * @param dataSource the dataSource to set
250 public void setDataSource(ICdmDataSource dataSource
) {
251 this.dataSource
= dataSource
;
255 * @return the dataSource
257 public ICdmDataSource
getDataSource() {
264 public String
getDataSourceName() {
265 if(dataSource
instanceof CdmPersistentDataSource
){
266 return ((CdmPersistentDataSource
) dataSource
).getName();
272 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
274 public void modifyText(ModifyEvent e
) {
276 updateAuthentication();
287 protected void modifyTextWithoutTriggeringListeners(Text text
, ModifyListener listener
, String string
){
288 text
.removeModifyListener(listener
);
289 text
.setText(string
);
290 text
.addModifyListener(listener
);