2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.datasource
;
12 import java
.util
.ArrayList
;
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
;
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
;
46 * List of database types comes from {@link eu.etaxonomy.cdm.database.DatabaseTypeEnum}.
52 public class CdmDataSourceDialog
extends Dialog
{
53 private static final Logger logger
= Logger
.getLogger(CdmDataSourceDialog
.class);
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
;
65 private ArrayList
<DatabaseTypeEnum
> databaseTypes
;
67 private ICdmDataSource dataSource
= null;
73 public CdmDataSourceDialog(Shell parentShell
) {
78 * Create contents of the dialog
82 protected Control
createDialogArea(Composite parent
) {
85 container
= (Composite
) super.createDialogArea(parent
);
86 final GridLayout gridLayout
= new GridLayout();
87 gridLayout
.numColumns
= 2;
88 container
.setLayout(gridLayout
);
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
);
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();
103 return super.getText(element
);
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 >>");
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
) {
115 ((IStructuredSelection
) event
.getSelection()).
117 if (dataSource
instanceof ICdmDataSource
) {
118 showDatasource((ICdmDataSource
) dataSource
);
120 showDatasource(null);
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
);
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));
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();
147 // Create listener to display database type-specific config options
148 databaseTypeCombo
.addSelectionListener(new SelectionAdapter() {
149 public void widgetSelected(SelectionEvent e
) {
151 // Get database type at the selected index
152 DatabaseTypeEnum type
= databaseTypes
.get(databaseTypeCombo
.getSelectionIndex());
154 // Display type-specific input fields
155 showDatabaseConfig(dataSource
, type
);
159 // Hide editable fields until a dataSource is chosen
160 editDatasourceComposite
.setVisible(false);
162 // createMySqlForm(null);
167 private void showDatasource(ICdmDataSource dataSource
) {
169 // Put dataSource in field
170 this.dataSource
= dataSource
;
172 // Make editable fields visible
173 editDatasourceComposite
.setVisible(true);
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());
181 datasourceNameText
.setText("");
182 datasourceNameText
.setEditable(true);
183 databaseTypeCombo
.deselectAll();
184 databaseTypeCombo
.clearSelection();
189 private void showDatabaseConfig(ICdmDataSource dataSource
, DatabaseTypeEnum type
) {
190 if (type
.equals(DatabaseTypeEnum
.MySQL
)) {
191 createMySqlForm(dataSource
);
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.");
199 private void selectDatabaseType(DatabaseTypeEnum type
) {
203 // Go through types list until match is found
204 for (DatabaseTypeEnum typeInList
: databaseTypes
) {
205 if (typeInList
.equals(type
)) {
207 // Set combo to the type
208 databaseTypeCombo
.select(i
);
214 // Display type-specific input fields
215 showDatabaseConfig(dataSource
, type
);
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.");
223 private void populateComboBoxItems() {
225 // Init DB types; if already init'ed, clear
226 if (databaseTypes
== null) {
227 databaseTypes
= new ArrayList
<DatabaseTypeEnum
>();
229 databaseTypes
.clear();
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
);
240 * Create contents of the button bar
244 protected void createButtonsForButtonBar(Composite parent
) {
245 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
247 createButton(parent
, IDialogConstants
.CANCEL_ID
,
248 IDialogConstants
.CANCEL_LABEL
, false);
252 * Return the initial size of the dialog
255 protected Point
getInitialSize() {
256 return new Point(500, 375);
259 private void createMySqlForm(ICdmDataSource dataSource
) {
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
);
269 // Create login label
270 final Label loginNameLabel
= new Label(authenticationGroup
, SWT
.NONE
);
271 loginNameLabel
.setText("Login Name:");
273 // Create login input
274 loginText
= new Text(authenticationGroup
, SWT
.BORDER
);
275 loginText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
277 // Create password label
278 final Label passwordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
279 passwordLabel
.setText("Password:");
281 // Create password input
282 passwordText
= new Text(authenticationGroup
, SWT
.BORDER
);
283 passwordText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
285 // Create save password label
286 final Label savePasswordLabel
= new Label(authenticationGroup
, SWT
.NONE
);
287 savePasswordLabel
.setText("Save password:");
289 // Create save password checkbox
290 final Button button
= new Button(authenticationGroup
, SWT
.CHECK
);
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
);
302 final Label hostLabel
= new Label(locationGroup
, SWT
.NONE
);
303 hostLabel
.setText("Host:");
306 hostText
= new Text(locationGroup
, SWT
.BORDER
);
307 hostText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
310 final Label portLabel
= new Label(locationGroup
, SWT
.NONE
);
311 portLabel
.setText("Port:");
314 portText
= new Text(locationGroup
, SWT
.BORDER
);
315 portText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
317 // Create database label
318 final Label databaseLabel
= new Label(locationGroup
, SWT
.NONE
);
319 databaseLabel
.setText("Database:");
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
);
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");
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());