close the polytomouskey-editor if the key is deleted
authorU-BGBM\k.luther <k.luther@BGBM11732.bgbm.fu-berlin.de>
Wed, 26 Aug 2015 10:29:16 +0000 (12:29 +0200)
committerU-BGBM\k.luther <k.luther@BGBM11732.bgbm.fu-berlin.de>
Wed, 26 Aug 2015 10:29:16 +0000 (12:29 +0200)
eu.etaxonomy.taxeditor.editor/META-INF/MANIFEST.MF
eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/key/polytomous/handler/DeleteHandler.java

index 6cd6141ef5b525ed27680172a1be94707b1ec035..d4471b24e77cb6f4e5a656b8fcce88e8d1df0757 100644 (file)
@@ -9,6 +9,7 @@ Bundle-Localization: OSGI-INF/l10n/plugin
 Export-Package: eu.etaxonomy.taxeditor.editor,
  eu.etaxonomy.taxeditor.editor.handler,
  eu.etaxonomy.taxeditor.editor.internal,
+ eu.etaxonomy.taxeditor.editor.key.polytomous,
  eu.etaxonomy.taxeditor.editor.name,
  eu.etaxonomy.taxeditor.editor.name.handler,
  eu.etaxonomy.taxeditor.editor.view.concept,
index faf141c73ba8ded1c2a973b3a72100bbf6fd2c64..a7968204941b92a7fc5efa0a1f1874ede385e9c6 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * 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.
 */
@@ -17,9 +17,14 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 import eu.etaxonomy.cdm.model.description.PolytomousKey;
+import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyViewPart;
@@ -33,28 +38,31 @@ import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
  */
 public class DeleteHandler extends AbstractHandler {
 
+    protected IWorkbenchPage activePage;
        /* (non-Javadoc)
         * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
         */
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
                IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
-               
+               activePage =  HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
                List<PolytomousKey> keys = selection.toList();
-               
+
                if(keys.isEmpty()){
                        return null;
                }
-               
+               for (PolytomousKey key : keys){
+                   closeObsoleteEditor(key);
+               }
                boolean confirmation = MessagingUtils.confirmDialog("Confirm deletaion", "Do you want to delete the selected key" + (keys.size() == 1 ? "" : "s") + "?");
-               
+
                if(confirmation){
-               
+
                        for(PolytomousKey key : keys){
                                try {
                                        AbstractPostOperation operation = new DeleteOperation(
                                                        event.getCommand().getName(),
-                                                       NavigationUtil.getUndoContext(), key, 
+                                                       NavigationUtil.getUndoContext(), key,
                                                        (PolytomousKeyViewPart) NavigationUtil.getView(PolytomousKeyViewPart.ID, false),
                                                        (PolytomousKeyViewPart) NavigationUtil.getView(PolytomousKeyViewPart.ID, false));
                                        NavigationUtil.executeOperation(operation);
@@ -63,8 +71,30 @@ public class DeleteHandler extends AbstractHandler {
                                }
                        }
                }
-               
+
                return null;
        }
 
+
+       protected boolean closeObsoleteEditor(PolytomousKey key){
+        boolean result = true;
+        for (IEditorReference ref : activePage.getEditorReferences()) {
+            try {
+
+                IEditorInput input = ref.getEditorInput();
+                if (input instanceof PolytomousKeyEditorInput) {
+                    PolytomousKey pKey = ((PolytomousKeyEditorInput)input).getKey();
+                    //if node is a child of taxonNode then close the editor
+                    if(key.equals(pKey)){
+                    //if (taxonNode.equals(node)) {
+                        result &= activePage.closeEditor(ref.getEditor(false), true);
+
+                    }
+                }
+            } catch (PartInitException e) {
+                continue;
+            }
+        }
+        return result;
+    }
 }