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
.common
.NotDefinedException
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.viewers
.TreeSelection
;
23 import org
.eclipse
.ui
.IEditorInput
;
24 import org
.eclipse
.ui
.IEditorReference
;
25 import org
.eclipse
.ui
.IWorkbenchPage
;
26 import org
.eclipse
.ui
.PartInitException
;
27 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
29 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
30 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonNodeDeletionConfigurator
;
31 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonNodeDeletionConfigurator
.ChildHandling
;
32 import eu
.etaxonomy
.cdm
.model
.common
.ITreeNode
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
36 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
37 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.DeleteOperation
;
41 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
42 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.deleteConfigurator
.DeleteConfiguratorDialog
;
45 * <p>DeleteTreeNodeHandler class.</p>
51 public class DeleteHandler
extends AbstractHandler
{
53 protected IWorkbenchPage activePage
;
54 protected TaxonNavigator taxonNavigator
;
58 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
60 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
62 taxonNavigator
= NavigationUtil
.showNavigator();
64 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
67 String plural
= selection
.size() > 1 ?
"s" : "";
68 // Prompt user for confirmation
72 Iterator selectionIterator
= selection
.iterator();
73 Set
<ITaxonTreeNode
> treeNodes
= new HashSet
<ITaxonTreeNode
>();
75 while (selectionIterator
.hasNext()){
76 Object object
= selectionIterator
.next();
77 if(object
instanceof ITaxonTreeNode
) {
78 treeNodes
.add((ITaxonTreeNode
) object
);
81 boolean allEditorsClosed
= true;
82 for (ITaxonTreeNode treeNode
: treeNodes
){
83 if(treeNode
instanceof TaxonNode
) {
84 allEditorsClosed
&= closeObsoleteEditor((TaxonNode
) treeNode
);
87 AbstractPostOperation operation
= null;
88 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
89 if (treeNodes
.size() == 1 ){
92 ITaxonTreeNode treeNode
= treeNodes
.iterator().next();
93 ITaxonTreeNode taxonNode
=treeNode
;
94 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
95 //configNodes.setDeleteTaxon(false);
96 if (taxonNode
instanceof Classification
&& taxonNode
.hasChildNodes()){
97 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification? The tree has children, they will be deleted, too.")){
100 } else if (taxonNode
instanceof Classification
&& !taxonNode
.hasChildNodes()){
101 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification?")){
106 if (taxonNode
.hasChildNodes()){
107 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
109 HandlerUtil
.getActiveShell(event
),
112 "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
113 MessageDialog
.WARNING
, new String
[] { "Delete all children",
114 "Move children to parent node", "Skip" }, 0);
115 int result
= dialog
.open();
118 //delete all children
119 configNodes
.setChildHandling(ChildHandling
.DELETE
);
120 config
.setTaxonNodeConfig(configNodes
);
121 } else if (result
== 1){
123 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
124 config
.setTaxonNodeConfig(configNodes
);
125 } else if (result
== 2){
131 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(configNodes
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected node?")){
134 config
.setTaxonNodeConfig(configNodes
);
138 if (allEditorsClosed
){
139 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
143 operation
= new DeleteOperation(event
.getCommand().getName(),
144 NavigationUtil
.getUndoContext(),
151 NavigationUtil
.executeOperation(operation
);
158 } catch (NotDefinedException e
) {
159 MessagingUtils
.warn(getClass(), "Command name not set");
163 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
166 if (allEditorsClosed
){
168 operation
= new DeleteOperation(event
.getCommand().getName(),
169 NavigationUtil
.getUndoContext(),
171 new TaxonDeletionConfigurator(),
176 NavigationUtil
.executeOperation(operation
);
179 }catch (NotDefinedException e
) {
180 MessagingUtils
.warn(getClass(), "Command name not set");
186 protected boolean closeObsoleteEditor(TaxonNode taxonNode
){
187 boolean result
= true;
188 for (IEditorReference ref
: activePage
.getEditorReferences()) {
190 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
193 IEditorInput input
= ref
.getEditorInput();
194 if (input
instanceof TaxonEditorInput
) {
195 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
196 //if node is a child of taxonNode then close the editor
197 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
198 //if (taxonNode.equals(node)) {
199 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
203 } catch (PartInitException e
) {