Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / DeleteHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.navigator.handler;
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16
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;
28
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;
43
44 /**
45 * <p>DeleteTreeNodeHandler class.</p>
46 *
47 * @author n.hoffmann
48 * @created 06.04.2009
49 * @version 1.0
50 */
51 public class DeleteHandler extends AbstractHandler{
52
53 protected IWorkbenchPage activePage;
54 protected TaxonNavigator taxonNavigator;
55
56 /** {@inheritDoc} */
57 @Override
58 public Object execute(ExecutionEvent event) throws ExecutionException {
59
60 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
61
62 taxonNavigator = NavigationUtil.showNavigator();
63
64 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
65
66
67 String plural = selection.size() > 1 ? "s" : "";
68 // Prompt user for confirmation
69
70
71
72 Iterator selectionIterator = selection.iterator();
73 Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
74
75 while (selectionIterator.hasNext()){
76 Object object = selectionIterator.next();
77 if(object instanceof ITaxonTreeNode) {
78 treeNodes.add((ITaxonTreeNode) object);
79 }
80 }
81 boolean allEditorsClosed = true;
82 for (ITaxonTreeNode treeNode : treeNodes){
83 if(treeNode instanceof TaxonNode) {
84 allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
85 }
86 }
87 AbstractPostOperation operation = null;
88 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
89 if (treeNodes.size() == 1 ){
90 try {
91
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.")){
98 return null;
99 }
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?")){
102 return null;
103 }
104 } else {
105
106 if (taxonNode.hasChildNodes()){
107 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
108 config,
109 HandlerUtil.getActiveShell(event),
110 "Confirm Deletion",
111 null,
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();
116
117 if (result == 0){
118 //delete all children
119 configNodes.setChildHandling(ChildHandling.DELETE);
120 config.setTaxonNodeConfig(configNodes);
121 } else if (result == 1){
122 //move children
123 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
124 config.setTaxonNodeConfig(configNodes);
125 } else if (result == 2){
126 //skip
127 return null;
128
129 }
130 }else{
131 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
132 return null;
133 }
134 config.setTaxonNodeConfig(configNodes);
135 }
136 }
137
138 if (allEditorsClosed){
139 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
140 return null;
141 }*/
142
143 operation = new DeleteOperation(event.getCommand().getName(),
144 NavigationUtil.getUndoContext(),
145 taxonNode,
146 config,
147 taxonNavigator,
148 taxonNavigator,
149 taxonNavigator);
150
151 NavigationUtil.executeOperation(operation);
152
153 //}
154 }
155
156
157
158 } catch (NotDefinedException e) {
159 MessagingUtils.warn(getClass(), "Command name not set");
160 }
161 } else{
162 try{
163 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
164 return null;
165 }
166 if (allEditorsClosed){
167
168 operation = new DeleteOperation(event.getCommand().getName(),
169 NavigationUtil.getUndoContext(),
170 treeNodes,
171 new TaxonDeletionConfigurator(),
172 taxonNavigator,
173 taxonNavigator,
174 taxonNavigator);
175
176 NavigationUtil.executeOperation(operation);
177
178 }
179 }catch (NotDefinedException e) {
180 MessagingUtils.warn(getClass(), "Command name not set");
181 }
182 }
183 return null;
184 }
185
186 protected boolean closeObsoleteEditor(TaxonNode taxonNode){
187 boolean result = true;
188 for (IEditorReference ref : activePage.getEditorReferences()) {
189 try {
190 String treeIndex = ((ITreeNode)taxonNode).treeIndex();
191
192
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);
200
201 }
202 }
203 } catch (PartInitException e) {
204 continue;
205 }
206 }
207 return result;
208 }
209 }