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
.AbstractUtility
;
38 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
41 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.DeleteOperation
;
42 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
43 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.deleteConfigurator
.DeleteConfiguratorDialog
;
46 * <p>DeleteTreeNodeHandler class.</p>
52 public class DeleteHandler
extends AbstractHandler
{
54 protected IWorkbenchPage activePage
;
55 protected TaxonNavigator taxonNavigator
;
59 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
61 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
63 taxonNavigator
= NavigationUtil
.showNavigator();
65 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
67 Iterator selectionIterator
= selection
.iterator();
68 Set
<ITaxonTreeNode
> treeNodes
= new HashSet
<ITaxonTreeNode
>();
70 while (selectionIterator
.hasNext()){
71 Object object
= selectionIterator
.next();
72 if(object
instanceof ITaxonTreeNode
) {
73 treeNodes
.add((ITaxonTreeNode
) object
);
76 boolean allEditorsClosed
= true;
77 for (ITaxonTreeNode treeNode
: treeNodes
){
78 if(treeNode
instanceof TaxonNode
) {
79 allEditorsClosed
&= closeObsoleteEditor((TaxonNode
) treeNode
);
82 AbstractPostOperation operation
= null;
83 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
84 if (treeNodes
.size() == 1 ){
87 ITaxonTreeNode treeNode
= treeNodes
.iterator().next();
88 ITaxonTreeNode taxonNode
=treeNode
;
89 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
90 //configNodes.setDeleteTaxon(false);
91 if (taxonNode
instanceof Classification
&& taxonNode
.hasChildNodes()){
92 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.")){
95 } else if (taxonNode
instanceof Classification
&& !taxonNode
.hasChildNodes()){
96 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification?")){
101 if (taxonNode
.hasChildNodes()){
102 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
104 HandlerUtil
.getActiveShell(event
),
107 "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
108 MessageDialog
.WARNING
, new String
[] { "Delete all children",
109 "Move children to parent node", "Skip" }, 0);
110 int result
= dialog
.open();
113 //delete all children
114 configNodes
.setChildHandling(ChildHandling
.DELETE
);
115 config
.setTaxonNodeConfig(configNodes
);
116 } else if (result
== 1){
118 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
119 config
.setTaxonNodeConfig(configNodes
);
120 } else if (result
== 2){
126 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(configNodes
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected node?")){
129 config
.setTaxonNodeConfig(configNodes
);
133 if (allEditorsClosed
){
134 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
137 operation
= new DeleteOperation(
138 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
139 taxonNode
, config
, taxonNavigator
, taxonNavigator
);
141 AbstractUtility
.executeOperation(operation
);
147 } catch (NotDefinedException e
) {
148 MessagingUtils
.warn(getClass(), "Command name not set");
152 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
155 if (allEditorsClosed
){
156 operation
= new DeleteOperation(
157 event
.getCommand().getName(), NavigationUtil
.getUndoContext(),
158 treeNodes
, new TaxonDeletionConfigurator(), taxonNavigator
, taxonNavigator
);
160 AbstractUtility
.executeOperation(operation
);
162 }catch (NotDefinedException e
) {
163 MessagingUtils
.warn(getClass(), "Command name not set");
169 protected boolean closeObsoleteEditor(TaxonNode taxonNode
){
170 boolean result
= true;
171 for (IEditorReference ref
: activePage
.getEditorReferences()) {
173 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
176 IEditorInput input
= ref
.getEditorInput();
177 if (input
instanceof TaxonEditorInput
) {
178 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
179 //if node is a child of taxonNode then close the editor
180 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
181 //if (taxonNode.equals(node)) {
182 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
186 } catch (PartInitException e
) {