Data source dialog now has saving and creation of new names.
authorp.ciardelli <p.ciardelli@localhost>
Wed, 17 Dec 2008 15:32:39 +0000 (15:32 +0000)
committerp.ciardelli <p.ciardelli@localhost>
Wed, 17 Dec 2008 15:32:39 +0000 (15:32 +0000)
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/TaxEditorPlugin.java
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/UiUtil.java
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/datasource/CdmDataSourceDialog.java
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.java
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/datasource/ICdmDataSourceRepository.java
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/model/CdmUtil.java
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/navigation/TaxonomicTreeView.java

index 96f80dfe8fb14b3cda5c1e17c25161726abfb7a8..4ccc3e49d3436add63a9c6e252abd4a630a90a04 100644 (file)
@@ -94,18 +94,6 @@ public class TaxEditorPlugin extends AbstractUIPlugin {
 //     private DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;\r
        private DbSchemaValidation dbSchemaValidation = DbSchemaValidation.UPDATE;\r
 \r
-       /**\r
-        * The constructor\r
-        */\r
-       public TaxEditorPlugin() {\r
-//             logger.fatal("Fatal");\r
-//             logger.error("Error");\r
-//             logger.debug("Debug");\r
-//             logger.info("Info");\r
-//             logger.warn("Warn");\r
-//             logger.trace("Trace");  \r
-       }\r
-\r
        /*\r
         * (non-Javadoc)\r
         * \r
@@ -115,7 +103,7 @@ public class TaxEditorPlugin extends AbstractUIPlugin {
                super.start(context);\r
                plugin = this;\r
                                \r
-               // Check in the preferences store whether the application (i.e. its default\r
+               // Check in the prefs store whether the application (i.e. its default\r
                //      data source) has been initialized\r
                boolean initialized = false;\r
                if (getPreferenceStore().contains(ITaxEditorConstants.INITIALIZED)) {\r
@@ -123,13 +111,14 @@ public class TaxEditorPlugin extends AbstractUIPlugin {
                                        ITaxEditorConstants.INITIALIZED);\r
                }\r
        \r
-               // If not, set validation to CREATE\r
+               // If not ...\r
                if (!initialized) {\r
                        \r
+                       // ... set validation to CREATE\r
                        logger.warn("Initializing datastore");\r
                        dbSchemaValidation = DbSchemaValidation.CREATE;\r
                        \r
-                       // Note in Preferences that application has been initialized\r
+                       // ... and note in prefs that application has been initialized\r
                        getPreferenceStore()\r
                                        .setValue(ITaxEditorConstants.INITIALIZED, true);\r
                }\r
@@ -137,7 +126,7 @@ public class TaxEditorPlugin extends AbstractUIPlugin {
                // Initialize application controller\r
                cdmApp = initCdmApp();\r
                \r
-               // Set application controller for objects that need it \r
+               // Set application controller for objects that use it \r
                CdmTransactionController.setCdmApplicationController(cdmApp);\r
                CdmDataSourceRepository.getDefault().setCdmApplicationController(cdmApp);\r
                \r
@@ -156,7 +145,6 @@ public class TaxEditorPlugin extends AbstractUIPlugin {
         */\r
        public void stop(BundleContext context) throws Exception {\r
                CdmTransactionController.commitTransaction();\r
-//             commitTransaction();\r
                plugin = null;\r
                super.stop(context);\r
                \r
@@ -184,12 +172,15 @@ public class TaxEditorPlugin extends AbstractUIPlugin {
 \r
        private CdmApplicationController initCdmApp() {\r
                \r
+               // Get the "current", i.e. most recently used, data source\r
                ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().\r
                                getCurrentDataSource();\r
                \r
+               // \r
                try {\r
                        cdmApp = CdmApplicationController.NewInstance(cdmDatasource, dbSchemaValidation);\r
                } catch (DataSourceNotFoundException e) {\r
+                       // TODO user-friendly failure here\r
                        e.printStackTrace();\r
                } catch (TermNotFoundException e) {\r
                        e.printStackTrace();\r
index 8518d6684d3bf401221103ca9a135ef875848096..a191f16d3a8dce01e368233a4a1c0b41af6f662f 100644 (file)
@@ -135,10 +135,13 @@ public class UiUtil {
                return taxonEditors;\r
        }\r
 \r
-       public static void closeOpenTaxonEditors() {\r
+       public static boolean closeOpenTaxonEditors() {\r
                for (IEditorPart editor : getOpenTaxonEditors()) {\r
-                       editor.dispose();\r
+                       if (!getActivePage().closeEditor(editor, true)) {\r
+                               return false;\r
+                       }\r
                }\r
+               return true;\r
        }\r
        \r
        public static IEditorPart getEditorByTaxon(Taxon taxon)\r
index 0c3d47acd1966caaf4518783e7d181762e6f5dce..cb75e77a536c426f13ae7a5ef1760b3a0907ab61 100644 (file)
@@ -12,6 +12,8 @@ package eu.etaxonomy.taxeditor.datasource;
 import java.util.ArrayList;\r
 \r
 import org.apache.log4j.Logger;\r
+import org.eclipse.jface.action.Action;\r
+import org.eclipse.jface.action.MenuManager;\r
 import org.eclipse.jface.dialogs.Dialog;\r
 import org.eclipse.jface.dialogs.IDialogConstants;\r
 import org.eclipse.jface.dialogs.MessageDialog;\r
@@ -20,7 +22,7 @@ import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;\r
 import org.eclipse.jface.viewers.IStructuredSelection;\r
 import org.eclipse.jface.viewers.LabelProvider;\r
-import org.eclipse.jface.viewers.ListViewer;\r
+import org.eclipse.jface.viewers.TableViewer;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.custom.CCombo;\r
 import org.eclipse.swt.events.MouseAdapter;\r
@@ -35,12 +37,15 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;\r
 import org.eclipse.swt.widgets.Group;\r
 import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Menu;\r
 import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Table;\r
 import org.eclipse.swt.widgets.Text;\r
 \r
+import eu.etaxonomy.cdm.common.CdmUtils;\r
+import eu.etaxonomy.cdm.database.CdmPersistentDataSource;\r
 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;\r
 import eu.etaxonomy.cdm.database.ICdmDataSource;\r
-import eu.etaxonomy.taxeditor.TaxEditorPlugin;\r
 import eu.etaxonomy.taxeditor.UiUtil;\r
 /**\r
  * List of database types comes from {@link eu.etaxonomy.cdm.database.DatabaseTypeEnum}.\r
@@ -54,27 +59,26 @@ public class CdmDataSourceDialog extends Dialog {
        \r
        private Text databaseText;\r
        private Text portText;\r
-       private Text hostText;\r
+       private Text serverText;\r
        private Text passwordText;\r
        private Text databaseNameText;\r
        private Text datasourceNameText;\r
        private CCombo databaseTypeCombo;\r
-       private Composite editDatasourceComposite;\r
+       private Text usernameText;\r
+       \r
        private Composite container;\r
-\r
+       private Composite editDatasourceComposite;\r
        private Group authenticationGroup;\r
        private Group locationGroup;\r
-       \r
-       private ArrayList<DatabaseTypeEnum> databaseTypes;\r
-\r
-       private ICdmDataSource dataSource = null;\r
-\r
        private Button saveButton;\r
+       private Composite dbConfigComposite;\r
 \r
-       private Composite connectionDataComposite;\r
-\r
+       private ArrayList<DatabaseTypeEnum> databaseTypes;\r
+       private ICdmDataSource dataSource = null;\r
 \r
+       private TableViewer dataSourceViewer;\r
 \r
+       \r
        /**\r
         * Create the dialog\r
         * @param parentShell\r
@@ -96,13 +100,13 @@ public class CdmDataSourceDialog extends Dialog {
                gridLayout.numColumns = 2;\r
                container.setLayout(gridLayout);\r
 \r
-               // Create and style list of datasources\r
-               ListViewer dataSourceViewer = new ListViewer(container, SWT.BORDER);\r
-               GridData listLayout = new GridData(SWT.LEFT, SWT.FILL, false, true);\r
-               listLayout.widthHint = 162;\r
-               dataSourceViewer.getList().setLayoutData(listLayout);\r
+               // Create viewer for datasource list\r
+               dataSourceViewer = new TableViewer(container, SWT.BORDER);\r
+               GridData viewerLayout = new GridData(SWT.LEFT, SWT.FILL, false, true);\r
+               viewerLayout.widthHint = 162;\r
+               dataSourceViewer.getTable().setLayoutData(viewerLayout);\r
                \r
-               // Set providers for dataSource list\r
+               // Set providers for datasource list\r
                dataSourceViewer.setContentProvider(new ArrayContentProvider());\r
                dataSourceViewer.setLabelProvider(new LabelProvider() {\r
                        public String getText(Object element) {\r
@@ -113,9 +117,8 @@ public class CdmDataSourceDialog extends Dialog {
                        }\r
                });\r
                \r
-               // Populate the list with saved datasources and an entry for "new dataSource"           \r
-               dataSourceViewer.setInput(CdmDataSourceRepository.getDefault().getAll());\r
-               dataSourceViewer.add("<< Add data source >>");\r
+               // Add data sources to viewer           \r
+               refreshDataSourceViewer();\r
                \r
                // Double-clicking on a dataSource in the viewer puts it in the editable area\r
                dataSourceViewer.addDoubleClickListener(new IDoubleClickListener() {\r
@@ -136,6 +139,30 @@ public class CdmDataSourceDialog extends Dialog {
                                }\r
                        }\r
                });\r
+               \r
+               // Create "remove datasource" rt-click menu\r
+               Table dataSourceControl = dataSourceViewer.getTable();\r
+               final MenuManager manager = new MenuManager();\r
+               final Menu menu = manager.createContextMenu(dataSourceControl);\r
+               dataSourceControl.setMenu(menu);\r
+               \r
+               // Add right-click menu to delete datasources\r
+               dataSourceControl.addSelectionListener(new SelectionAdapter() {\r
+                       public void widgetSelected(SelectionEvent e) {\r
+                               \r
+                               // Get content of widget's data field\r
+                               Object data = e.item.getData();\r
+                               \r
+                               // Clear menu\r
+                               manager.removeAll();\r
+                                       \r
+                               // Only persistent data sources can be removed -\r
+                               //      H2 database and "New datasource" entry remain untouched\r
+                               if (data instanceof CdmPersistentDataSource) {\r
+                                       manager.add(new RemoveDataSourceAction((CdmPersistentDataSource) data));\r
+                               }\r
+                       }\r
+               });\r
 \r
                // Create editDatasourceComposite to display a dataSource's name and type\r
                editDatasourceComposite = new Composite(container, SWT.NONE);\r
@@ -185,6 +212,9 @@ public class CdmDataSourceDialog extends Dialog {
                // Make editable fields visible\r
                editDatasourceComposite.setVisible(true);\r
                \r
+               // Remove any existing db config data\r
+               removeDbConfigFields();\r
+               \r
                // Populate name and database type if this is a non-null dataSource\r
                if (dataSource != null) {\r
                        datasourceNameText.setText(dataSource.getName());\r
@@ -194,8 +224,7 @@ public class CdmDataSourceDialog extends Dialog {
                } else {\r
                        datasourceNameText.setText("");\r
                        datasourceNameText.setEnabled(true);\r
-                       databaseTypeCombo.deselectAll();\r
-                       databaseTypeCombo.clearSelection();\r
+                       databaseTypeCombo.setText(""); // for some reason, .deselectAll and .clearSelection don't work as advertised\r
                        databaseTypeCombo.setEnabled(true);\r
                }\r
                \r
@@ -237,11 +266,9 @@ public class CdmDataSourceDialog extends Dialog {
        \r
        private void populateComboBoxItems() {\r
                \r
-               // Init DB types; if already init'ed, clear\r
+               // Init DB types\r
                if (databaseTypes == null) {\r
                        databaseTypes = new ArrayList<DatabaseTypeEnum>();\r
-               } else {\r
-                       databaseTypes.clear();\r
                }\r
                \r
                // Add types to the type drop-down and to the types collection\r
@@ -270,34 +297,35 @@ public class CdmDataSourceDialog extends Dialog {
        protected Point getInitialSize() {\r
                return new Point(500, 375);\r
        }\r
+\r
+       private void removeDbConfigFields() {\r
+               // Init db config area by removing all children\r
+               if (dbConfigComposite != null) {\r
+                       for (Control child : dbConfigComposite.getChildren()) {\r
+                               child.dispose();\r
+                       }\r
+               }\r
+       }\r
+       \r
+       private void createH2Form() {\r
                \r
+       }\r
+       \r
        private void createMySqlForm(final ICdmDataSource dataSource) {\r
 \r
-               // Dispose previous form\r
-//             if (authenticationGroup != null) {\r
-//                     authenticationGroup.dispose();\r
-//             }\r
-//             if (locationGroup != null) {\r
-//                     locationGroup.dispose();\r
-//             }\r
-//             if (saveButton != null) {\r
-//                     saveButton.dispose();\r
-//             }\r
-               if (connectionDataComposite != null) {\r
-                       connectionDataComposite.dispose();\r
-                       editDatasourceComposite.layout();\r
+               // Create composite for db config as necessary\r
+               if (dbConfigComposite == null) {\r
+                       dbConfigComposite = new Composite(editDatasourceComposite, SWT.NONE);\r
+                       dbConfigComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));\r
+                       GridLayout formLayout = new GridLayout();\r
+                       formLayout.numColumns = 2;\r
+                       dbConfigComposite.setLayout(formLayout);\r
                }\r
-               connectionDataComposite = new Composite(editDatasourceComposite, SWT.NONE);\r
-               connectionDataComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));\r
-               GridLayout formLayout = new GridLayout();\r
-               formLayout.numColumns = 2;\r
-               connectionDataComposite.setLayout(formLayout);\r
                \r
                // Create group composite for authentication data\r
-               authenticationGroup = new Group(connectionDataComposite, SWT.NONE);\r
+               authenticationGroup = new Group(dbConfigComposite, SWT.NONE);\r
                authenticationGroup.setText("Authentication");\r
                authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
-//             authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));\r
                GridLayout authenticationLayout = new GridLayout();\r
                authenticationLayout.numColumns = 2;\r
                authenticationGroup.setLayout(authenticationLayout);\r
@@ -309,6 +337,14 @@ public class CdmDataSourceDialog extends Dialog {
                // Create database name input\r
                databaseNameText = new Text(authenticationGroup, SWT.BORDER);\r
                databaseNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+\r
+               // Create username label\r
+               Label usernameLabel = new Label(authenticationGroup, SWT.NONE);\r
+               usernameLabel.setText("User Name:");\r
+\r
+               // Create username input\r
+               usernameText = new Text(authenticationGroup, SWT.BORDER);\r
+               usernameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
                \r
                // Create password label\r
                Label passwordLabel = new Label(authenticationGroup, SWT.NONE);\r
@@ -326,21 +362,20 @@ public class CdmDataSourceDialog extends Dialog {
                Button button = new Button(authenticationGroup, SWT.CHECK);\r
 \r
                // Create group composite for location data \r
-               locationGroup = new Group(connectionDataComposite, SWT.NONE);\r
+               locationGroup = new Group(dbConfigComposite, SWT.NONE);\r
                locationGroup.setText("Location");\r
                locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
-//             locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));\r
                GridLayout locationLayout = new GridLayout();\r
                locationLayout.numColumns = 2;\r
                locationGroup.setLayout(locationLayout);\r
 \r
                // Create host label\r
-               Label hostLabel = new Label(locationGroup, SWT.NONE);\r
-               hostLabel.setText("Host:");\r
+               Label serverLabel = new Label(locationGroup, SWT.NONE);\r
+               serverLabel.setText("Host:");\r
 \r
                // Create host input\r
-               hostText = new Text(locationGroup, SWT.BORDER);\r
-               hostText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+               serverText = new Text(locationGroup, SWT.BORDER);\r
+               serverText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
 \r
                // Create port label\r
                Label portLabel = new Label(locationGroup, SWT.NONE);\r
@@ -351,7 +386,7 @@ public class CdmDataSourceDialog extends Dialog {
                portText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
 \r
                // Create save button\r
-               saveButton = new Button(connectionDataComposite, SWT.NONE);\r
+               saveButton = new Button(dbConfigComposite, SWT.NONE);\r
                final GridData saveButtonLayout = new GridData(SWT.RIGHT, SWT.CENTER, false, false);\r
                saveButtonLayout.widthHint = 76;\r
                saveButton.setLayoutData(saveButtonLayout);\r
@@ -360,20 +395,90 @@ public class CdmDataSourceDialog extends Dialog {
                // Save data to datasource on mouse click\r
                saveButton.addMouseListener(new MouseAdapter() {\r
                        public void mouseUp(MouseEvent e) {\r
-                               \r
-                               \r
+                               saveDbConfig();\r
                        }\r
                });\r
                \r
                // Populate fields with datasource data\r
                if (dataSource != null) {\r
-                       databaseNameText.setText(dataSource.getDatabase());\r
+                       databaseNameText.setText(CdmUtils.Nz(dataSource.getDatabase()));\r
 //                     passwordText.setText(dataSource.getP);\r
-                       hostText.setText(dataSource.getServer());\r
+                       serverText.setText(CdmUtils.Nz(dataSource.getServer()));\r
                        portText.setText(String.valueOf(dataSource.getPort()));                 \r
                }\r
                \r
                // Redraw\r
-               container.layout();\r
+               container.layout(true, true);\r
+       }\r
+       \r
+\r
+       private void saveDbConfig() {\r
+               \r
+               // Get field values\r
+               DatabaseTypeEnum databaseType = databaseTypes.get(databaseTypeCombo.getSelectionIndex());\r
+               String dataSourceName = datasourceNameText.getText();\r
+               String server = serverText.getText();\r
+               String database = databaseNameText.getText();\r
+               String username = usernameText.getText(); \r
+               String password = passwordText.getText();\r
+               \r
+               try {\r
+                       \r
+                       // Try casting port to an integer\r
+                       Integer port = new Integer(portText.getText());\r
+                       \r
+                       logger.warn(port);\r
+                       \r
+                       // Save with port\r
+                       CdmDataSourceRepository.getDefault().save (\r
+                                       dataSourceName, \r
+                                       databaseType, \r
+                                       server, \r
+                                       database, \r
+                                       port, \r
+                                       username, \r
+                                       password);\r
+                       \r
+               } catch (ClassCastException e) {\r
+                       \r
+                       // If cast failed, save without port\r
+                       CdmDataSourceRepository.getDefault().save (\r
+                                       dataSourceName, \r
+                                       databaseType, \r
+                                       server, \r
+                                       database,\r
+                                       username, \r
+                                       password);\r
+               }\r
+               \r
+               // Refresh list of data sources to reflect changes\r
+               refreshDataSourceViewer();\r
+       }\r
+       \r
+       private void refreshDataSourceViewer() {\r
+               \r
+               // Populate the list with saved datasources and an entry for "new dataSource"\r
+               dataSourceViewer.setInput(\r
+                               CdmDataSourceRepository.getDefault().getAll());\r
+               dataSourceViewer.add("<< Add data source >>");\r
+       }\r
+\r
+       private class RemoveDataSourceAction extends Action {\r
+               \r
+               private CdmPersistentDataSource dataSource;\r
+\r
+               RemoveDataSourceAction(CdmPersistentDataSource dataSource) {\r
+                       setText("Remove '" + dataSource.getName() + "' from saved data sources.");\r
+                       this.dataSource = dataSource;\r
+               }\r
+               \r
+               public void run() {\r
+                       \r
+                       // Delete from persistent data sources\r
+                       CdmDataSourceRepository.getDefault().delete(dataSource);\r
+                       \r
+                       // Refresh list of data sources to reflect changes\r
+                       refreshDataSourceViewer();\r
+               }\r
        }\r
 }\r
index cef9027bbb3950b17b70c2eabfaaeb6b9b003d0b..f8245bf75f4d73172d47a0f096ec0e07e1e23809 100644 (file)
@@ -21,6 +21,7 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
 import eu.etaxonomy.cdm.api.application.CdmApplicationController;\r
 import eu.etaxonomy.cdm.database.CdmDataSource;\r
 import eu.etaxonomy.cdm.database.CdmPersistentDataSource;\r
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;\r
 import eu.etaxonomy.cdm.database.ICdmDataSource;\r
 import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;\r
 import eu.etaxonomy.taxeditor.TaxEditorPlugin;\r
@@ -40,19 +41,30 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
                if (repository == null) {\r
                        repository = new CdmDataSourceRepository();\r
                        \r
-                       // Create test data sources\r
-                       CdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(\r
+                       // Create datasource for local H2 database\r
+                       ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(\r
                                        "cdm", "sa", "");\r
-                       CdmDataSource mySqlDataSource = CdmDataSource.NewMySqlInstance(\r
-                                       "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");\r
-                       \r
-                       // Add test data sources to repository\r
                        repository.add(h2DataSource);\r
-                       repository.add(mySqlDataSource);\r
+                                               \r
+                       // Add stored datasources to repository\r
+                       for (ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()) {\r
+                               repository.add(dataSource);\r
+                       }\r
+                       \r
+                       // Get default datasource\r
+                       ICdmDataSource defaultDataSource = CdmPersistentDataSource.NewDefaultInstance();\r
+                       \r
+                       // If none specified, use local H2 database \r
+                       if (defaultDataSource == null) {\r
+                               defaultDataSource = h2DataSource;\r
+                       }\r
                        \r
-                       // Set current data source\r
-//                     repository.setCurrentDataSource(h2DataSource);\r
-                       repository.setCurrentDataSource(mySqlDataSource);\r
+                       // Set repository's current datasource\r
+                       repository.setCurrentDataSource(defaultDataSource);\r
+                       \r
+//                     defaultDataSource = CdmPersistentDataSource.save("mysql_cichorieae", DatabaseTypeEnum.MySQL, "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");\r
+//                     CdmDataSource mySqlDataSource = CdmDataSource.NewMySqlInstance(\r
+//                                     "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");\r
                }\r
                return repository;\r
        }\r
@@ -62,24 +74,23 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
                return getDataSources().add(dataSource);\r
        }\r
 \r
+\r
        @Override\r
-       public List<ICdmDataSource> getAll() {\r
-               return getDataSources();\r
+       public boolean delete(CdmPersistentDataSource dataSource) {\r
+               CdmPersistentDataSource.delete(dataSource);\r
+               return remove(dataSource);\r
        }\r
 \r
        @Override\r
-       public boolean save(CdmPersistentDataSource dataSource) {\r
-               // TODO Auto-generated method stub\r
-               return false;\r
+       public boolean remove(ICdmDataSource dataSource) {\r
+               return getDataSources().remove(dataSource);\r
        }\r
        \r
-\r
        @Override\r
-       public void setCdmApplicationController(\r
-                       CdmApplicationController cdmAppController) {\r
-               this.cdmAppController = cdmAppController;               \r
+       public List<ICdmDataSource> getAll() {\r
+               return getDataSources();\r
        }\r
-       \r
+               \r
        @Override\r
        public ICdmDataSource getCurrentDataSource() {\r
                if (currentDataSource == null) {\r
@@ -99,6 +110,12 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
                return true;\r
        }\r
 \r
+       @Override\r
+       public void setCdmApplicationController(\r
+                       CdmApplicationController cdmAppController) {\r
+               this.cdmAppController = cdmAppController;               \r
+       }\r
+       \r
        private CdmApplicationController getCdmAppController() {\r
                if (cdmAppController == null) {\r
                        throw new IllegalStateException("CdmApplicationController not set.");\r
@@ -114,6 +131,14 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
        }\r
                \r
        private boolean changeDataSource(final ICdmDataSource dataSource) {\r
+\r
+               // Close all open editors before showing progress monitor\r
+               if (!UiUtil.closeOpenTaxonEditors()) {\r
+                       \r
+                       // User has canceled operation   \r
+                       return false;\r
+               }\r
+               \r
                try {\r
                        IRunnableWithProgress op = new IRunnableWithProgress() {\r
 \r
@@ -121,9 +146,8 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
                                public void run(IProgressMonitor monitor)\r
                                                throws InvocationTargetException, InterruptedException {\r
                                \r
+                                       // Start the progress bar with 25 steps\r
                                        monitor.beginTask("Changing to a new datasource ...", 25);\r
-                                       \r
-                                       // Close all open editors\r
                                \r
                                        // Clear all session variables\r
                                        CdmUtil.clearLibraryObjects();\r
@@ -140,8 +164,8 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
                                        try {\r
                                                getCdmAppController().changeDataSource(dataSource);\r
                                        } catch (TermNotFoundException e) {\r
-                                               // TODO Auto-generated catch block\r
                                                e.printStackTrace();\r
+                                               throw new InterruptedException();\r
                                        }\r
                                        monitor.worked(10);\r
                                        \r
@@ -166,4 +190,23 @@ public class CdmDataSourceRepository implements ICdmDataSourceRepository {
                }\r
                return true;\r
        }\r
+\r
+       @Override\r
+       public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,\r
+                       String server, String database, int port, String username,\r
+                       String password) {\r
+               CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(\r
+                               dataSourceName, databaseType, server, database, port, username, password);\r
+               add(dataSource);\r
+               return dataSource;\r
+       }\r
+       \r
+       @Override\r
+       public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,\r
+                       String server, String database, String username, String password) {\r
+               CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(\r
+                               dataSourceName, databaseType, server, database, username, password);\r
+               add(dataSource);\r
+               return dataSource;\r
+       }\r
 }
\ No newline at end of file
index eec744a8db763ff1ca067f4eef49af8fa37472ca..0990b36812386864838e18fbf9c0a40bd0e5fd56 100644 (file)
@@ -13,6 +13,7 @@ import java.util.List;
 \r
 import eu.etaxonomy.cdm.api.application.CdmApplicationController;\r
 import eu.etaxonomy.cdm.database.CdmPersistentDataSource;\r
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;\r
 import eu.etaxonomy.cdm.database.ICdmDataSource;\r
 \r
 /**\r
@@ -42,14 +43,57 @@ public interface ICdmDataSourceRepository {
        public boolean add(ICdmDataSource dataSource);\r
        \r
        /**\r
-        * Add <code>dataSource</code> to the list of all\r
-        * persistent CDM dataSources associated with this \r
+        * If <code>dataSourceName</code> already exists as persistent CDM\r
+        * dataSources, it is updated with parameter values.\r
+        * <p>\r
+        * If it does not exist, <code>dataSource</code> will be created. \r
+        * \r
+        * @param dataSourceName\r
+        * @param databaseType\r
+        * @param server\r
+        * @param database\r
+        * @param port\r
+        * @param username\r
+        * @param password\r
+        * @return\r
+        */\r
+       public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType, String server, String database, \r
+                       int port, String username, String password);\r
+\r
+       /**\r
+        * Almost identical to {@link #save(String,DatabaseTypeEnum,String,String,int,String,String)}, \r
+        * except it does not take a <code>port</code>.\r
+        * \r
+        * @param dataSourceName\r
+        * @param databaseType\r
+        * @param server\r
+        * @param database\r
+        * @param username\r
+        * @param password\r
+        * @return\r
+        */\r
+       public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType, String server, String database, \r
+                       String username, String password);\r
+       \r
+       /**\r
+        * Remove <code>dataSource</code> from the list of all\r
+        * CDM dataSources associated with this session\r
+        * of the Taxonomic Editor. \r
+        * \r
+        * @param dataSource\r
+        * @return\r
+        */     \r
+       public boolean remove(ICdmDataSource dataSource);\r
+\r
+       /**\r
+        * Remove <code>dataSource</code> from the list of all\r
+        * <i>persistent</i> CDM dataSources associated with this \r
         * Taxonomic Editor.\r
         *  \r
         * @param dataSource\r
         * @return\r
         */\r
-       public boolean save(CdmPersistentDataSource dataSource);\r
+       public boolean delete(CdmPersistentDataSource dataSource);\r
        \r
        /**\r
         * All data source operations run through a <code>CdmApplicationController</code>.\r
index 3de5e0eb7a53f436addef7ade8ed2f3de7a12196..7cd1bc2c1a954c608c854e0dc47ca989d81584d4 100644 (file)
@@ -9,7 +9,6 @@
 \r
 package eu.etaxonomy.taxeditor.model;\r
 \r
-import java.util.ArrayList;\r
 import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Set;\r
@@ -17,14 +16,11 @@ import java.util.SortedSet;
 \r
 import org.apache.log4j.Logger;\r
 import org.eclipse.jface.dialogs.MessageDialog;\r
-import org.springframework.transaction.TransactionStatus;\r
 \r
 import eu.etaxonomy.cdm.api.service.IDescriptionService;\r
 import eu.etaxonomy.cdm.api.service.INameService;\r
 import eu.etaxonomy.cdm.api.service.IReferenceService;\r
 import eu.etaxonomy.cdm.api.service.ITaxonService;\r
-import eu.etaxonomy.cdm.database.CdmDataSource;\r
-import eu.etaxonomy.cdm.database.ICdmDataSource;\r
 import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;\r
 import eu.etaxonomy.cdm.model.common.TermVocabulary;\r
 import eu.etaxonomy.cdm.model.common.TimePeriod;\r
@@ -46,7 +42,6 @@ import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;\r
 import eu.etaxonomy.taxeditor.TaxEditorPlugin;\r
 import eu.etaxonomy.taxeditor.UiUtil;\r
-import eu.etaxonomy.taxeditor.datasource.ICdmDataSourceRepository;\r
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;\r
 \r
 /**\r
index c3b6c829e37890d88a3689aac7e3ab6294d4df50..492af3e0b84dc89bb1d26cef6fae1617802ca108 100644 (file)
@@ -158,7 +158,34 @@ public class TaxonomicTreeView extends ViewPart {
        }\r
        \r
        private void createDataSourceMenu() {\r
+\r
+               // Create entry to open data source dialog\r
+               new MenuItem(dataSourceMenu, SWT.SEPARATOR);\r
+               final MenuItem newDataSourceMenuItem = new MenuItem(dataSourceMenu, SWT.NONE);\r
+               newDataSourceMenuItem.setText("Edit data sources ...");\r
+               newDataSourceMenuItem.addSelectionListener(new SelectionAdapter() {\r
+                       public void widgetSelected(SelectionEvent event) {\r
+                               Dialog dialog = new CdmDataSourceDialog(UiUtil.getShell());\r
+                               dialog.open();\r
+                               \r
+                               // Refresh menu to reflect any changes in data \r
+                               refreshDataSources();\r
+                       }\r
+               });\r
+               \r
+               // Add data sources to menu\r
+               refreshDataSources();\r
+       }\r
+\r
+       private void refreshDataSources() {\r
                \r
+               // First dispose of any entries that are already there\r
+               for (MenuItem menuItem : dataSourceMenu.getItems()) {\r
+                       if (menuItem.getData() instanceof ICdmDataSource) {\r
+                               menuItem.dispose();\r
+                       }\r
+               }\r
+                               \r
                // Get current datasource and list of all data sources \r
                ICdmDataSource sessionDataSource = CdmDataSourceRepository.getDefault().\r
                                getCurrentDataSource();\r
@@ -167,7 +194,9 @@ public class TaxonomicTreeView extends ViewPart {
                \r
                // Create menu items, putting a check next to the current one\r
                for (ICdmDataSource dataSource : dataSources) {\r
-                       MenuItem item = new MenuItem(dataSourceMenu, SWT.CHECK);\r
+                       \r
+                       // Always add menu item to index 0, i.e. at the beginning of the menu\r
+                       MenuItem item = new MenuItem(dataSourceMenu, SWT.CHECK, 0);\r
                        \r
                        // If this is the current data source, put a check by it\r
                        if (dataSource.equals(sessionDataSource)) {\r
@@ -216,21 +245,8 @@ public class TaxonomicTreeView extends ViewPart {
                                }\r
                        });\r
                }\r
-\r
-               // Create entry to open data source dialog\r
-               new MenuItem(dataSourceMenu, SWT.SEPARATOR);\r
-               final MenuItem newDataSourceMenuItem = new MenuItem(dataSourceMenu, SWT.NONE);\r
-               newDataSourceMenuItem.setText("Edit data sources ...");\r
-               newDataSourceMenuItem.addSelectionListener(new SelectionAdapter() {\r
-                       public void widgetSelected(SelectionEvent event) {\r
-                               Dialog dialog = new CdmDataSourceDialog(UiUtil.getShell());\r
-                               dialog.open();\r
-                               \r
-                               // Refresh menu to reflect any changes in datasource repository\r
-                       }\r
-               });             \r
        }\r
-\r
+       \r
        public TaxonomicTreeViewer createTreeViewer() {\r
                \r
                // Dispose link to new taxon as necessary\r