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