cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.java
index 6680e5f40ab29f1ccb9902ff6801734b0766bdcc..2d33925c277fe7ca1736b9173fd89188cf521b97 100644 (file)
@@ -9,57 +9,45 @@
 
 package eu.etaxonomy.taxeditor.navigation;
 
-import java.util.HashMap;
+import java.util.Collection;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
-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.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.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.description.PolytomousKey;
 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.persistence.dto.TaxonNodeDto;
 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.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>
  *
  * @author n.hoffmann
  * @created 24.03.2009
@@ -68,7 +56,6 @@ import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 public class NavigationUtil extends AbstractUtility{
 
     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;
 
        /**
@@ -76,10 +63,14 @@ public class NavigationUtil extends AbstractUtility{
         *
         * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
         */
-       public static void openEditor(UuidAndTitleCache uuidAndTitleCache){
+       public static void openEditor(UuidAndTitleCache uuidAndTitleCache, Shell shell, EModelService modelService, EPartService partService, MApplication application){
            Class<?> type = uuidAndTitleCache.getType();
            ICdmBase cdmBase = null;
-           if(type.equals(Classification.class)){
+           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)){
@@ -95,42 +86,36 @@ public class NavigationUtil extends AbstractUtility{
                MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
            }
            if(cdmBase!=null){
-               openEditor(cdmBase);
+               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){
+       public static void openEditor(ICdmBase selectedObject, Shell shell, EModelService modelService, EPartService partService, MApplication application){
                UUID entityUuid = selectedObject.getUuid();
                try {
-                       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();
+                       if(selectedObject instanceof TaxonNode){
+                           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 TaxonNode){
-                               EditorUtil.openTaxonNode(entityUuid);
-                       }else if(selectedObject instanceof TaxonBase){
-                               TaxonBase taxonBase = (TaxonBase)selectedObject;
-                               if(taxonBase.isOrphaned()){
-                                       openInBulkEditor(taxonBase);
-                               }
-                               else{
-                                       EditorUtil.openTaxonBase(entityUuid);
-                               }
-                       }else if(selectedObject instanceof TaxonName){
-                               openInBulkEditor(selectedObject);
-                       }else if(selectedObject instanceof PolytomousKey){
-                               EditorUtil.openPolytomousKey(entityUuid);
-                       }else{
+                       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) {
                    MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
                            NavigationUtil.class,
@@ -141,72 +126,13 @@ public class NavigationUtil extends AbstractUtility{
                }
        }
 
-       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$
-                       }
-               }
-       }
-
        /**
         * <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);
        }
 
        /**
@@ -233,52 +159,39 @@ public class NavigationUtil extends AbstractUtility{
         * @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;
        }
 
        /**
-        * <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>
@@ -307,15 +220,16 @@ public class NavigationUtil extends AbstractUtility{
        }
 
        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, 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);
-               }
+           //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);
+//             }
        }
 
        /**
@@ -323,44 +237,17 @@ 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.
-                       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);
-               }
-       }
-
-       /**
-        * <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);
+//             }
        }
 
        /**