fixes #1358
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / datasource / wizard / CdmDataSourceCredentialsWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.datasource.wizard;
12
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;
28
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;
34
35
36 /**
37 * @author n.hoffmann
38 * @created 19.05.2009
39 * @version 1.0
40 */
41 public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener{
42
43 private ICdmDataSource dataSource;
44
45 protected Text text_password;
46 protected Text text_databaseName;
47 protected Text text_username;
48
49 protected Group authenticationGroup;
50 protected Group locationGroup;
51 protected Group nomenclaturalCodeGroup;
52
53 protected Composite composite;
54
55 protected Composite parent;
56
57 protected String database;
58 protected String username;
59 protected String password;
60
61 protected NomenclaturalCode nomenclaturalCode;
62
63 /**
64 * @param pageName
65 */
66 protected CdmDataSourceCredentialsWizardPage(String pageName) {
67 super(pageName);
68 this.setPageComplete(false);
69 }
70
71 /* (non-Javadoc)
72 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
73 */
74 public void createControl(Composite parent) {
75 this.parent = parent;
76
77 // Create top-level composite
78
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);
84
85 // create a database specific form
86 createDatabaseForm();
87
88 // create the authentication input fields
89 createAuthenticationForm();
90
91 // create nomenclatural code combo
92 createNomenclaturalCodeForm();
93
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);
100
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");
105
106 // Test connection when button is pressed
107 testButton.addSelectionListener(new SelectionAdapter() {
108 /* (non-Javadoc)
109 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
110 */
111 @Override
112 public void widgetSelected(SelectionEvent e) {
113 testDbConfiguration();
114 }
115 });
116
117 setControl(composite);
118
119 init();
120 }
121
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);
130
131 // Create database name label
132 Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
133 databaseNameLabel.setText("Database Name:");
134
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);
139
140 // Create username label
141 Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
142 usernameLabel.setText("User Name:");
143
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);
148
149 // Create password label
150 Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
151 passwordLabel.setText("Password:");
152
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);
157 }
158
159 /**
160 * Create a radio button group to select a nomenclatural code from
161 */
162 private void createNomenclaturalCodeForm() {
163 nomenclaturalCodeGroup = new Group(composite , SWT.NONE);
164 nomenclaturalCodeGroup.setLayout(new GridLayout());
165
166 nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode();
167
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));
174 }
175 button.addSelectionListener(new SelectionAdapter() {
176 public void widgetSelected(SelectionEvent e) {
177 nomenclaturalCode = (NomenclaturalCode) e.widget.getData();
178 updateDataSource();
179 }
180 });
181 }
182 }
183
184
185
186 /**
187 * Tries to open a connection to the given dataSource. Generates a message on either
188 * failure or success
189 *
190 * @param dataSource
191 */
192 public void testDbConfiguration(){
193 try{
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);
199 }
200 }
201
202 /**
203 * Form implementation for the specific database
204 */
205 public abstract void createDatabaseForm();
206
207 public abstract void updateLocation();
208
209 public abstract void updateDataSource();
210
211 public void checkPageComplete(){
212 boolean complete = database.length() != 0;
213 // complete &= username.length() != 0;
214
215 this.setPageComplete(complete);
216 }
217
218 /**
219 * Initialize text fields
220 */
221 public void init() {
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());
226 }
227 }
228
229 /**
230 * updates the current datasource with form values
231 */
232 public void updateAuthentication(){
233
234 database = text_databaseName.getText();
235 username = text_username.getText();
236 password = text_password.getText();
237 }
238
239 /* (non-Javadoc)
240 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
241 */
242 @Override
243 public IWizardPage getNextPage() {
244 return null;
245 }
246
247 /**
248 * @param dataSource the dataSource to set
249 */
250 public void setDataSource(ICdmDataSource dataSource) {
251 this.dataSource = dataSource;
252 }
253
254 /**
255 * @return the dataSource
256 */
257 public ICdmDataSource getDataSource() {
258 return dataSource;
259 }
260
261 /**
262 * @return
263 */
264 public String getDataSourceName() {
265 if(dataSource instanceof CdmPersistentDataSource){
266 return ((CdmPersistentDataSource) dataSource).getName();
267 }
268 return null;
269 }
270
271 /* (non-Javadoc)
272 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
273 */
274 public void modifyText(ModifyEvent e) {
275 updateLocation();
276 updateAuthentication();
277 updateDataSource();
278 checkPageComplete();
279 }
280
281 /**
282 *
283 * @param text
284 * @param listener
285 * @param string
286 */
287 protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
288 text.removeModifyListener(listener);
289 text.setText(string);
290 text.addModifyListener(listener);
291 }
292 }