Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.java
index ab46175b11a6c98cc0369d4a27a6caa82713945c..626af92f5755215aaf0811a237630f7993826c3a 100644 (file)
@@ -9,40 +9,54 @@
 
 package eu.etaxonomy.taxeditor.navigation;
 
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.NotEnabledException;
-import org.eclipse.core.commands.NotHandledException;
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ParameterizedCommand;
 import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.commands.operations.UndoContext;
 import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.commands.ICommandService;
 import org.eclipse.ui.handlers.IHandlerService;
 import org.eclipse.ui.navigator.CommonViewer;
 
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.INameService;
+import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
 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.Classification;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 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.l10n.Messages;
 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
+import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
  * <p>NavigationUtil class.</p>
@@ -52,70 +66,104 @@ import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
  * @version 1.0
  */
 public class NavigationUtil extends AbstractUtility{
-       private static IUndoContext defaultUndoContext;
 
-       /**
-        * <p>executeEditHandler</p>
-        */
-       public static void executeEditHandler(){
-
-               String commandId = "eu.etaxonomy.taxeditor.navigation.command.update.editSelection";
-
-               IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
-               try {
-                       handlerService.executeCommand(commandId, null);
-               } catch (ExecutionException e) {
-                       MessagingUtils.error(NavigationUtil.class, e);
-               } catch (NotDefinedException e) {
-                       MessagingUtils.error(NavigationUtil.class, e);
-               } catch (NotEnabledException e) {
-                       MessagingUtils.error(NavigationUtil.class, e);
-               } catch (NotHandledException e) {
-                       MessagingUtils.error(NavigationUtil.class, e);
-               }
-       }
+    private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
+    private static final String ERROR_OPENING_THE_EDITOR = Messages.NavigationUtil_OPEN_ERROR;
+    private static IUndoContext defaultUndoContext;
 
        /**
         * <p>openEditor</p>
         *
         * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
         */
+       public static void openEditor(UuidAndTitleCache uuidAndTitleCache){
+           Class type = uuidAndTitleCache.getType();
+           ICdmBase cdmBase = null;
+           if(type.equals(Classification.class)){
+               cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
+           }
+           else if(type.equals(TaxonNode.class)){
+               cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
+           }
+           else if(TaxonBase.class.isAssignableFrom(type)){
+               cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
+           }
+           else if(type.equals(TaxonNameBase.class)){
+               cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
+           }
+           else{
+               MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
+           }
+           if(cdmBase!=null){
+               openEditor(cdmBase);
+           }
+           else{
+               MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
+           }
+       }
+
        public static void openEditor(ICdmBase selectedObject){
                UUID entityUuid = selectedObject.getUuid();
                try {
-                       if(selectedObject instanceof TaxonNode){
+                       if(selectedObject instanceof Classification){
+                       NewClassificationWizard classificationWizard = new NewClassificationWizard();
+                       classificationWizard.init(null, null);
+                       classificationWizard.setEntity((Classification) selectedObject);
+                       WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), classificationWizard);
+                       dialog.open();
+                       }
+                       else if(selectedObject instanceof TaxonNode){
                                EditorUtil.openTaxonNode(entityUuid);
                        }else if(selectedObject instanceof TaxonBase){
-                               EditorUtil.openTaxonBase(entityUuid);
+                               TaxonBase taxonBase = (TaxonBase)selectedObject;
+                               if(taxonBase.isOrphaned()){
+                                       openInBulkEditor(taxonBase);
+                               }
+                               else{
+                                       EditorUtil.openTaxonBase(entityUuid);
+                               }
                        }else if(selectedObject instanceof TaxonNameBase){
-                               // TODO open bulk editor
-                               MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet.");
+                               openInBulkEditor(selectedObject);
                        }else if(selectedObject instanceof PolytomousKey){
                                EditorUtil.openPolytomousKey(entityUuid);
                        }else{
-                               MessagingUtils.warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
+                               MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
                        }
                } catch (PartInitException e) {
-                       MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
+                       MessagingUtils.error(NavigationUtil.class, ERROR_OPENING_THE_EDITOR, e);
                } catch (Exception e) {
-                       MessagingUtils.warningDialog("Could not create Taxon", NavigationUtil.class, e.getMessage());
+                   MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
+                           NavigationUtil.class,
+                           e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
+                           e,
+                           true);
+
                }
        }
 
-       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) {
-                                       MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
+       private static void openInBulkEditor(ICdmBase selectedObject) {
+               ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+               IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
+               String openInBulkEditorCommand = "eu.etaxonomy.taxeditor.bulkeditor.openBulkEditorForIdentifiableEntity"; //$NON-NLS-1$
+               Command command = commandService.getCommand(openInBulkEditorCommand);
+               if(command.isDefined()){
+                       Map<String, UUID> params = new HashMap<String, UUID>();
+                       params.put(openInBulkEditorCommand+".uuid", selectedObject.getUuid()); //$NON-NLS-1$
+                       ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
+                       try {
+                               if(parameterizedCommand!=null){
+                                       handlerService.executeCommand(parameterizedCommand, null);
+                                       return;
+                               }
+                               else{
+                                       handlerService.executeCommand(command.getId(), null);
+                                       return;
                                }
+                       } catch (NotDefinedException nde) {
+                               throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
+                       } catch (Exception exception) {
+                               MessagingUtils.error(NavigationUtil.class, "An exception occured while trying to execute "+command.getId(), exception); //$NON-NLS-1$
                        }
-               }else if(selectedObject instanceof ICdmBase){
-                       openEditor((ICdmBase) selectedObject);
-               }else{
-                       MessagingUtils.error(NavigationUtil.class, new IllegalArgumentException("Selected object is not supported: " + selectedObject));
                }
        }
 
@@ -128,7 +176,7 @@ public class NavigationUtil extends AbstractUtility{
                try {
                        EditorUtil.openEmpty(parentNodeUuid);
                } catch (PartInitException e) {
-                       MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
+                       MessagingUtils.error(NavigationUtil.class, ERROR_OPENING_THE_EDITOR, e);
                }
        }
 
@@ -246,10 +294,14 @@ public class NavigationUtil extends AbstractUtility{
                }else if(selection instanceof Synonym){
                        Synonym synonym = (Synonym) selection;
 
-                       handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
+                       Set<Taxon> accTaxa = new HashSet<Taxon>();
+                       if (synonym.getAcceptedTaxon() != null){
+                               accTaxa.add(synonym.getAcceptedTaxon());
+                       }
+                       handleOpeningOfMultipleTaxa(accTaxa);
 
                }else{
-                       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.");
+                       MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
                }
 
        }
@@ -259,11 +311,10 @@ 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.
-                       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.");
+                       MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
                }else if(acceptedTaxa.size() == 0){
                        // this is an undesired state
-                       MessagingUtils.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, Messages.NavigationUtil_ORPHAN_TAXON);
                }
        }
 
@@ -277,11 +328,10 @@ public class NavigationUtil extends AbstractUtility{
                        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.
-                       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.");
+                       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
-                       MessagingUtils.warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
+                       MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
                }
        }