2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
.handler
;
12 import java
.util
.HashSet
;
13 import java
.util
.Iterator
;
16 import org
.eclipse
.core
.commands
.AbstractHandler
;
17 import org
.eclipse
.core
.commands
.ExecutionEvent
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
20 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
21 import org
.eclipse
.jface
.viewers
.TreeSelection
;
22 import org
.eclipse
.ui
.IEditorInput
;
23 import org
.eclipse
.ui
.IEditorReference
;
24 import org
.eclipse
.ui
.IWorkbenchPage
;
25 import org
.eclipse
.ui
.PartInitException
;
26 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
29 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonNodeDeletionConfigurator
;
30 import eu
.etaxonomy
.cdm
.api
.service
.config
.NodeDeletionConfigurator
.ChildHandling
;
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
;
41 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.deleteConfigurator
.DeleteConfiguratorDialog
;
44 * <p>DeleteTreeNodeHandler class.</p>
50 public class DeleteHandler
extends AbstractHandler
{
52 protected IWorkbenchPage activePage
;
53 protected TaxonNavigator taxonNavigator
;
57 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
59 activePage
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage();
61 taxonNavigator
= NavigationUtil
.showNavigator();
63 TreeSelection selection
= (TreeSelection
) HandlerUtil
.getCurrentSelection(event
);
66 String plural
= selection
.size() > 1 ?
"s" : "";
67 // Prompt user for confirmation
71 Iterator selectionIterator
= selection
.iterator();
72 Set
<ITaxonTreeNode
> treeNodes
= new HashSet
<ITaxonTreeNode
>();
74 while (selectionIterator
.hasNext()){
75 Object object
= selectionIterator
.next();
76 if(object
instanceof ITaxonTreeNode
) {
77 treeNodes
.add((ITaxonTreeNode
) object
);
80 boolean allEditorsClosed
= true;
81 for (ITaxonTreeNode treeNode
: treeNodes
){
82 if(treeNode
instanceof TaxonNode
) {
83 allEditorsClosed
&= closeObsoleteEditor((TaxonNode
) treeNode
);
86 AbstractPostOperation operation
= null;
87 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
88 config
.setDeleteInAllClassifications(false);
90 if (treeNodes
.size() == 1 ){
93 ITaxonTreeNode treeNode
= treeNodes
.iterator().next();
94 ITaxonTreeNode taxonNode
=treeNode
;
95 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
97 //configNodes.setDeleteTaxon(false);
98 if (taxonNode
instanceof Classification
&& taxonNode
.hasChildNodes()){
99 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.")){
102 } else if (taxonNode
instanceof Classification
&& !taxonNode
.hasChildNodes()){
103 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the classification?")){
108 if (taxonNode
.hasChildNodes()){
109 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
111 HandlerUtil
.getActiveShell(event
),
114 "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
115 MessageDialog
.WARNING
, new String
[] { "Delete all children",
116 "Move children to parent node", "Skip" }, 0);
117 int dialog_result
= dialog
.open();
119 if (dialog_result
== 0){
120 //delete all children
121 configNodes
.setChildHandling(ChildHandling
.DELETE
);
122 config
.setTaxonNodeConfig(configNodes
);
123 } else if (dialog_result
== 1){
125 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
126 config
.setTaxonNodeConfig(configNodes
);
127 } else if (dialog_result
== 2){
133 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(configNodes
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected node?")){
136 config
.setTaxonNodeConfig(configNodes
);
140 if (allEditorsClosed
){
141 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
145 operation
= new DeleteOperation(event
.getCommand().getName(),
146 NavigationUtil
.getUndoContext(),
153 NavigationUtil
.executeOperation(operation
);
160 } catch (NotDefinedException e
) {
161 MessagingUtils
.warn(getClass(), "Command name not set");
162 } catch (Exception e
){
163 MessagingUtils
.error(getClass(), e
);
167 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
170 if (allEditorsClosed
){
172 operation
= new DeleteOperation(event
.getCommand().getName(),
173 NavigationUtil
.getUndoContext(),
175 new TaxonDeletionConfigurator(),
180 NavigationUtil
.executeOperation(operation
);
183 }catch (NotDefinedException e
) {
184 MessagingUtils
.warn(getClass(), "Command name not set");
185 } catch (Exception e
){
186 MessagingUtils
.error(getClass(), e
);
192 protected boolean closeObsoleteEditor(TaxonNode taxonNode
){
193 boolean result
= true;
194 for (IEditorReference ref
: activePage
.getEditorReferences()) {
196 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
199 IEditorInput input
= ref
.getEditorInput();
200 if (input
instanceof TaxonEditorInput
) {
201 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
202 //if node is a child of taxonNode then close the editor
203 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
204 //if (taxonNode.equals(node)) {
205 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
209 } catch (PartInitException e
) {