cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.java
index a266c8d9249de70341c8ddebb3d14107d3c44dd8..2d33925c277fe7ca1736b9173fd89188cf521b97 100644 (file)
 
 package eu.etaxonomy.taxeditor.navigation;
 
 
 package eu.etaxonomy.taxeditor.navigation;
 
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.Set;
 import java.util.UUID;
 
 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.common.NotDefinedException;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.commands.operations.UndoContext;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.commands.operations.UndoContext;
-import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.swt.widgets.Display;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.widgets.Shell;
 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.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.ICdmBase;
-import eu.etaxonomy.cdm.model.description.PolytomousKey;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.name.TaxonName;
+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.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.TaxonNodeDto;
 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 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.editor.e4.TaxonEditorInputE4;
+import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
 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.navigation.l10n.Messages;
+import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
- * <p>NavigationUtil class.</p>
  *
  * @author n.hoffmann
  * @created 24.03.2009
  * @version 1.0
  */
 public class NavigationUtil extends AbstractUtility{
  *
  * @author n.hoffmann
  * @created 24.03.2009
  * @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 IUndoContext defaultUndoContext;
 
        /**
         * <p>openEditor</p>
         *
         * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
         */
 
        /**
         * <p>openEditor</p>
         *
         * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
         */
-       public static void openEditor(ICdmBase selectedObject){
+       public static void openEditor(UuidAndTitleCache uuidAndTitleCache, Shell shell, EModelService modelService, EPartService partService, MApplication application){
+           Class<?> type = uuidAndTitleCache.getType();
+           ICdmBase cdmBase = null;
+           if(uuidAndTitleCache instanceof TaxonNodeDto){
+            EditorUtil.openTaxonNodeE4(uuidAndTitleCache.getUuid(), modelService, partService, application);
+            return;
+        }
+           else 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(TaxonName.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, shell, modelService, partService, application);
+           }
+           else{
+               MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
+           }
+       }
+
+       public static void openEditor(ICdmBase selectedObject, Shell shell, EModelService modelService, EPartService partService, MApplication application){
                UUID entityUuid = selectedObject.getUuid();
                try {
                        if(selectedObject instanceof TaxonNode){
                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
-                               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{
-                               MessagingUtils.warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
+                           TaxonNode taxonNode = (TaxonNode)selectedObject;
+                           Classification classification = taxonNode.getClassification();
+                           if(classification!=null && classification.getRootNode().equals(taxonNode)){
+                               NewClassificationWizard classificationWizard = new NewClassificationWizard();
+                               classificationWizard.init(null, null);
+                               classificationWizard.setEntity(classification);
+                               WizardDialog dialog = new WizardDialog(shell, classificationWizard);
+                               dialog.open();
+                           }
+                           else{
+                               EditorUtil.openTaxonNodeE4(entityUuid, modelService, partService, application);
+                           }
+                       }
+                       else if(selectedObject instanceof TaxonBase){
+                           EditorUtil.openTaxonBaseE4(entityUuid, modelService, partService, application);
+                       }
+                       else{
+                               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);
                } catch (Exception e) {
                } catch (Exception e) {
-                   MessagingUtils.errorDialog("Could not create Taxon",
+                   MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
                            NavigationUtil.class,
                            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
                            e,
                            NavigationUtil.class,
                            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
                            e,
@@ -108,63 +126,13 @@ public class NavigationUtil extends AbstractUtility{
                }
        }
 
                }
        }
 
-       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);
-                               }
-                       }
-               }else if(selectedObject instanceof ICdmBase){
-                       openEditor((ICdmBase) selectedObject);
-               }else{
-                       MessagingUtils.error(NavigationUtil.class, new IllegalArgumentException("Selected object is not supported: " + selectedObject));
-               }
-       }
-
        /**
         * <p>openEmpty</p>
         *
         * @param parentNodeUuid a {@link java.util.UUID} object.
         */
        public static void openEmpty(UUID parentNodeUuid) {
        /**
         * <p>openEmpty</p>
         *
         * @param parentNodeUuid a {@link java.util.UUID} object.
         */
        public static void openEmpty(UUID parentNodeUuid) {
-               try {
-                       EditorUtil.openEmpty(parentNodeUuid);
-               } catch (PartInitException e) {
-                       MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
-               }
-       }
-
-       /**
-        * <p>getShell</p>
-        *
-        * @return a {@link org.eclipse.swt.widgets.Shell} object.
-        */
-       public static Shell getShell() {
-               return getActiveWindow().getShell();
-       }
-
-       /**
-        * <p>getActiveWindow</p>
-        *
-        * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
-        */
-       public static IWorkbenchWindow getActiveWindow() {
-               return TaxeditorNavigationPlugin.getDefault().getWorkbench().
-                               getActiveWorkbenchWindow();
-       }
-
-       /**
-        * <p>getWorkbenchUndoContext</p>
-        *
-        * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
-        */
-       public static IUndoContext getWorkbenchUndoContext() {
-               return TaxeditorEditorPlugin.getDefault().getWorkbench().
-                                       getOperationSupport().getUndoContext();
+           EditorUtil.openEmptyE4(parentNodeUuid);
        }
 
        /**
        }
 
        /**
@@ -191,52 +159,39 @@ public class NavigationUtil extends AbstractUtility{
         * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
         * @return a boolean.
         */
         * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
         * @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();
-                                       if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
-                                               return true;
-                                       }
-                               }
-                       } catch (PartInitException e) {
-                               MessagingUtils.error(NavigationUtil.class, e.getMessage(), e);
-                               throw new RuntimeException(e);
-                       }
-
-               }
+       public static boolean isDirty(TaxonNodeDto taxonNode, EPartService partService){
+
+           Collection<MPart> dirtyParts = partService.getDirtyParts();
+           for (MPart part : dirtyParts) {
+            if(part.getObject() instanceof TaxonNameEditorE4){
+                TaxonEditorInputE4 input = ((TaxonNameEditorE4) part.getObject()).getEditorInput();
+                if(input.getTaxonNode().getUuid().equals(taxonNode.getUuid())){
+                    return true;
+                }
+            }
+        }
                return false;
        }
 
        /**
                return false;
        }
 
        /**
-        * <p>selectInNavigator</p>
-        *
-        * @param element a {@link java.lang.Object} object.
-        * @param parentElement a {@link java.lang.Object} object.
-        */
-       public static void selectInNavigator(final Object element, final Object parentElement) {
-               Display.getDefault().asyncExec(new Runnable(){
-
-                       @Override
-            public void run() {
-                               TaxonNavigator navigator = showNavigator();
-
-                               if (navigator != null) {
-                                       CommonViewer viewer = navigator.getCommonViewer();
-                                       if (viewer != null) {
-                                               if (parentElement != null) {
-                                                       viewer.setExpandedState(parentElement, true);
-                                               }
-                                               viewer.setSelection(new StructuredSelection(element));
-                                       }
-                               }
-                       }
-
-               });
-       }
+     * Whether a taxonNode has unsaved changes.
+     *
+     * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
+     * @return a boolean.
+     */
+    public static boolean isDirty(TaxonNode taxonNode, EPartService partService){
+
+        Collection<MPart> dirtyParts = partService.getDirtyParts();
+        for (MPart part : dirtyParts) {
+            if(part.getObject() instanceof TaxonNameEditorE4){
+                TaxonEditorInputE4 input = ((TaxonNameEditorE4) part.getObject()).getEditorInput();
+                if(input.getTaxonNode().equals(taxonNode)){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
        /**
         * <p>openSearch</p>
 
        /**
         * <p>openSearch</p>
@@ -252,25 +207,29 @@ public class NavigationUtil extends AbstractUtility{
                }else if(selection instanceof Synonym){
                        Synonym synonym = (Synonym) selection;
 
                }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{
 
                }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);
                }
 
        }
 
        private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
                }
 
        }
 
        private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
-               if(acceptedTaxa.size() == 1){
-                       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.");
-               }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.");
-               }
+           //FIXME E4 migrate/delete
+//             if(acceptedTaxa.size() == 1){
+//                     openEditor(acceptedTaxa.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+//             }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, Messages.NavigationUtil_MULTI_TREE);
+//             }else if(acceptedTaxa.size() == 0){
+//                     // this is an undesired state
+//                     MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
+//             }
        }
 
        /**
        }
 
        /**
@@ -278,45 +237,17 @@ public class NavigationUtil extends AbstractUtility{
         */
        private static void handleOpeningOfMultipleTaxonNodes(
                        Set<TaxonNode> taxonNodes) {
         */
        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.
-                       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.");
-               }
-       }
-
-       /**
-        * <p>showNavigator</p>
-        *
-        * @return the TaxonNavigator instance if present
-        */
-       public static TaxonNavigator showNavigator() {
-               return (TaxonNavigator) showView(TaxonNavigator.ID);
-       }
-
-       /**
-        * <p>getNavigator</p>
-        *
-        * @param restore a boolean.
-        * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
-        */
-       public static TaxonNavigator getNavigator(boolean restore) {
-               return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
-       }
-
-       /**
-        * <p>getOpenEditors</p>
-        *
-        * @return a {@link java.util.Set} object.
-        */
-       public static Set<IEditorPart> getOpenEditors() {
-               return EditorUtil.getOpenEditors();
+           //FIXME E4 migrate/delete
+//
+//             if(taxonNodes.size() == 1){
+//                     openEditor(taxonNodes.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+//             }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.");
+//             }else if(taxonNodes.size() == 0){
+//                     // this is an undesired state
+//                     MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
+//             }
        }
 
        /**
        }
 
        /**