Project

General

Profile

Download (9.34 KB) Statistics
| Branch: | Tag: | Revision:
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 java.sql.SQLException;
14

    
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;
30

    
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;
36

    
37

    
38
/**
39
 * <p>Abstract CdmDataSourceCredentialsWizardPage class.</p>
40
 *
41
 * @author n.hoffmann
42
 * @created 19.05.2009
43
 * @version 1.0
44
 */
45
public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener{
46

    
47
	private ICdmDataSource dataSource;
48
	
49
	protected Text text_password;
50
	protected Text text_databaseName;
51
	protected Text text_username;
52
	
53
	protected Group authenticationGroup;
54
	protected Group locationGroup;
55
	protected Group nomenclaturalCodeGroup;
56

    
57
	protected Composite composite;
58

    
59
	protected Composite parent;
60

    
61
	protected String database;
62
	protected String username;
63
	protected String password;
64
	
65
	protected NomenclaturalCode nomenclaturalCode;
66
	
67
	/**
68
	 * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
69
	 *
70
	 * @param pageName a {@link java.lang.String} object.
71
	 */
72
	protected CdmDataSourceCredentialsWizardPage(String pageName) {
73
		super(pageName);
74
		this.setPageComplete(false);
75
	}
76
	
77
	/* (non-Javadoc)
78
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
79
	 */
80
	/** {@inheritDoc} */
81
	public void createControl(Composite parent) {
82
		this.parent = parent;
83
		
84
		// Create top-level composite 
85
		
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);
91

    
92
		// create a database specific form
93
		createDatabaseForm();
94
		
95
		// create the authentication input fields
96
		createAuthenticationForm();
97
		
98
		// create nomenclatural code combo
99
		createNomenclaturalCodeForm();
100
		
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);
107
		
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");
112
		
113
		// Test connection when button is pressed
114
		testButton.addSelectionListener(new SelectionAdapter() {
115
			/* (non-Javadoc)
116
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
117
			 */
118
			@Override
119
			public void widgetSelected(SelectionEvent e) {
120
				testDbConfiguration();
121
			}
122
		});
123
		
124
		setControl(composite);
125
		
126
		init();
127
	}
128

    
129
	/**
130
	 * <p>createAuthenticationForm</p>
131
	 */
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);
140
		
141
		// Create database name label
142
		Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
143
		databaseNameLabel.setText("Database Name:");
144

    
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);
149

    
150
		// Create username label
151
		Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
152
		usernameLabel.setText("User Name:");
153
		
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);
158
		
159
		// Create password label
160
		Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
161
		passwordLabel.setText("Password:");
162

    
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);
167
	}
168
	
169
	/**
170
	 * Create a radio button group to select a nomenclatural code from
171
	 */
172
	private void createNomenclaturalCodeForm() {
173
		nomenclaturalCodeGroup = new Group(composite , SWT.NONE);
174
		nomenclaturalCodeGroup.setLayout(new GridLayout());
175
		
176
		nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode();
177
		
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));
184
			}
185
			button.addSelectionListener(new SelectionAdapter() {
186
				public void widgetSelected(SelectionEvent e) {
187
					nomenclaturalCode = (NomenclaturalCode) e.widget.getData();
188
					modifyText(null);
189
				}
190
			});
191
		}		
192
	}
193
	
194
	
195
	
196
	/**
197
	 * Tries to open a connection to the given dataSource. Generates a message on either
198
	 * failure or success
199
	 */
200
	public void testDbConfiguration(){
201
		try{
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);
210
		} 
211
	}
212
	
213
	/**
214
	 * Form implementation for the specific database
215
	 */
216
	public abstract void createDatabaseForm(); 
217
	
218
	/**
219
	 * <p>updateLocation</p>
220
	 */
221
	public abstract void updateLocation();
222
	
223
	/**
224
	 * <p>updateDataSource</p>
225
	 */
226
	public abstract void updateDataSource();
227
	
228
	/**
229
	 * <p>checkPageComplete</p>
230
	 */
231
	public void checkPageComplete(){
232
		boolean complete = database.length() != 0;
233
//		complete &= username.length() != 0;
234
		
235
		this.setPageComplete(complete);
236
	}
237
	
238
	/**
239
	 * Initialize text fields
240
	 */
241
	public void init() {
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());
246
		}
247
	}
248
	
249
	/**
250
	 * updates the current datasource with form values
251
	 */
252
	public void updateAuthentication(){
253
		
254
		database = text_databaseName.getText();
255
		username = text_username.getText();
256
		password = text_password.getText();
257
	}
258

    
259
	/* (non-Javadoc)
260
	 * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
261
	 */
262
	/** {@inheritDoc} */
263
	@Override
264
	public IWizardPage getNextPage() {
265
		return null;
266
	}
267

    
268
	/**
269
	 * <p>Setter for the field <code>dataSource</code>.</p>
270
	 *
271
	 * @param dataSource the dataSource to set
272
	 */
273
	public void setDataSource(ICdmDataSource dataSource) {
274
		this.dataSource = dataSource;
275
	}
276

    
277
	/**
278
	 * <p>Getter for the field <code>dataSource</code>.</p>
279
	 *
280
	 * @return the dataSource
281
	 */
282
	public ICdmDataSource getDataSource() {
283
		return dataSource;
284
	}
285
	
286
	/**
287
	 * <p>getDataSourceName</p>
288
	 *
289
	 * @return a {@link java.lang.String} object.
290
	 */
291
	public String getDataSourceName() {
292
		if(dataSource instanceof CdmPersistentDataSource){
293
			return ((CdmPersistentDataSource) dataSource).getName();
294
		}
295
		return null;
296
	}
297
	
298
	/* (non-Javadoc)
299
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
300
	 */
301
	/** {@inheritDoc} */
302
	public void modifyText(ModifyEvent e) {
303
		updateLocation();
304
		updateAuthentication();
305
		updateDataSource();
306
		checkPageComplete();
307
	}	
308
	
309
	/**
310
	 * <p>modifyTextWithoutTriggeringListeners</p>
311
	 *
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.
315
	 */
316
	protected void modifyTextWithoutTriggeringListeners(Text text, ModifyListener listener, String string){
317
		text.removeModifyListener(listener);
318
		text.setText(string);
319
		text.addModifyListener(listener);
320
	}
321
}
(1-1/6)