Fixes #1412
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / handler / DeleteTaxonNodeHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.handler;
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.commands.IHandler;
22 import org.eclipse.core.commands.common.NotDefinedException;
23 import org.eclipse.core.commands.operations.IUndoableOperation;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.viewers.TreeSelection;
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorReference;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.PartInitException;
32 import org.eclipse.ui.handlers.HandlerUtil;
33
34 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
35 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
37 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
38 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
39 import eu.etaxonomy.taxeditor.operations.DeleteTreeNodeOperation;
40
41 /**
42 * @author n.hoffmann
43 * @created 06.04.2009
44 * @version 1.0
45 */
46 public class DeleteTaxonNodeHandler extends AbstractHandler implements IHandler{
47 private static final Logger logger = Logger
48 .getLogger(DeleteTaxonNodeHandler.class);
49 private IWorkbenchPage activePage;
50 private TaxonNavigator taxonNavigator;
51
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 // Prompt user for confirmation
55 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Are you sure you want to delete the selected taxa?")){
56 return null;
57 }
58
59 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
60
61 try {
62 taxonNavigator = (TaxonNavigator) NavigationUtil.getView(TaxonNavigator.ID);
63 } catch (PartInitException e) {
64 logger.error(e);
65 throw new RuntimeException(e);
66 }
67
68 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
69
70 Iterator selectionIterator = selection.iterator();
71 Set<ITreeNode> treeNodes = new HashSet<ITreeNode>();
72
73 while (selectionIterator.hasNext()){
74 Object object = selectionIterator.next();
75 if(object instanceof ITreeNode)
76 treeNodes.add((ITreeNode) object);
77 }
78
79 IUndoableOperation operation = null;
80 try {
81 operation = new DeleteTreeNodeOperation(event.getCommand().getName(), NavigationUtil.getUndoContext(), treeNodes, taxonNavigator, taxonNavigator);
82
83 IStatus status = NavigationUtil.executeOperation(operation);
84
85
86 // FIXME is there a better place for this code?
87 if (status == Status.OK_STATUS){
88 for (ITreeNode treeNode : treeNodes){
89 if(treeNode instanceof TaxonNode) {
90 closeObsoleteEditor((TaxonNode) treeNode);
91 }
92 }
93 }
94
95 } catch (NotDefinedException e) {
96 logger.warn("Command name not set");
97 }
98 return null;
99 }
100
101 private void closeObsoleteEditor(TaxonNode taxonNode){
102 for (IEditorReference ref : activePage.getEditorReferences()) {
103 try {
104 IEditorInput input = ref.getEditorInput();
105 if (input instanceof TaxonEditorInput) {
106 TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
107 if (taxonNode.equals(node)) {
108 activePage.closeEditor(ref.getEditor(false), false);
109 }
110 }
111 } catch (PartInitException e) {
112 continue;
113 }
114 }
115 }
116 }