ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / datasource / wizard / CdmDataSourceCredentialsWizardPage.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.datasource.wizard;
11
12 import java.sql.SQLException;
13
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;
29
30 import eu.etaxonomy.cdm.database.CdmDataSource;
31 import eu.etaxonomy.cdm.database.ICdmDataSource;
32 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
33
34
35 /**
36 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
37 *
38 * @author n.hoffmann
39 * @created 19.05.2009
40 * @version 1.0
41 */
42 public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener {
43 private ICdmDataSource dataSource;
44
45 protected Text text_password;
46 protected Text text_databaseName;
47 protected Text text_dataSourceName;
48 protected Text text_username;
49
50 protected Group authenticationGroup;
51 protected Group locationGroup;
52
53 protected Composite composite;
54
55 protected Composite parent;
56
57 protected String name;
58 protected String database;
59 protected String username;
60 protected String password;
61
62 CdmDataSourceWizard.Mode mode;
63
64 /**
65 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
66 *
67 * @param pageName a {@link java.lang.String} object.
68 */
69 protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) {
70 super(pageName);
71 this.setPageComplete(false);
72 setDataSource(dataSource);
73 mode = CdmDataSourceWizard.Mode.CREATE;
74 }
75
76 /**
77 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
78 *
79 * @param pageName a {@link java.lang.String} object.
80 */
81 protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
82 super(pageName);
83 this.setPageComplete(false);
84
85 this.mode = mode;
86 if(mode == CdmDataSourceWizard.Mode.CLONE) {
87 setDataSource(CdmDataSource.NewInstance(dataSource));
88 } else {
89 setDataSource(dataSource);
90 }
91 }
92
93 /** {@inheritDoc} */
94 @Override
95 public void createControl(Composite parent) {
96 this.parent = parent;
97
98 // Create top-level composite
99 parent.setLayout(new GridLayout());
100 composite = new Composite(parent, SWT.NONE);
101 composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5));
102 GridLayout formLayout = new GridLayout();
103 formLayout.numColumns = 2;
104 composite.setLayout(formLayout);
105
106
107
108 // Create composite for data source name
109 Composite editDatasourceComposite = new Composite(composite, SWT.NONE);
110 GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true,2,1);
111 editDatasourceComposite.setLayoutData(datasourceGridData);
112 GridLayout datasourceLayout = new GridLayout();
113 datasourceLayout.numColumns = 2;
114 editDatasourceComposite.setLayout(datasourceLayout);
115
116 // Create label and input for dataSource name
117 Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
118 datasourceNameLabel.setText("Datasource Name:");
119 text_dataSourceName = new Text(editDatasourceComposite, SWT.BORDER);
120 text_dataSourceName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
121
122 if(getDataSource() == null) {
123 editDatasourceComposite.setVisible(false);
124 }
125
126 // create a database specific form
127 createDatabaseForm();
128
129 // create the authentication input fields
130 createAuthenticationForm();
131
132 // Create composite for buttons
133 Composite buttonComposite = new Composite(composite, SWT.NONE);
134 buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
135 GridLayout buttonLayout = new GridLayout();
136 buttonLayout.numColumns = 1;
137 buttonComposite.setLayout(buttonLayout);
138
139 // Create test connection button
140 Button testButton = new Button(buttonComposite, SWT.NONE);
141 testButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
142 testButton.setText("Test connection");
143
144 // Test connection when button is pressed
145 testButton.addSelectionListener(new SelectionAdapter() {
146 @Override
147 public void widgetSelected(SelectionEvent e) {
148 testDbConfiguration();
149 }
150 });
151
152 setControl(composite);
153
154 init();
155
156 }
157
158 /**
159 * <p>createAuthenticationForm</p>
160 */
161 protected void createAuthenticationForm(){
162 // Create group composite for authentication data
163 authenticationGroup = new Group(composite, SWT.NONE);
164 authenticationGroup.setText("Authentication");
165 authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 3));
166 GridLayout authenticationLayout = new GridLayout();
167 authenticationLayout.numColumns = 2;
168 authenticationGroup.setLayout(authenticationLayout);
169
170 // Create database name label
171 Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
172 databaseNameLabel.setText("Database Name:");
173
174 // Create database name input
175 text_databaseName = new Text(authenticationGroup, SWT.BORDER);
176 text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
177
178 // Create username label
179 Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
180 usernameLabel.setText("User Name:");
181
182 // Create username input
183 text_username = new Text(authenticationGroup, SWT.BORDER);
184 text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
185
186 // Create password label
187 Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
188 passwordLabel.setText("Password:");
189
190 // Create password input
191 text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD);
192 text_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
193
194 }
195
196 /**
197 * Initialize text fields
198 */
199 public void init() {
200 removeListeners();
201 if(getDataSource() != null){
202 text_dataSourceName.setText(getDataSource().getName());
203 text_databaseName.setText(getDataSource().getDatabase());
204 text_username.setText(getDataSource().getUsername());
205 text_password.setText(getDataSource().getPassword());
206 }
207 // add listeners after setting text to avoid the modify event being called
208 // for the initial value
209 addListeners();
210
211 // in the case of cloning we use the same datasource info
212 // except for the name
213 if(mode == CdmDataSourceWizard.Mode.CLONE) {
214 getDataSource().setName("");
215 text_dataSourceName.setText("");
216 } else {
217 name = text_dataSourceName.getText();
218 }
219
220 }
221
222 private void addListeners() {
223 text_dataSourceName.addModifyListener(this);
224 text_databaseName.addModifyListener(this);
225 text_username.addModifyListener(this);
226 text_password.addModifyListener(this);
227 }
228
229 private void removeListeners() {
230 text_dataSourceName.removeModifyListener(this);
231 text_databaseName.removeModifyListener(this);
232 text_username.removeModifyListener(this);
233 text_password.removeModifyListener(this);
234 }
235
236 public void testDbConfiguration(){
237 testDbConfiguration(false);
238 }
239
240 /**
241 * Tries to open a connection to the given dataSource. Generates a message on either
242 * failure or success
243 */
244 public void testDbConfiguration(boolean ignoreSuccess){
245 try{
246 updateAndCheck();
247 updateDataSource();
248 getDataSource().testConnection();
249 if(!ignoreSuccess) {
250 MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!");
251 }
252 } catch(SQLException e){
253 MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
254 + System.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
255 + System.getProperty("line.separator")
256 + "Please double check the connection fields");
257 } catch (ClassNotFoundException e) {
258 MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage()
259 + System.getProperty("line.separator") //we may use System.lineSeparator when migrated to Java 1.7
260 + System.getProperty("line.separator")
261 + "Please double check the connection fields");
262 }
263 }
264
265 /**
266 * Form implementation for the specific database
267 */
268 public abstract void createDatabaseForm();
269
270 /**
271 * <p>updateLocation</p>
272 */
273 public abstract void updateLocation();
274
275 /**
276 * <p>updateDataSource</p>
277 */
278 public abstract void updateDataSource();
279
280 /**
281 * <p>checkPageComplete</p>
282 */
283 public void checkPageComplete(){
284 boolean complete = false;
285 if(mode == CdmDataSourceWizard.Mode.CREATE) {
286 complete = database.length() != 0;
287 } else {
288 complete = name.length() != 0 && database.length() != 0;
289 }
290 this.setPageComplete(complete);
291 }
292
293
294
295 /**
296 * updates the current datasource with form values
297 */
298 public void updateAuthentication(){
299 database = text_databaseName.getText();
300 username = text_username.getText();
301 password = text_password.getText();
302 }
303
304 /** {@inheritDoc} */
305 @Override
306 public IWizardPage getNextPage() {
307 return null;
308 }
309
310 /**
311 * <p>Setter for the field <code>dataSource</code>.</p>
312 *
313 * @param dataSource the dataSource to set
314 */
315 public void setDataSource(ICdmDataSource dataSource) {
316 this.dataSource = dataSource;
317 }
318
319 /**
320 * <p>Getter for the field <code>dataSource</code>.</p>
321 *
322 * @return the dataSource
323 */
324 public ICdmDataSource getUpdatedDataSource() {
325 updateDataSource();
326 return dataSource;
327 }
328
329 protected ICdmDataSource getDataSource() {
330 return dataSource;
331 }
332
333 /**
334 * <p>getDataSourceName</p>
335 *
336 * @return a {@link java.lang.String} object.
337 */
338 public String getDataSourceName() {
339 return name;
340 }
341
342 /** {@inheritDoc} */
343 @Override
344 public void modifyText(ModifyEvent e) {
345
346 name = text_dataSourceName.getText();
347 database = text_databaseName.getText();
348
349 switch(mode) {
350 case EDIT:
351 if(name.length() == 0){
352 name = "";
353 setErrorMessage("DataSource name must not be empty.");
354 this.setPageComplete(false);
355 return;
356 } else if ((!dataSource.getName().equals(name)) && (CdmDataSourceRepository.getDataSource(name) != null)) {
357 name = "";
358 setErrorMessage("DataSource with the same name already exists");
359 this.setPageComplete(false);
360 return;
361 }
362 break;
363 case CLONE:
364 if(name.length() == 0){
365 name = "";
366 setErrorMessage("DataSource name must not be empty.");
367 this.setPageComplete(false);
368 return;
369 } else if (CdmDataSourceRepository.getDataSource(name) != null) {
370 name = "";
371 setErrorMessage("DataSource with the same name already exists");
372 this.setPageComplete(false);
373 return;
374 }
375 break;
376 default:
377 break;
378 }
379
380 if(database.length() == 0){
381 setErrorMessage("Database name must not be empty.");
382 this.setPageComplete(false);
383 } else {
384 updateAndCheck();
385 setErrorMessage(null);
386 }
387 }
388
389
390 private void updateAndCheck() {
391 updateLocation();
392 updateAuthentication();
393 checkPageComplete();
394 }
395 /**
396 * <p>modifyTextWithoutTriggeringListeners</p>
397 *
398 * @param text a {@link org.eclipse.swt.widgets.Text} object.
399 * @param listener a {@link org.eclipse.swt.events.ModifyListener} object.
400 * @param string a {@link java.lang.String} object.
401 */
402 protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
403 text.removeModifyListener(listener);
404 text.setText(string);
405 text.addModifyListener(listener);
406 }
407 }