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
.navigation
.NavigationUtil
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.DeleteOperation
;
39 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
42 * <p>DeleteTreeNodeHandler class.</p>
48 public class DeleteHandler
extends AbstractHandler
implements IHandler
{
50 protected IWorkbenchPage activePage
;
51 protected TaxonNavigator taxonNavigator
;
55 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
57 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
59 taxonNavigator
= NavigationUtil
.showNavigator();
61 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
63 String plural
= selection
.size() > 1 ?
"s" : "";
64 // Prompt user for confirmation
68 Iterator selectionIterator
= selection
.iterator();
69 Set
<ITaxonTreeNode
> treeNodes
= new HashSet
<ITaxonTreeNode
>();
71 while (selectionIterator
.hasNext()){
72 Object object
= selectionIterator
.next();
73 if(object
instanceof ITaxonTreeNode
) {
74 treeNodes
.add((ITaxonTreeNode
) object
);
77 boolean allEditorsClosed
= true;
78 for (ITaxonTreeNode treeNode
: treeNodes
){
79 if(treeNode
instanceof TaxonNode
) {
80 allEditorsClosed
&= closeObsoleteEditor((TaxonNode
) treeNode
);
83 AbstractPostOperation operation
= null;
84 if (treeNodes
.size() == 1 ){
87 ITaxonTreeNode treeNode
= treeNodes
.iterator().next();
88 ITaxonTreeNode taxonNode
=treeNode
;
90 if (taxonNode
instanceof Classification
&& taxonNode
.hasChildNodes()){
91 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")){
94 } else if (taxonNode
instanceof Classification
&& !taxonNode
.hasChildNodes()){
95 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification?")){
99 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected node?")){
104 if (allEditorsClosed
){
105 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
108 operation
= new DeleteOperation(
109 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
110 taxonNode
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
112 NavigationUtil
.executeOperation(operation
);
118 } catch (NotDefinedException e
) {
119 NavigationUtil
.warn(getClass(), "Command name not set");
123 if(! MessageDialog
.openConfirm(HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
126 if (allEditorsClosed
){
127 operation
= new DeleteOperation(
128 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
129 treeNodes
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
131 NavigationUtil
.executeOperation(operation
);
133 }catch (NotDefinedException e
) {
134 NavigationUtil
.warn(getClass(), "Command name not set");
140 protected boolean closeObsoleteEditor(TaxonNode taxonNode
){
141 boolean result
= true;
142 for (IEditorReference ref
: activePage
.getEditorReferences()) {
144 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
147 IEditorInput input
= ref
.getEditorInput();
148 if (input
instanceof TaxonEditorInput
) {
149 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
150 //if node is a child of taxonNode then close the editor
151 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
152 //if (taxonNode.equals(node)) {
153 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
157 } catch (PartInitException e
) {