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