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
.navigator
.handler
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
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
.jface
.dialogs
.MessageDialog
;
23 import org
.eclipse
.jface
.viewers
.TreeSelection
;
24 import org
.eclipse
.ui
.IEditorInput
;
25 import org
.eclipse
.ui
.IEditorReference
;
26 import org
.eclipse
.ui
.IWorkbenchPage
;
27 import org
.eclipse
.ui
.PartInitException
;
28 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
30 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
31 import eu
.etaxonomy
.cdm
.model
.common
.ITreeNode
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
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
;
41 * <p>DeleteTreeNodeHandler class.</p>
47 public class DeleteHandler
extends AbstractHandler
implements IHandler
{
49 private IWorkbenchPage activePage
;
50 private TaxonNavigator taxonNavigator
;
54 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
56 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
58 taxonNavigator
= NavigationUtil
.showNavigator();
60 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
62 String plural
= selection
.size() > 1 ?
"s" : "";
63 // Prompt user for confirmation
64 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Are you sure you want to delete the selected node" + plural
+"?")){
68 Iterator selectionIterator
= selection
.iterator();
69 Set
<ITreeNode
> treeNodes
= new HashSet
<ITreeNode
>();
71 while (selectionIterator
.hasNext()){
72 Object object
= selectionIterator
.next();
73 if(object
instanceof ITreeNode
) {
74 treeNodes
.add((ITreeNode
) object
);
77 boolean allEditorsClosed
= true;
78 for (ITreeNode treeNode
: treeNodes
){
79 if(treeNode
instanceof TaxonNode
) {
80 allEditorsClosed
&= closeObsoleteEditor((TaxonNode
) treeNode
);
83 AbstractPostOperation operation
= null;
84 if (treeNodes
.size() == 1 ){
88 if (allEditorsClosed
){
89 TaxonNode taxonNode
= (TaxonNode
)treeNodes
.iterator().next();
90 operation
= new DeleteOperation(
91 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
92 taxonNode
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
94 NavigationUtil
.executeOperation(operation
);
99 } catch (NotDefinedException e
) {
100 NavigationUtil
.warn(getClass(), "Command name not set");
104 if (allEditorsClosed
){
105 operation
= new DeleteOperation(
106 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
107 treeNodes
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
109 NavigationUtil
.executeOperation(operation
);
111 }catch (NotDefinedException e
) {
112 NavigationUtil
.warn(getClass(), "Command name not set");
118 private boolean closeObsoleteEditor(TaxonNode taxonNode
){
119 boolean result
= true;
120 for (IEditorReference ref
: activePage
.getEditorReferences()) {
122 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
125 IEditorInput input
= ref
.getEditorInput();
126 if (input
instanceof TaxonEditorInput
) {
127 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
128 //if node is a child of taxonNode then close the editor
129 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
130 //if (taxonNode.equals(node)) {
131 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
135 } catch (PartInitException e
) {