Merge branch 'release/3.12.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.NodeDeletionConfigurator.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 config.setDeleteInAllClassifications(false);
90
91 if (treeNodes.size() == 1 ){
92 try {
93
94 ITaxonTreeNode treeNode = treeNodes.iterator().next();
95 ITaxonTreeNode taxonNode =treeNode;
96 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
97
98 //configNodes.setDeleteTaxon(false);
99 if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
100 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.")){
101 return null;
102 }
103 } else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
104 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
105 return null;
106 }
107 } else {
108
109 if (taxonNode.hasChildNodes()){
110 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
111 config,
112 HandlerUtil.getActiveShell(event),
113 "Confirm Deletion",
114 null,
115 "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
116 MessageDialog.WARNING, new String[] { "Delete all children",
117 "Move children to parent node", "Skip" }, 0);
118 int dialog_result = dialog.open();
119
120 if (dialog_result == 0){
121 //delete all children
122 configNodes.setChildHandling(ChildHandling.DELETE);
123 config.setTaxonNodeConfig(configNodes);
124 } else if (dialog_result == 1){
125 //move children
126 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
127 config.setTaxonNodeConfig(configNodes);
128 } else if (dialog_result == 2){
129 //skip
130 return null;
131
132 }
133 }else{
134 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
135 return null;
136 }
137 config.setTaxonNodeConfig(configNodes);
138 }
139 }
140
141 if (allEditorsClosed){
142 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
143 return null;
144 }*/
145
146 operation = new DeleteOperation(event.getCommand().getName(),
147 NavigationUtil.getUndoContext(),
148 taxonNode,
149 config,
150 taxonNavigator,
151 taxonNavigator,
152 taxonNavigator);
153
154 NavigationUtil.executeOperation(operation);
155
156 //}
157 }
158
159
160
161 } catch (NotDefinedException e) {
162 MessagingUtils.warn(getClass(), "Command name not set");
163 } catch (Exception e){
164 MessagingUtils.error(getClass(), e);
165 }
166 } else{
167 try{
168 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
169 return null;
170 }
171 if (allEditorsClosed){
172
173 operation = new DeleteOperation(event.getCommand().getName(),
174 NavigationUtil.getUndoContext(),
175 treeNodes,
176 new TaxonDeletionConfigurator(),
177 taxonNavigator,
178 taxonNavigator,
179 taxonNavigator);
180
181 NavigationUtil.executeOperation(operation);
182
183 }
184 }catch (NotDefinedException e) {
185 MessagingUtils.warn(getClass(), "Command name not set");
186 } catch (Exception e){
187 MessagingUtils.error(getClass(), e);
188 }
189 }
190 return null;
191 }
192
193 protected boolean closeObsoleteEditor(TaxonNode taxonNode){
194 boolean result = true;
195 for (IEditorReference ref : activePage.getEditorReferences()) {
196 try {
197 String treeIndex = ((ITreeNode)taxonNode).treeIndex();
198
199
200 IEditorInput input = ref.getEditorInput();
201 if (input instanceof TaxonEditorInput) {
202 TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
203 //if node is a child of taxonNode then close the editor
204 if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
205 //if (taxonNode.equals(node)) {
206 result &= activePage.closeEditor(ref.getEditor(false), true);
207
208 }
209 }
210 } catch (PartInitException e) {
211 continue;
212 }
213 }
214 return result;
215 }
216 }