- fixed compile error in test
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / DeleteHandler.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.navigator.handler;
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.IHandler;
21 import org.eclipse.core.commands.common.NotDefinedException;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.TreeSelection;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorReference;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.handlers.HandlerUtil;
31
32 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
33 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
35 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
36 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
37 import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
38 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39
40 /**
41 * <p>DeleteTreeNodeHandler class.</p>
42 *
43 * @author n.hoffmann
44 * @created 06.04.2009
45 * @version 1.0
46 */
47 public class DeleteHandler extends AbstractHandler implements IHandler{
48
49 private IWorkbenchPage activePage;
50 private TaxonNavigator taxonNavigator;
51
52 /** {@inheritDoc} */
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54
55 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
56
57 taxonNavigator = NavigationUtil.showNavigator();
58
59 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
60
61 String plural = selection.size() > 1 ? "s" : "";
62 // Prompt user for confirmation
63 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Are you sure you want to delete the selected node" + plural +"?")){
64 return null;
65 }
66
67 Iterator selectionIterator = selection.iterator();
68 Set<ITreeNode> treeNodes = new HashSet<ITreeNode>();
69
70 while (selectionIterator.hasNext()){
71 Object object = selectionIterator.next();
72 if(object instanceof ITreeNode)
73 treeNodes.add((ITreeNode) object);
74 }
75
76 AbstractPostOperation operation = null;
77 try {
78 operation = new DeleteOperation(
79 event.getCommand().getName(), NavigationUtil.getUndoContext(),
80 treeNodes, taxonNavigator, taxonNavigator);
81
82 IStatus status = NavigationUtil.executeOperation(operation);
83
84
85 // FIXME is there a better place for this code?
86 if (status == Status.OK_STATUS){
87 for (ITreeNode treeNode : treeNodes){
88 if(treeNode instanceof TaxonNode) {
89 closeObsoleteEditor((TaxonNode) treeNode);
90 }
91 }
92 }
93
94 } catch (NotDefinedException e) {
95 NavigationUtil.warn(getClass(), "Command name not set");
96 }
97 return null;
98 }
99
100 private void closeObsoleteEditor(TaxonNode taxonNode){
101 for (IEditorReference ref : activePage.getEditorReferences()) {
102 try {
103 IEditorInput input = ref.getEditorInput();
104 if (input instanceof TaxonEditorInput) {
105 TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
106 if (taxonNode.equals(node)) {
107 activePage.closeEditor(ref.getEditor(false), false);
108 }
109 }
110 } catch (PartInitException e) {
111 continue;
112 }
113 }
114 }
115 }