3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.navigation
.handler
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
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
;
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
;
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
;
52 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
54 // Prompt user for confirmation
55 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Are you sure you want to delete the selected taxa?")){
59 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
62 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.getView(TaxonNavigator
.ID
);
63 } catch (PartInitException e
) {
65 throw new RuntimeException(e
);
68 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
70 Iterator selectionIterator
= selection
.iterator();
71 Set
<ITreeNode
> treeNodes
= new HashSet
<ITreeNode
>();
73 while (selectionIterator
.hasNext()){
74 Object object
= selectionIterator
.next();
75 if(object
instanceof ITreeNode
)
76 treeNodes
.add((ITreeNode
) object
);
79 IUndoableOperation operation
= null;
81 operation
= new DeleteTreeNodeOperation(event
.getCommand().getName(), NavigationUtil
.getUndoContext(), treeNodes
, taxonNavigator
, taxonNavigator
);
83 IStatus status
= NavigationUtil
.executeOperation(operation
);
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
);
95 } catch (NotDefinedException e
) {
96 logger
.warn("Command name not set");
101 private void closeObsoleteEditor(TaxonNode taxonNode
){
102 for (IEditorReference ref
: activePage
.getEditorReferences()) {
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);
111 } catch (PartInitException e
) {