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
.Classification
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
35 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
36 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.DeleteOperation
;
40 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
43 * <p>DeleteTreeNodeHandler class.</p>
49 public class DeleteHandler
extends AbstractHandler
implements IHandler
{
51 protected IWorkbenchPage activePage
;
52 protected TaxonNavigator taxonNavigator
;
56 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
58 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
60 taxonNavigator
= NavigationUtil
.showNavigator();
62 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
64 String plural
= selection
.size() > 1 ?
"s" : "";
65 // Prompt user for confirmation
69 Iterator selectionIterator
= selection
.iterator();
70 Set
<ITaxonTreeNode
> treeNodes
= new HashSet
<ITaxonTreeNode
>();
72 while (selectionIterator
.hasNext()){
73 Object object
= selectionIterator
.next();
74 if(object
instanceof ITaxonTreeNode
) {
75 treeNodes
.add((ITaxonTreeNode
) object
);
78 boolean allEditorsClosed
= true;
79 for (ITaxonTreeNode treeNode
: treeNodes
){
80 if(treeNode
instanceof TaxonNode
) {
81 allEditorsClosed
&= closeObsoleteEditor((TaxonNode
) treeNode
);
84 AbstractPostOperation operation
= null;
85 if (treeNodes
.size() == 1 ){
88 ITaxonTreeNode treeNode
= treeNodes
.iterator().next();
89 ITaxonTreeNode taxonNode
=treeNode
;
91 if (taxonNode
instanceof Classification
&& taxonNode
.hasChildNodes()){
92 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification? The tree has children, they will be deleted, too")){
95 } else if (taxonNode
instanceof Classification
&& !taxonNode
.hasChildNodes()){
96 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification?")){
100 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected node?")){
105 if (allEditorsClosed
){
106 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
109 operation
= new DeleteOperation(
110 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
111 taxonNode
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
113 NavigationUtil
.executeOperation(operation
);
119 } catch (NotDefinedException e
) {
120 MessagingUtils
.warn(getClass(), "Command name not set");
124 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
127 if (allEditorsClosed
){
128 operation
= new DeleteOperation(
129 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
130 treeNodes
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
132 NavigationUtil
.executeOperation(operation
);
134 }catch (NotDefinedException e
) {
135 MessagingUtils
.warn(getClass(), "Command name not set");
141 protected boolean closeObsoleteEditor(TaxonNode taxonNode
){
142 boolean result
= true;
143 for (IEditorReference ref
: activePage
.getEditorReferences()) {
145 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
148 IEditorInput input
= ref
.getEditorInput();
149 if (input
instanceof TaxonEditorInput
) {
150 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
151 //if node is a child of taxonNode then close the editor
152 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
153 //if (taxonNode.equals(node)) {
154 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
158 } catch (PartInitException e
) {