Merge branch 'develop' of ssh://dev.e-taxonomy.eu/var/git/taxeditor into develop
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.java
index 5925be91e873ab2ed6bc4acddf15898b199cac96..613dbabb33885f5a9207f5df1f4dcefd966b63c6 100644 (file)
@@ -1,8 +1,8 @@
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -29,7 +29,6 @@ import org.eclipse.ui.handlers.IHandlerService;
 import org.eclipse.ui.navigator.CommonViewer;
 
 import eu.etaxonomy.cdm.model.common.ICdmBase;
-import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
 import eu.etaxonomy.cdm.model.description.PolytomousKey;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
@@ -37,12 +36,13 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
-import eu.etaxonomy.taxeditor.editor.OpenEditorConfiguration;
 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
  * <p>NavigationUtil class.</p>
@@ -53,72 +53,60 @@ import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
  */
 public class NavigationUtil extends AbstractUtility{
        private static IUndoContext defaultUndoContext;
-               
+
        /**
         * <p>executeEditHandler</p>
         */
        public static void executeEditHandler(){
 
-               String commandId = "eu.etaxonomy.taxeditor.navigation.command.editSelection";
-               
-               IHandlerService handlerService = (IHandlerService) NavigationUtil.getService(IHandlerService.class); 
+               String commandId = "eu.etaxonomy.taxeditor.navigation.command.update.editSelection";
+
+               IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
                try {
                        handlerService.executeCommand(commandId, null);
                } catch (ExecutionException e) {
-                       NavigationUtil.error(NavigationUtil.class, e);
+                       MessagingUtils.error(NavigationUtil.class, e);
                } catch (NotDefinedException e) {
-                       NavigationUtil.error(NavigationUtil.class, e);
+                       MessagingUtils.error(NavigationUtil.class, e);
                } catch (NotEnabledException e) {
-                       NavigationUtil.error(NavigationUtil.class, e);
+                       MessagingUtils.error(NavigationUtil.class, e);
                } catch (NotHandledException e) {
-                       NavigationUtil.error(NavigationUtil.class, e);
+                       MessagingUtils.error(NavigationUtil.class, e);
                }
        }
-       
+
        /**
         * <p>openEditor</p>
         *
         * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
         */
        public static void openEditor(ICdmBase selectedObject){
-               UUID entityUuid = selectedObject.getUuid();     
-               try {   
+               UUID entityUuid = selectedObject.getUuid();
+               try {
                        if(selectedObject instanceof TaxonNode){
                                EditorUtil.openTaxonNode(entityUuid);
                        }else if(selectedObject instanceof TaxonBase){
                                EditorUtil.openTaxonBase(entityUuid);
                        }else if(selectedObject instanceof TaxonNameBase){
                                // TODO open bulk editor
-                               warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet.");
+                               MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet.");
                        }else if(selectedObject instanceof PolytomousKey){
                                EditorUtil.openPolytomousKey(entityUuid);
                        }else{
-                               warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
+                               MessagingUtils.warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
                        }
                } catch (PartInitException e) {
-                       NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
+                       MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
                } catch (Exception e) {
-                       EditorUtil.warningDialog("Could not create Taxon", NavigationUtil.class, e.getMessage());
-               }
-       }
-       
-       public static void openEditor(Object selectedObject){
-               if (selectedObject instanceof UuidAndTitleCache){
-                       Class type = ((UuidAndTitleCache) selectedObject).getType();
-                       if(type == Taxon.class || type == Synonym.class){
-                               try {
-                                       EditorUtil.openTaxonBase(((UuidAndTitleCache) selectedObject).getUuid());
-                               } catch (PartInitException e) {
-                                       NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
-                               }
-                       }
-               }else if(selectedObject instanceof ICdmBase){
-                       openEditor((ICdmBase) selectedObject); 
-               }else{
-                       NavigationUtil.error(NavigationUtil.class, new IllegalArgumentException("Selected object is not supported: " + selectedObject));
+                   MessagingUtils.errorDialog("Could not create Taxon",
+                           NavigationUtil.class,
+                           e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
+                           e,
+                           true);
+
                }
        }
-       
+
        /**
         * <p>openEmpty</p>
         *
@@ -128,10 +116,10 @@ public class NavigationUtil extends AbstractUtility{
                try {
                        EditorUtil.openEmpty(parentNodeUuid);
                } catch (PartInitException e) {
-                       NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
+                       MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
                }
        }
-       
+
        /**
         * <p>getShell</p>
         *
@@ -150,7 +138,7 @@ public class NavigationUtil extends AbstractUtility{
                return TaxeditorNavigationPlugin.getDefault().getWorkbench().
                                getActiveWorkbenchWindow();
        }
-       
+
        /**
         * <p>getWorkbenchUndoContext</p>
         *
@@ -169,10 +157,10 @@ public class NavigationUtil extends AbstractUtility{
        public static IUndoContext getUndoContext() {
                // FIXME this has to be more specific. Every widget has to have its own undo context
 //             return IOperationHistory.GLOBAL_UNDO_CONTEXT;
-               
-               // Plug-ins that wish their operations to be undoable from workbench views 
-               // such as the Navigator or Package Explorer should assign the workbench 
-               // undo context to their operations. 
+
+               // Plug-ins that wish their operations to be undoable from workbench views
+               // such as the Navigator or Package Explorer should assign the workbench
+               // undo context to their operations.
                if (defaultUndoContext == null) {
                        defaultUndoContext = new UndoContext();
                }
@@ -186,9 +174,9 @@ public class NavigationUtil extends AbstractUtility{
         * @return a boolean.
         */
        public static boolean isDirty(TaxonNode taxonNode){
-               
+
                for (IEditorReference reference : getActivePage().getEditorReferences()) {
-                       
+
                        try {
                                if (reference.getEditorInput() instanceof TaxonEditorInput) {
                                        TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
@@ -197,10 +185,10 @@ public class NavigationUtil extends AbstractUtility{
                                        }
                                }
                        } catch (PartInitException e) {
-                               NavigationUtil.error(NavigationUtil.class, e.getMessage(), e);
+                               MessagingUtils.error(NavigationUtil.class, e.getMessage(), e);
                                throw new RuntimeException(e);
                        }
-                       
+
                }
                return false;
        }
@@ -214,7 +202,8 @@ public class NavigationUtil extends AbstractUtility{
        public static void selectInNavigator(final Object element, final Object parentElement) {
                Display.getDefault().asyncExec(new Runnable(){
 
-                       public void run() {
+                       @Override
+            public void run() {
                                TaxonNavigator navigator = showNavigator();
 
                                if (navigator != null) {
@@ -223,11 +212,11 @@ public class NavigationUtil extends AbstractUtility{
                                                if (parentElement != null) {
                                                        viewer.setExpandedState(parentElement, true);
                                                }
-                                               viewer.setSelection(new StructuredSelection((TaxonNode) element));
+                                               viewer.setSelection(new StructuredSelection(element));
                                        }
                                }
                        }
-                       
+
                });
        }
 
@@ -239,18 +228,18 @@ public class NavigationUtil extends AbstractUtility{
        public static void openSearch(Object selection) {
                if(selection instanceof Taxon){
                        Taxon taxon = (Taxon) selection;
-                       
+
                        handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
-                       
+
                }else if(selection instanceof Synonym){
                        Synonym synonym = (Synonym) selection;
-                       
+
                        handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
-                       
+
                }else{
-                       warningDialog("Not implemented yet", NavigationUtil.class, "You chose to open a name that has no connection to a taxon. The Editor does not support editing of such a content type at the moment.");
+                       MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "You chose to open a name that has no connection to a taxon. The Editor does not support editing of such a content type at the moment.");
                }
-               
+
        }
 
        private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
@@ -258,11 +247,11 @@ public class NavigationUtil extends AbstractUtility{
                        openEditor(acceptedTaxa.iterator().next());
                }else if(acceptedTaxa.size() > 1){
                        // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
-                       warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
+                       MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
                                        " This case is not handled yet by the software.");
                }else if(acceptedTaxa.size() == 0){
                        // this is an undesired state
-                       warningDialog("Not implemented yet", NavigationUtil.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
+                       MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
                }
        }
 
@@ -271,16 +260,16 @@ public class NavigationUtil extends AbstractUtility{
         */
        private static void handleOpeningOfMultipleTaxonNodes(
                        Set<TaxonNode> taxonNodes) {
-               
+
                if(taxonNodes.size() == 1){
                        openEditor(taxonNodes.iterator().next());
                }else if(taxonNodes.size() > 1){
                        // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
-                       warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
+                       MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
                                        " This case is not handled yet by the software.");
                }else if(taxonNodes.size() == 0){
                        // this is an undesired state
-                       warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
+                       MessagingUtils.warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
                }
        }