Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.local / src / main / java / eu / etaxonomy / taxeditor / local / 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.local.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.local.datasource.common.CdmDataSourceRepository;
33
34 /**
35 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
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 private ICdmDataSource dataSource;
43
44 protected Text text_password;
45 protected Text text_databaseName;
46 protected Text text_dataSourceName;
47 protected Text text_username;
48
49 protected Group authenticationGroup;
50 protected Group locationGroup;
51
52 protected Composite composite;
53
54 protected Composite parent;
55
56 protected String name;
57 protected String database;
58 protected String username;
59 protected String password;
60
61 CdmDataSourceWizard.Mode mode;
62
63 /**
64 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
65 *
66 * @param pageName a {@link java.lang.String} object.
67 */
68 protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) {
69 super(pageName);
70 this.setPageComplete(false);
71 setDataSource(dataSource);
72 mode = CdmDataSourceWizard.Mode.CREATE;
73 }
74
75 /**
76 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
77 *
78 * @param pageName a {@link java.lang.String} object.
79 */
80 protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
81 super(pageName);
82 this.setPageComplete(false);
83
84 this.mode = mode;
85 if(mode == CdmDataSourceWizard.Mode.CLONE) {
86 setDataSource(CdmDataSource.NewInstance(dataSource));
87 } else {
88 setDataSource(dataSource);
89 }
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public void createControl(Composite parent) {
95 this.parent = parent;
96
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);
104
105
106
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);
114
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));
120
121 if(getDataSource() == null) {
122 editDatasourceComposite.setVisible(false);
123 }
124
125 // create a database specific form
126 createDatabaseForm();
127
128 // create the authentication input fields
129 createAuthenticationForm();
130
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);
137
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");
142
143 // Test connection when button is pressed
144 testButton.addSelectionListener(new SelectionAdapter() {
145 @Override
146 public void widgetSelected(SelectionEvent e) {
147 testDbConfiguration();
148 }
149 });
150
151 setControl(composite);
152
153 init();
154
155 }
156
157 /**
158 * <p>createAuthenticationForm</p>
159 */
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);
168
169 // Create database name label
170 Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
171 databaseNameLabel.setText("Database Name:");
172
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));
176
177 // Create username label
178 Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
179 usernameLabel.setText("User Name:");
180
181 // Create username input
182 text_username = new Text(authenticationGroup, SWT.BORDER);
183 text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
184
185 // Create password label
186 Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
187 passwordLabel.setText("Password:");
188
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));
192
193 }
194
195 /**
196 * Initialize text fields
197 */
198 public void init() {
199 removeListeners();
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());
205 }
206 // add listeners after setting text to avoid the modify event being called
207 // for the initial value
208 addListeners();
209
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("");
215 } else {
216 name = text_dataSourceName.getText();
217 }
218
219 }
220
221 private void addListeners() {
222 text_dataSourceName.addModifyListener(this);
223 text_databaseName.addModifyListener(this);
224 text_username.addModifyListener(this);
225 text_password.addModifyListener(this);
226 }
227
228 private void removeListeners() {
229 text_dataSourceName.removeModifyListener(this);
230 text_databaseName.removeModifyListener(this);
231 text_username.removeModifyListener(this);
232 text_password.removeModifyListener(this);
233 }
234
235 public void testDbConfiguration(){
236 testDbConfiguration(false);
237 }
238
239 /**
240 * Tries to open a connection to the given dataSource. Generates a message on either
241 * failure or success
242 */
243 public void testDbConfiguration(boolean ignoreSuccess){
244 try{
245 updateAndCheck();
246 updateDataSource();
247 getDataSource().testConnection();
248 if(!ignoreSuccess) {
249 MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!");
250 }
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");
261 }
262 }
263
264 /**
265 * Form implementation for the specific database
266 */
267 public abstract void createDatabaseForm();
268
269 /**
270 * <p>updateLocation</p>
271 */
272 public abstract void updateLocation();
273
274 /**
275 * <p>updateDataSource</p>
276 */
277 public abstract void updateDataSource();
278
279 /**
280 * <p>checkPageComplete</p>
281 */
282 public void checkPageComplete(){
283 boolean complete = false;
284 if(mode == CdmDataSourceWizard.Mode.CREATE) {
285 complete = database.length() != 0;
286 } else {
287 complete = name.length() != 0 && database.length() != 0;
288 }
289 this.setPageComplete(complete);
290 }
291
292
293
294 /**
295 * updates the current datasource with form values
296 */
297 public void updateAuthentication(){
298 database = text_databaseName.getText();
299 username = text_username.getText();
300 password = text_password.getText();
301 }
302
303 /** {@inheritDoc} */
304 @Override
305 public IWizardPage getNextPage() {
306 return null;
307 }
308
309 /**
310 * <p>Setter for the field <code>dataSource</code>.</p>
311 *
312 * @param dataSource the dataSource to set
313 */
314 public void setDataSource(ICdmDataSource dataSource) {
315 this.dataSource = dataSource;
316 }
317
318 /**
319 * <p>Getter for the field <code>dataSource</code>.</p>
320 *
321 * @return the dataSource
322 */
323 public ICdmDataSource getUpdatedDataSource() {
324 updateDataSource();
325 return dataSource;
326 }
327
328 protected ICdmDataSource getDataSource() {
329 return dataSource;
330 }
331
332 /**
333 * <p>getDataSourceName</p>
334 *
335 * @return a {@link java.lang.String} object.
336 */
337 public String getDataSourceName() {
338 return name;
339 }
340
341 /** {@inheritDoc} */
342 @Override
343 public void modifyText(ModifyEvent e) {
344
345 name = text_dataSourceName.getText();
346 database = text_databaseName.getText();
347
348 switch(mode) {
349 case EDIT:
350 if(name.length() == 0){
351 name = "";
352 setErrorMessage("DataSource name must not be empty.");
353 this.setPageComplete(false);
354 return;
355 } else if ((!dataSource.getName().equals(name)) && (CdmDataSourceRepository.getDataSource(name) != null)) {
356 name = "";
357 setErrorMessage("DataSource with the same name already exists");
358 this.setPageComplete(false);
359 return;
360 }
361 break;
362 case CLONE:
363 if(name.length() == 0){
364 name = "";
365 setErrorMessage("DataSource name must not be empty.");
366 this.setPageComplete(false);
367 return;
368 } else if (CdmDataSourceRepository.getDataSource(name) != null) {
369 name = "";
370 setErrorMessage("DataSource with the same name already exists");
371 this.setPageComplete(false);
372 return;
373 }
374 break;
375 default:
376 break;
377 }
378
379 if(database.length() == 0){
380 setErrorMessage("Database name must not be empty.");
381 this.setPageComplete(false);
382 } else {
383 updateAndCheck();
384 setErrorMessage(null);
385 }
386 }
387
388
389 private void updateAndCheck() {
390 updateLocation();
391 updateAuthentication();
392 checkPageComplete();
393 }
394 /**
395 * <p>modifyTextWithoutTriggeringListeners</p>
396 *
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.
400 */
401 protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
402 text.removeModifyListener(listener);
403 text.setText(string);
404 text.addModifyListener(listener);
405 }
406 }