Project

General

Profile

« Previous | Next » 

Revision 192243b3

Added by Katja Luther over 3 years ago

ref #9189: move datasource to webapp

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.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
 * @author n.hoffmann
10
 */
11
package eu.etaxonomy.taxeditor.datasource;
12

  
13
import java.io.File;
14
import java.io.FileNotFoundException;
15
import java.util.ArrayList;
16
import java.util.List;
17

  
18
import org.eclipse.core.runtime.IPath;
19
import org.eclipse.ui.IMemento;
20
import org.eclipse.ui.XMLMemento;
21

  
22
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
23
import eu.etaxonomy.cdm.config.ICdmPersistentSource;
24
import eu.etaxonomy.cdm.config.ICdmSource;
25
import eu.etaxonomy.cdm.database.CdmDataSource;
26
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
27
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
28
import eu.etaxonomy.cdm.database.ICdmDataSource;
29
import eu.etaxonomy.taxeditor.model.MementoHelper;
30
import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
32
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
33
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
34
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
37

  
38
public class CdmDataSourceRepository{
39

  
40
	/** Constant <code>TAG_DATASOURCE="tagDataSource"</code> */
41
	public static final String TAG_DATASOURCE = "tagDataSource";
42
	private static final String CURRENT_DATASOURCE = "currentDataSource";
43
	private static final String CURRENT_DATASOURCE_POSTFIX = "currentDataSourcePostFix";
44
	private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml";
45

  
46
	private static final String DEFAULT_DATASOURCE_NAME = "cdm";
47
	private static final String DEFAULT_DATASOURCE_POSTFIX = CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX;
48

  
49
	private static ICdmSource currentCdmSource;
50
	private static IMemento memento;
51

  
52
	private static String lastUsedCdmSourceName;
53
	private static String lastUsedCdmSourcePostFix;
54

  
55
	/**
56
	 * <p>Getter for the field <code>lastUsedDataSourceName</code>.</p>
57
	 *
58
	 * @return a {@link java.lang.String} object.
59
	 */
60
	public static void updateLastUsedDataSource(){
61
		if(lastUsedCdmSourceName == null){
62
			memento = readMemento();
63
			lastUsedCdmSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME;
64
			lastUsedCdmSourcePostFix = memento != null ? memento.getString(CURRENT_DATASOURCE_POSTFIX) : DEFAULT_DATASOURCE_POSTFIX;
65
		}
66
	}
67

  
68
	/**
69
	 * <p>delete</p>
70
	 *
71
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
72
	 * @return a boolean.
73
	 */
74
	public static boolean delete(ICdmPersistentSource cdmPersistentSource) {
75
		CdmPersistentSourceUtils.delete(cdmPersistentSource);
76
		return true;
77
	}
78

  
79
	/**
80
	 * <p>getAll</p>
81
	 *
82
	 * @return a {@link java.util.List} object.
83
	 */
84
	public static List<ICdmSource> getAll() {
85
		List<ICdmSource> remoteSources = new ArrayList<>();
86

  
87
		if (CdmPersistentDataSource.getAllDataSources() == null){
88
			MessagingUtils.errorDialog("Could not read root element", CdmPersistentDataSource.class,
89
					"Could not read element in datasources.xml in .cdmLibrary folder. Maybe your datasources.xml file is broken.",
90
					TaxeditorStorePlugin.PLUGIN_ID, null, true);
91
			return remoteSources;
92
		}
93
		for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
94
			try {
95
				remoteSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName()));
96
			} catch (DataSourceNotFoundException e) {
97
				MessagingUtils.error(CdmDataSourceRepository.class, "Could not find dataSource", e);
98
			}
99
		}
100

  
101
		try {
102
			for(ICdmRemoteSource remoteSource : CdmPersistentRemoteSource.getAllRemoteSources()){
103
				remoteSources.add(remoteSource);
104
			}
105
		} catch (CdmRemoteSourceException e) {
106
			MessagingUtils.error(CdmDataSourceRepository.class, "Error retrieving remote sources", e);
107
		}
108

  
109
		// TODO sort by database name
110

  
111
		return remoteSources;
112
	}
113

  
114
	/**
115
	 * <p>getDataSource</p>
116
	 *
117
	 * @param name a {@link java.lang.String} object.
118
	 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
119
	 */
120
	public static ICdmDataSource getDataSource(String name){
121

  
122
		for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
123
			try {
124
				if(dataSource.getName().equals(name)){
125
					return CdmPersistentDataSource.NewInstance(dataSource.getName());
126
				}
127
			} catch (DataSourceNotFoundException e) {
128
				MessagingUtils.error(CdmDataSourceRepository.class, "Could not find dataSource", e);
129
			}
130
		}
131

  
132
		return null;
133
	}
134

  
135
	/**
136
	 * <p>Getter for the field <code>currentDataSource</code>.</p>
137
	 *
138
	 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
139
	 * @throws CdmRemoteSourceException
140
	 */
141
	public static ICdmSource getCurrentCdmSource() throws CdmRemoteSourceException {
142

  
143
		if (currentCdmSource == null) {
144
			updateLastUsedDataSource();
145

  
146
			if(lastUsedCdmSourceName == null) {
147
				return null;
148
			}
149
			// for post fix = null , source is by default a data source
150
			if(lastUsedCdmSourcePostFix == null || lastUsedCdmSourcePostFix.equals(CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX)) {
151
				try {
152
					currentCdmSource = CdmPersistentDataSource.NewInstance(lastUsedCdmSourceName);
153
				} catch (DataSourceNotFoundException e) {
154
					// fallback creates a new default
155
					setCurrentCdmSource(createDefaultH2DataSource());
156
				}
157
			} else if (lastUsedCdmSourcePostFix.equals(CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX)) {
158
				currentCdmSource = CdmPersistentRemoteSource.NewInstance(lastUsedCdmSourceName);
159
			} else {
160
				throw new CdmRemoteSourceException("Unkown Cdm Source Type");
161
			}
162
		}
163
		return currentCdmSource;
164
	}
165

  
166
	/**
167
	 * Creates a default H2 CDM Data Source
168
	 *
169
	 * @return the newly created data source
170
	 */
171
	public static ICdmDataSource createDefaultH2DataSource() {
172
	    ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
173
	    		DEFAULT_DATASOURCE_NAME, "sa", "");
174
        save(h2DataSource.getName(), h2DataSource);
175
        return h2DataSource;
176
	}
177

  
178
	/**
179
	 * <p>Setter for the field <code>currentDataSource</code>.</p>
180
	 *
181
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
182
	 * @return a boolean.
183
	 */
184
	public static boolean setCurrentCdmSource(ICdmSource cdmSource) {
185
		currentCdmSource = cdmSource;
186
		return true;
187
	}
188

  
189
	/**
190
	 * <p>changeDataSource</p>
191
	 *
192
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
193
	 * @return a boolean.
194
	 */
195
	public static boolean changeDataSource(final ICdmSource cdmSource) {
196
		saveAsCurrentDatabaseToMemento(cdmSource);
197
		CdmStore.connect(cdmSource);
198

  
199
		return true;
200
	}
201
//
202
//	/**
203
//	 * <p>save</p>
204
//	 *
205
//	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
206
//	 * @param strDataSourceName a {@link java.lang.String} object.
207
//	 * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
208
//	 * @throws CdmRemoteSourceException
209
//	 */
210
//	public static ICdmPersistentSource save(String strCdmSourceName, ICdmRemoteSource cdmSource) throws CdmRemoteSourceException {
211
//		return CdmPersistentRemoteSource.save(strCdmSourceName, cdmSource);
212
//	}
213

  
214
	/**
215
	 * <p>save</p>
216
	 *
217
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
218
	 * @param strDataSourceName a {@link java.lang.String} object.
219
	 * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
220
	 * @throws CdmRemoteSourceException
221
	 */
222
	public static ICdmPersistentSource save(String strCdmSourceName, ICdmDataSource cdmSource)  {
223
		return CdmPersistentDataSource.save(strCdmSourceName, cdmSource);
224
	}
225

  
226
	/**
227
	 * <p>update</p>
228
	 *
229
	 * @param strDataSourceName a {@link java.lang.String} object.
230
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
231
	 * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
232
	 */
233
	public static ICdmPersistentSource update(String strCdmSourceName, ICdmDataSource cdmSource){
234
		try {
235
			return CdmPersistentDataSource.update(strCdmSourceName, cdmSource);
236
		} catch (Exception e) {
237
			MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
238
		}
239
		return null;
240
	}
241

  
242
	public static ICdmPersistentSource replace(String strCdmSourceName, ICdmDataSource cdmSource){
243
		try {
244
			return CdmPersistentDataSource.replace(strCdmSourceName, cdmSource);
245
		} catch (Exception e) {
246
			MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
247
		}
248
		return null;
249
	}
250

  
251
	/**
252
	 * <p>update</p>
253
	 *
254
	 * @param strDataSourceName a {@link java.lang.String} object.
255
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
256
	 * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
257
	 */
258
	public static ICdmPersistentSource update(String strCdmSourceName, CdmRemoteSource cdmSource){
259
		try {
260
			return CdmPersistentRemoteSource.update(strCdmSourceName, cdmSource);
261
		} catch (Exception e) {
262
			MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
263

  
264
		}
265
		return null;
266
	}
267

  
268
	/*********************************************************
269
	 * Memento Handling										 *
270
	 *********************************************************/
271

  
272
	private static void saveAsCurrentDatabaseToMemento(ICdmSource cdmSource){
273
		if(memento == null) {
274
			memento = readMemento();
275
		}
276
		memento.putString(CURRENT_DATASOURCE, cdmSource.getName());
277

  
278
		// Set postfix to distinguish between data and remote sources
279
		if(cdmSource instanceof CdmPersistentRemoteSource) {
280
			memento.putString(CURRENT_DATASOURCE_POSTFIX, CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX);
281
		} else {
282
			memento.putString(CURRENT_DATASOURCE_POSTFIX, CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX);
283
		}
284

  
285

  
286
		saveMementoToFile(memento);
287
	}
288

  
289
	/*
290
	 * Answer the workbench state file.
291
	 */
292
	private static File getCdmSourceStateFile() {
293
		IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
294
		if (path == null) {
295
			return null;
296
		}
297
		path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME);
298
		return path.toFile();
299
	}
300

  
301
	private static IMemento readMemento(){
302
		try {
303
			return MementoHelper.readMementoFromFile(getCdmSourceStateFile());
304
		} catch (FileNotFoundException e) {
305
			return initializeMemento();
306
		}
307
	}
308

  
309
	private static IMemento initializeMemento() {
310

  
311
		XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE);
312
		memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME);
313
		saveMementoToFile(memento);
314

  
315
		return readMemento();
316
	}
317

  
318
	/*
319
	 * Save the workbench UI in a persistence file.
320
	 */
321
	private static IMemento saveMementoToFile(IMemento memento) {
322
		return MementoHelper.saveMementoToFile(memento, getCdmSourceStateFile());
323
	}
324
}
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
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceH2WizardPage.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 eu.etaxonomy.cdm.database.CdmDataSource;
13
import eu.etaxonomy.cdm.database.ICdmDataSource;
14

  
15
/**
16
 * <p>CdmDataSourceH2WizardPage class.</p>
17
 *
18
 * @author n.hoffmann
19
 * @created 19.05.2009
20
 */
21
public class CdmDataSourceH2WizardPage extends CdmDataSourceCredentialsWizardPage {
22
	
23
	/**
24
	 * <p>Constructor for CdmDataSourceH2WizardPage.</p>
25
	 *
26
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
27
	 */
28
	@Deprecated
29
	protected CdmDataSourceH2WizardPage(ICdmDataSource dataSource) {
30
		super("H2", dataSource);
31
		setTitle("Enter credentials for embedded H2 database");		
32
	}
33
	
34
	/**
35
	 * <p>Constructor for CdmDataSourceH2WizardPage.</p>
36
	 *
37
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
38
	 * @param mode a {@link eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceWizard.Mode} enum type.
39
	 */
40
	protected CdmDataSourceH2WizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
41
		super("H2", dataSource, mode);
42
		setTitle("Enter credentials for embedded H2 database");		
43
	}
44
	
45
	/** {@inheritDoc} */
46
	@Override
47
	public void createDatabaseForm() {
48
		// no more fields needed for embedded H2
49
	}
50

  
51
	/** {@inheritDoc} */
52
	@Override
53
	public void updateLocation() {
54
		// nothing to do, no location data provided
55
	}
56

  
57
	/** {@inheritDoc} */
58
	@Override
59
	public void updateDataSource() {		
60
		ICdmDataSource dataSource = getDataSource();
61

  
62
		if(dataSource == null) {
63
			setDataSource(CdmDataSource.NewH2EmbeddedInstance(database, 
64
					 username, 
65
					 password));
66
		} else {
67
			dataSource.setName(name);
68
			dataSource.setDatabase(database);
69
			dataSource.setUsername(username);
70
			dataSource.setPassword(password);
71
		}
72
	}
73

  
74
	/** {@inheritDoc} */
75
	@Override
76
	public void checkPageComplete() {
77
		super.checkPageComplete();
78
	}
79
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceMySQLWizardPage.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 org.eclipse.swt.SWT;
13
import org.eclipse.swt.layout.GridData;
14
import org.eclipse.swt.layout.GridLayout;
15
import org.eclipse.swt.widgets.Group;
16
import org.eclipse.swt.widgets.Label;
17
import org.eclipse.swt.widgets.Text;
18

  
19
import eu.etaxonomy.cdm.database.CdmDataSource;
20
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
21
import eu.etaxonomy.cdm.database.ICdmDataSource;
22

  
23
/**
24
 * <p>CdmDataSourceMySQLWizardPage class.</p>
25
 *
26
 * @author n.hoffmann
27
 * @created 19.05.2009
28
 */
29
public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizardPage {
30
	
31
	private Text text_port;
32
	private Text text_server;
33

  
34
	private String server;
35

  
36
	private int port;
37

  
38
	
39

  
40
	/**
41
	 * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
42
	 *
43
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
44
	 */
45
	@Deprecated
46
	protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource) {
47
		super("MySQL", dataSource, CdmDataSourceWizard.Mode.CREATE);
48
		setTitle("MySQL Server");
49
		setDescription("Enter credentials for MySQL database");
50

  
51
	}
52
	
53
	/**
54
	 * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
55
	 *
56
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
57
	 * @param mode a {@link eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceWizard.Mode} enum type.
58
	 */
59
	protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
60
		super("MySQL", dataSource, mode);
61
		setTitle("MySQL Server");
62
		setDescription("Enter credentials for MySQL database");
63

  
64
	}
65
	
66
	/** {@inheritDoc} */
67
	@Override
68
	public void createDatabaseForm() {
69
		// Create group composite for location data 
70
		locationGroup = new Group(composite, SWT.NONE);
71
		locationGroup.setText("Location");
72
		locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 2));
73
		GridLayout locationLayout = new GridLayout();
74
		locationLayout.numColumns = 2;
75
		locationGroup.setLayout(locationLayout);
76

  
77
		// Create host label
78
		Label serverLabel = new Label(locationGroup, SWT.NONE);
79
		serverLabel.setText("Host:");
80

  
81
		// Create host input
82
		text_server = new Text(locationGroup, SWT.BORDER);
83
		text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
84
		
85

  
86
		// Create port label
87
		Label portLabel = new Label(locationGroup, SWT.NONE);
88
		portLabel.setText("Port:");
89

  
90
		// Create port input
91
		text_port = new Text(locationGroup, SWT.BORDER);
92
		text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
93
		
94
		
95
	}
96

  
97
	/** {@inheritDoc} */
98
	@Override
99
	public void updateLocation() {
100
		server = text_server.getText();
101
		try{
102
			if(! text_port.getText().equals("")){
103
				port = new Integer(text_port.getText());
104
				setErrorMessage(null);
105
			}
106
		}catch(NumberFormatException e){
107
			setErrorMessage("Port number contains invalid characters");
108
		}
109
	}
110

  
111
	/** {@inheritDoc} */
112
	@Override
113
	public void updateDataSource() {	
114
		ICdmDataSource dataSource = getDataSource();
115

  
116
		if(dataSource == null) {
117
			setDataSource(CdmDataSource.NewMySqlInstance(server,
118
					database,
119
					port,
120
					username,
121
					password));
122
		} else {
123
			dataSource.setName(name);
124
			dataSource.setServer(server);
125
			dataSource.setDatabase(database);
126
			dataSource.setPort(port);
127
			dataSource.setUsername(username);
128
			dataSource.setPassword(password);
129
		}
130
	}
131
	
132
	/** {@inheritDoc} */
133
	@Override
134
	public void checkPageComplete() {
135
		// check if widgets of this component are complete
136
		boolean complete = server.trim().length() != 0;
137
		
138
		// set default port
139
		if(port == 0){
140
			port = DatabaseTypeEnum.MySQL.getDefaultPort();
141
		}
142
		
143
		setPageComplete(complete);
144
		
145
		// check if widgets of the extended class are complete
146
		super.checkPageComplete();
147
	}
148
	
149
	/** {@inheritDoc} */
150
	@Override
151
	public void init() {
152
		super.init();
153
		if(getDataSource() != null){
154
			removeListeners();
155
			text_server.setText(getDataSource().getServer());
156
			text_port.setText(String.valueOf(getDataSource().getPort()));		
157
			// add listeners after setting text to avoid the modify event being called
158
			// for the initial value
159
			addListeners();
160
		}
161

  
162
	}
163
	
164
	private void addListeners() {
165
		text_server.addModifyListener(this);
166
		text_port.addModifyListener(this);
167
	}
168
	
169
	private void removeListeners() {
170
		text_server.removeModifyListener(this);
171
		text_port.removeModifyListener(this);
172
	}
173
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourcePostgreSQLServerWizardPage.java
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.taxeditor.datasource.wizard;
5

  
6
import org.eclipse.swt.SWT;
7
import org.eclipse.swt.layout.GridData;
8
import org.eclipse.swt.layout.GridLayout;
9
import org.eclipse.swt.widgets.Group;
10
import org.eclipse.swt.widgets.Label;
11
import org.eclipse.swt.widgets.Text;
12

  
13
import eu.etaxonomy.cdm.database.CdmDataSource;
14
import eu.etaxonomy.cdm.database.ICdmDataSource;
15

  
16
/**
17
 * @author n.hoffmann
18
 */
19
public class CdmDataSourcePostgreSQLServerWizardPage extends
20
		CdmDataSourceCredentialsWizardPage {
21

  
22
	private Text text_port;
23
	private Text text_server;
24

  
25
	private String server;
26

  
27
	private int port;
28
	
29
	@Deprecated
30
	protected CdmDataSourcePostgreSQLServerWizardPage(ICdmDataSource dataSource) {
31
		super("PostgreSQL Server", dataSource);
32
		setTitle("PostgreSQL Server");
33
		setDescription("Enter credentials for PostgreSQL Server database");
34

  
35
	}
36
	
37
	/**
38
	 * <p>Constructor for CdmDataSourcePostgreSQLServerWizardPage.</p>
39
	 *
40
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
41
	 * @param mode a {@link eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceWizard.Mode} enum type.
42
	 */
43
	protected CdmDataSourcePostgreSQLServerWizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
44
		super("PostgreSQL Server", dataSource, mode);
45
		setTitle("PostgreSQL Server");
46
		setDescription("Enter credentials for PostgreSQL Server database");
47

  
48
	}
49

  
50
	@Override
51
	public void createDatabaseForm() {
52
		// Create group composite for location data 
53
		locationGroup = new Group(composite, SWT.NONE);
54
		locationGroup.setText("Location");
55
		locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
56
		GridLayout locationLayout = new GridLayout();
57
		locationLayout.numColumns = 2;
58
		locationGroup.setLayout(locationLayout);
59

  
60
		// Create host label
61
		Label serverLabel = new Label(locationGroup, SWT.NONE);
62
		serverLabel.setText("Host:");
63

  
64
		// Create host input
65
		text_server = new Text(locationGroup, SWT.BORDER);
66
		text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
67
		
68

  
69
		// Create port label
70
		Label portLabel = new Label(locationGroup, SWT.NONE);
71
		portLabel.setText("Port:");
72

  
73
		// Create port input
74
		text_port = new Text(locationGroup, SWT.BORDER);
75
		text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
76
		
77
		
78
	}
79

  
80
	@Override
81
	public void updateLocation() {
82
		server = text_server.getText();
83
		try{
84
			if(! text_port.getText().equals("")){
85
				port = new Integer(text_port.getText());
86
				setErrorMessage(null);
87
			}
88
		}catch(NumberFormatException e){
89
			setErrorMessage("Port number contains invalid characters");
90
		}
91
	}
92

  
93
	@Override
94
	public void updateDataSource() {
95

  
96
		
97
		ICdmDataSource dataSource = getDataSource();
98

  
99
		if(dataSource == null) {
100
			setDataSource(CdmDataSource.NewPostgreSQLInstance(server,
101
					database,
102
					port,
103
					username,
104
					password));
105
		} else {
106
			dataSource.setName(name);
107
			dataSource.setServer(server);
108
			dataSource.setDatabase(database);
109
			dataSource.setPort(port);
110
			dataSource.setUsername(username);
111
			dataSource.setPassword(password);
112
		}
113
	}
114
	
115
	/** {@inheritDoc} */
116
	@Override
117
	public void init() {
118
		super.init();
119
		if(getDataSource() != null){
120
			removeListeners();
121
			text_server.setText(getDataSource().getServer());
122
			text_port.setText(String.valueOf(getDataSource().getPort()));	
123
			// add listeners after setting text to avoid the modify event being called
124
			// for the initial value
125
			addListeners();			
126
		}
127

  
128
	}
129
	
130
	private void addListeners() {
131
		text_server.addModifyListener(this);
132
		text_port.addModifyListener(this);
133
	}
134
	
135
	private void removeListeners() {
136
		text_server.removeModifyListener(this);
137
		text_port.removeModifyListener(this);
138
	}
139

  
140
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceSQLServerWizardPage.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 org.eclipse.swt.SWT;
13
import org.eclipse.swt.layout.GridData;
14
import org.eclipse.swt.layout.GridLayout;
15
import org.eclipse.swt.widgets.Group;
16
import org.eclipse.swt.widgets.Label;
17
import org.eclipse.swt.widgets.Text;
18

  
19
import eu.etaxonomy.cdm.database.CdmDataSource;
20
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
21
import eu.etaxonomy.cdm.database.ICdmDataSource;
22

  
23
/**
24
 * <p>CdmDataSourceSQLServerWizardPage class.</p>
25
 *
26
 * @author n.hoffmann
27
 * @created 19.05.2009
28
 * @version 1.0
29
 */
30
public class CdmDataSourceSQLServerWizardPage extends CdmDataSourceCredentialsWizardPage {
31

  
32
	private Text text_port;
33
	private Text text_server;
34

  
35
	private String server;
36

  
37
	private int port;
38

  
39
	/**
40
	 * <p>Constructor for CdmDataSourceSQLServerWizardPage.</p>
41
	 *
42
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
43
	 */
44
	@Deprecated
45
	public CdmDataSourceSQLServerWizardPage(ICdmDataSource dataSource) {
46
		super("SQL Server", dataSource);
47
		setTitle("SQL Server");
48
		setDescription("Enter credentials for SQL Server database");
49
		this.setDataSource(dataSource);
50
	}
51
	
52
	/**
53
	 * <p>Constructor for CdmDataSourceSQLServerWizardPage.</p>
54
	 *
55
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
56
	 * param mode a {@link eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceWizard.Mode} enum type.
57
	 */
58
	public CdmDataSourceSQLServerWizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
59
		super("SQL Server", dataSource, mode);
60
		setTitle("SQL Server");
61
		setDescription("Enter credentials for SQL Server database");
62
		this.setDataSource(dataSource);
63
	}
64
	
65
	/* (non-Javadoc)
66
	 * @see eu.etaxonomy.taxeditor.store.datasource.CdmDataSourceCredentialsWizardPage#createDatabaseForm()
67
	 */
68
	/** {@inheritDoc} */
69
	@Override
70
	public void createDatabaseForm() {		
71
		// Create group composite for location data 
72
		locationGroup = new Group(composite, SWT.NONE);
73
		locationGroup.setText("Location");
74
		locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
75
		GridLayout locationLayout = new GridLayout();
76
		locationLayout.numColumns = 2;
77
		locationGroup.setLayout(locationLayout);
78

  
79
		// Create host label
80
		Label serverLabel = new Label(locationGroup, SWT.NONE);
81
		serverLabel.setText("Host:");
82

  
83
		// Create host input
84
		text_server = new Text(locationGroup, SWT.BORDER);
85
		text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
86
		text_server.addModifyListener(this);
87

  
88
		// Create port label
89
		Label portLabel = new Label(locationGroup, SWT.NONE);
90
		portLabel.setText("Port:");
91

  
92
		// Create port input
93
		text_port = new Text(locationGroup, SWT.BORDER);
94
		text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
95
		
96
		text_port.addModifyListener(this);
97
	}
98

  
99
	/* (non-Javadoc)
100
	 * @see eu.etaxonomy.taxeditor.store.datasource.CdmDataSourceCredentialsWizardPage#updateLocation()
101
	 */
102
	/** {@inheritDoc} */
103
	@Override
104
	public void updateLocation() {
105
		server = text_server.getText();
106
		try{
107
			if(! text_port.getText().equals("")){
108
				port = new Integer(text_port.getText());
109
				setErrorMessage(null);
110
			}
111
		}catch(NumberFormatException e){
112
			setErrorMessage("Port number contains invalid characters");
113
		}
114
	}
115

  
116
	/* (non-Javadoc)
117
	 * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#updateDataSource()
118
	 */
119
	/** {@inheritDoc} */
120
	@Override
121
	public void updateDataSource() {
122

  
123

  
124
		
125
		ICdmDataSource dataSource = getDataSource();
126

  
127
		if(dataSource == null) {
128
			setDataSource(CdmDataSource.NewSqlServer2005Instance(server,
129
					database,
130
					port,
131
					username,
132
					password));
133
		} else {
134
			dataSource.setName(name);
135
			dataSource.setServer(server);
136
			dataSource.setDatabase(database);
137
			dataSource.setPort(port);
138
			dataSource.setUsername(username);
139
			dataSource.setPassword(password);
140
		}
141
	}
142
	
143
	/* (non-Javadoc)
144
	 * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#checkPageComplete()
145
	 */
146
	/** {@inheritDoc} */
147
	@Override
148
	public void checkPageComplete() {
149
		// check if widgets of this component are complete
150
		boolean complete = server.trim().length() != 0;
151
		
152
		// set default port
153
		if(port == 0){
154
			port = DatabaseTypeEnum.SqlServer2005.getDefaultPort();
155
		}
156
		
157
		setPageComplete(complete);
158
		
159
		// check if widgets of the extended class are complete
160
		super.checkPageComplete();
161
	}
162

  
163
	/* (non-Javadoc)
164
	 * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#init()
165
	 */
166
	/** {@inheritDoc} */
167
	@Override
168
	public void init() {
169
		super.init();
170
		if(getDataSource() != null){
171
			removeListeners();
172
			text_server.setText(getDataSource().getServer());
173
			text_port.setText(String.valueOf(getDataSource().getPort()));			
174
			// add listeners after setting text to avoid the modify event being called
175
			// for the initial value
176
			addListeners();
177
		}
178

  
179
	}
180
	
181
	private void addListeners() {
182
		text_server.addModifyListener(this);
183
		text_port.addModifyListener(this);
184
	}
185
	
186
	private void removeListeners() {
187
		text_server.removeModifyListener(this);
188
		text_port.removeModifyListener(this);
189
	}
190
	
191
	
192
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceTypeSelectionWizardPage.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.util.ArrayList;
13

  
14
import org.eclipse.jface.wizard.IWizardPage;
15
import org.eclipse.jface.wizard.Wizard;
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.Combo;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Label;
27
import org.eclipse.swt.widgets.Text;
28

  
29
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
30
import eu.etaxonomy.cdm.database.ICdmDataSource;
31
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
32
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
33

  
34
/**
35
 * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
36
 *
37
 * @author n.hoffmann
38
 * @created 18.05.2009
39
 * @version 1.0
40
 */
41
public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
42

  
43
	public static final DatabaseTypeEnum[] supportedDatabaseTypes = new DatabaseTypeEnum[]{
44
		DatabaseTypeEnum.MySQL,
45
		DatabaseTypeEnum.H2,
46
		DatabaseTypeEnum.PostgreSQL
47
		/*DatabaseTypeEnum.SqlServer2005*/
48
	};
49

  
50

  
51
	private ArrayList<DatabaseTypeEnum> databaseTypes;
52

  
53
	private Text datasourceNameText;
54
	private String dataSourceName;
55
	private Combo databaseTypeCombo;
56

  
57
	private Composite composite;
58
	private Composite editDatasourceComposite;
59

  
60
	private NomenclaturalCode nomenclaturalCode;
61

  
62
	private boolean dataBaseTypeSelected = false;
63
	private boolean dataSourceNameSet = false;
64

  
65
	private ICdmDataSource dataSource;
66

  
67
	private WizardPage nextPage;
68

  
69
	private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
70

  
71
	/**
72
	 * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
73
	 *
74
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
75
	 */
76
	public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
77
		super("DataSourceWizardPage");
78

  
79
		this.dataSource = dataSource;
80

  
81
		String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
82

  
83
		setTitle(pageName);
84
	}
85

  
86
	/* (non-Javadoc)
87
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
88
	 */
89
	/** {@inheritDoc} */
90
	@Override
91
    public void createControl(Composite parent) {
92

  
93
		setPageComplete(false);
94

  
95
		// Create top-level composite
96
		composite = new Composite(parent, SWT.NONE);
97
		GridLayout gridLayout = new GridLayout();
98
		gridLayout.numColumns = 1;
99
		composite.setLayout(gridLayout);
100

  
101
		// Create editDatasourceComposite to display a dataSource's name and type
102
		editDatasourceComposite = new Composite(composite, SWT.NONE);
103
		GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true);
104
		editDatasourceComposite.setLayoutData(datasourceGridData);
105
		GridLayout datasourceLayout = new GridLayout();
106
		datasourceLayout.numColumns = 2;
107
		editDatasourceComposite.setLayout(datasourceLayout);
108

  
109
		// Create label and input for dataSource name
110
		Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
111
		datasourceNameLabel.setText("Datasource Name:");
112
		datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
113
		datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
114
		datasourceNameText.addModifyListener(this);
115

  
116
		// Create label and dropdown for database type
117
		Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
118
		databaseTypeLabel.setText("Database Type:");
119
		databaseTypeCombo = new Combo(editDatasourceComposite, SWT.BORDER | SWT.READ_ONLY);
120
		GridData comboLayout = new GridData(SWT.FILL, SWT.CENTER, false, false);
121
		databaseTypeCombo.setLayoutData(comboLayout);
122
		populateComboBoxItems();
123

  
124
		// Create listener to display database type-specific config options
125
		databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
126
			@Override
127
            public void widgetSelected(SelectionEvent e) {
128

  
129
				// Get database type at the selected index
130
				DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
131

  
132
				addDatabasePage(type);
133
				setDataBaseTypeSelected(true);
134
				checkPageComplete();
135
			}
136
		});
137

  
138
		// make the composite the wizard pages control
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff