Commit before deleting CdmDataSourceDialog_
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / datasource / CdmDataSourceDialog.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;
11
12 import java.util.ArrayList;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.ArrayContentProvider;
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.IDoubleClickListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.viewers.ListViewer;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.CCombo;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Group;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Text;
38
39 import eu.etaxonomy.cdm.database.CdmDataSource;
40 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
41 import eu.etaxonomy.cdm.database.ICdmDataSource;
42 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
43 import eu.etaxonomy.taxeditor.UiUtil;
44 import eu.etaxonomy.taxeditor.model.CdmUtil;
45 /**
46 * List of database types comes from {@link eu.etaxonomy.cdm.database.DatabaseTypeEnum}.
47 *
48 * @author p.ciardelli
49 * @created 11.12.2008
50 * @version 1.0
51 */
52 public class CdmDataSourceDialog extends Dialog {
53 private static final Logger logger = Logger.getLogger(CdmDataSourceDialog.class);
54
55 private Text databaseText;
56 private Text portText;
57 private Text hostText;
58 private Text passwordText;
59 private Text loginText;
60 private Text datasourceNameText;
61 private CCombo databaseTypeCombo;
62 private Composite editDatasourceComposite;
63 private Composite container;
64
65 private ArrayList<DatabaseTypeEnum> databaseTypes;
66
67 private ICdmDataSource dataSource = null;
68
69 /**
70 * Create the dialog
71 * @param parentShell
72 */
73 public CdmDataSourceDialog(Shell parentShell) {
74 super(parentShell);
75 }
76
77 /**
78 * Create contents of the dialog
79 * @param parent
80 */
81 @Override
82 protected Control createDialogArea(Composite parent) {
83
84 // Create
85 container = (Composite) super.createDialogArea(parent);
86 final GridLayout gridLayout = new GridLayout();
87 gridLayout.numColumns = 2;
88 container.setLayout(gridLayout);
89
90 // Create and style list of datasources
91 final ListViewer dataSourceViewer = new ListViewer(container, SWT.BORDER);
92 final GridData gd_list = new GridData(SWT.LEFT, SWT.FILL, false, true);
93 gd_list.widthHint = 162;
94 dataSourceViewer.getList().setLayoutData(gd_list);
95
96 // Set providers for dataSource list
97 dataSourceViewer.setContentProvider(new ArrayContentProvider());
98 dataSourceViewer.setLabelProvider(new LabelProvider() {
99 public String getText(Object element) {
100 if (element instanceof ICdmDataSource) {
101 return ((ICdmDataSource) element).getName();
102 }
103 return super.getText(element);
104 }
105 });
106
107 // Populate the list with saved datasources and an entry for "new dataSource"
108 dataSourceViewer.setInput(TaxEditorPlugin.getDefault().getDataSources());
109 dataSourceViewer.add("<< Add data source >>");
110
111 // Double-clicking on a dataSource in the viewer puts it in the editable area
112 dataSourceViewer.addDoubleClickListener(new IDoubleClickListener() {
113 public void doubleClick(DoubleClickEvent event) {
114 Object dataSource =
115 ((IStructuredSelection) event.getSelection()).
116 getFirstElement();
117 if (dataSource instanceof ICdmDataSource) {
118 showDatasource((ICdmDataSource) dataSource);
119 } else {
120 showDatasource(null);
121 }
122 }
123 });
124
125 // Create editDatasourceComposite to display a dataSource's name and type
126 editDatasourceComposite = new Composite(container, SWT.NONE);
127 final GridData gd_composite = new GridData(SWT.FILL, SWT.TOP, true, true);
128 editDatasourceComposite.setLayoutData(gd_composite);
129 final GridLayout gridLayout_1 = new GridLayout();
130 gridLayout_1.numColumns = 2;
131 editDatasourceComposite.setLayout(gridLayout_1);
132
133 // Create label and input for dataSource name
134 final Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
135 datasourceNameLabel.setText("Datasource Name:");
136 datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
137 datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
138
139 // Create label and dropdown for database type
140 final Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
141 databaseTypeLabel.setText("Database Type:");
142 databaseTypeCombo = new CCombo(editDatasourceComposite, SWT.BORDER);
143 final GridData gd_list_1 = new GridData(SWT.FILL, SWT.CENTER, false, false);
144 databaseTypeCombo.setLayoutData(gd_list_1);
145 populateComboBoxItems();
146
147 // Create listener to display database type-specific config options
148 databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
149 public void widgetSelected(SelectionEvent e) {
150
151 // Get database type at the selected index
152 DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
153
154 // Display type-specific input fields
155 showDatabaseConfig(dataSource, type);
156 }
157 });
158
159 // Hide editable fields until a dataSource is chosen
160 editDatasourceComposite.setVisible(false);
161
162 // createMySqlForm(null);
163 //
164 return container;
165 }
166
167 private void showDatasource(ICdmDataSource dataSource) {
168
169 // Put dataSource in field
170 this.dataSource = dataSource;
171
172 // Make editable fields visible
173 editDatasourceComposite.setVisible(true);
174
175 // Populate name and database type if this is a non-null dataSource
176 if (dataSource != null) {
177 datasourceNameText.setText(dataSource.getName());
178 datasourceNameText.setEditable(false);
179 selectDatabaseType(dataSource.getDatabaseType());
180 } else {
181 datasourceNameText.setText("");
182 datasourceNameText.setEditable(true);
183 databaseTypeCombo.deselectAll();
184 databaseTypeCombo.clearSelection();
185 }
186
187 }
188
189 private void showDatabaseConfig(ICdmDataSource dataSource, DatabaseTypeEnum type) {
190 if (type.equals(DatabaseTypeEnum.MySQL)) {
191 createMySqlForm(dataSource);
192 } else {
193 // TODO Intrusive - change to text
194 MessageDialog.openInformation(UiUtil.getShell(), "Database not yet implemented",
195 "Creation of datasources using a(n) '" + type.getName() + "' database not yet implemented.");
196 }
197 }
198
199 private void selectDatabaseType(DatabaseTypeEnum type) {
200
201 int i = 0;
202
203 // Go through types list until match is found
204 for (DatabaseTypeEnum typeInList : databaseTypes ) {
205 if (typeInList.equals(type)) {
206
207 // Set combo to the type
208 databaseTypeCombo.select(i);
209 break;
210 }
211 i++;
212 }
213
214 // Display type-specific input fields
215 showDatabaseConfig(dataSource, type);
216
217 // // If we're this far, database type was not in the list
218 // MessageDialog.openInformation(UiUtil.getShell(), "Database not yet implemented",
219 // "This dataSource expects a database type '" + type.getName() +
220 // "', which is not in the Taxonomic Editor's list of expected databases.");
221 }
222
223 private void populateComboBoxItems() {
224
225 // Init DB types; if already init'ed, clear
226 if (databaseTypes == null) {
227 databaseTypes = new ArrayList<DatabaseTypeEnum>();
228 } else {
229 databaseTypes.clear();
230 }
231
232 // Add types to the type drop-down and to the types collection
233 for (DatabaseTypeEnum type : DatabaseTypeEnum.getAllTypes()) {
234 databaseTypeCombo.add(type.getName());
235 databaseTypes.add(type);
236 }
237 }
238
239 /**
240 * Create contents of the button bar
241 * @param parent
242 */
243 @Override
244 protected void createButtonsForButtonBar(Composite parent) {
245 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
246 true);
247 createButton(parent, IDialogConstants.CANCEL_ID,
248 IDialogConstants.CANCEL_LABEL, false);
249 }
250
251 /**
252 * Return the initial size of the dialog
253 */
254 @Override
255 protected Point getInitialSize() {
256 return new Point(500, 375);
257 }
258
259 private void createMySqlForm(ICdmDataSource dataSource) {
260
261 // Create group for authentication fields
262 final Group authenticationGroup = new Group(editDatasourceComposite, SWT.NONE);
263 authenticationGroup.setText("Authentication");
264 authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
265 final GridLayout gridLayout_2 = new GridLayout();
266 gridLayout_2.numColumns = 2;
267 authenticationGroup.setLayout(gridLayout_2);
268
269 // Create login label
270 final Label loginNameLabel = new Label(authenticationGroup, SWT.NONE);
271 loginNameLabel.setText("Login Name:");
272
273 // Create login input
274 loginText = new Text(authenticationGroup, SWT.BORDER);
275 loginText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
276
277 // Create password label
278 final Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
279 passwordLabel.setText("Password:");
280
281 // Create password input
282 passwordText = new Text(authenticationGroup, SWT.BORDER);
283 passwordText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
284
285 // Create save password label
286 final Label savePasswordLabel = new Label(authenticationGroup, SWT.NONE);
287 savePasswordLabel.setText("Save password:");
288
289 // Create save password checkbox
290 final Button button = new Button(authenticationGroup, SWT.CHECK);
291
292 // Create group for location fields
293 final Group locationGroup = new Group(editDatasourceComposite, SWT.NONE);
294 locationGroup.setText("Location");
295 final GridData gd_locationGroup = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
296 locationGroup.setLayoutData(gd_locationGroup);
297 final GridLayout gridLayout_3 = new GridLayout();
298 gridLayout_3.numColumns = 2;
299 locationGroup.setLayout(gridLayout_3);
300
301 // Create host label
302 final Label hostLabel = new Label(locationGroup, SWT.NONE);
303 hostLabel.setText("Host:");
304
305 // Create host input
306 hostText = new Text(locationGroup, SWT.BORDER);
307 hostText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
308
309 // Create port label
310 final Label portLabel = new Label(locationGroup, SWT.NONE);
311 portLabel.setText("Port:");
312
313 // Create port input
314 portText = new Text(locationGroup, SWT.BORDER);
315 portText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
316
317 // Create database label
318 final Label databaseLabel = new Label(locationGroup, SWT.NONE);
319 databaseLabel.setText("Database:");
320
321 // Create database input
322 databaseText = new Text(locationGroup, SWT.BORDER);
323 databaseText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
324 new Label(editDatasourceComposite, SWT.NONE);
325
326 // Create save button
327 final Button saveButton = new Button(editDatasourceComposite, SWT.NONE);
328 final GridData gd_saveButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
329 gd_saveButton.widthHint = 76;
330 saveButton.setLayoutData(gd_saveButton);
331 saveButton.setText("Save");
332
333 // Populate fields with datasource data
334 if (dataSource != null) {
335 loginText.setText(dataSource.getName());
336 // passwordText.setText(dataSource.getP);
337 hostText.setText(dataSource.getServer());
338 portText.setText(String.valueOf(dataSource.getPort()));
339 databaseText.setText(dataSource.getDatabase());
340 }
341
342 // Redraw
343 container.layout();
344 }
345 }