Updated version in pom / project files to taxeditor version : 3.8.0-SNAPSHOT and...
[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.AbstractUtility;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
40 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
41 import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
42 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
43 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
44
45 /**
46 * <p>DeleteTreeNodeHandler class.</p>
47 *
48 * @author n.hoffmann
49 * @created 06.04.2009
50 * @version 1.0
51 */
52 public class DeleteHandler extends AbstractHandler{
53
54 protected IWorkbenchPage activePage;
55 protected TaxonNavigator taxonNavigator;
56
57 /** {@inheritDoc} */
58 @Override
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60
61 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
62
63 taxonNavigator = NavigationUtil.showNavigator();
64
65 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
66
67 Iterator selectionIterator = selection.iterator();
68 Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
69
70 while (selectionIterator.hasNext()){
71 Object object = selectionIterator.next();
72 if(object instanceof ITaxonTreeNode) {
73 treeNodes.add((ITaxonTreeNode) object);
74 }
75 }
76 boolean allEditorsClosed = true;
77 for (ITaxonTreeNode treeNode : treeNodes){
78 if(treeNode instanceof TaxonNode) {
79 allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
80 }
81 }
82 AbstractPostOperation operation = null;
83 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
84 if (treeNodes.size() == 1 ){
85 try {
86
87 ITaxonTreeNode treeNode = treeNodes.iterator().next();
88 ITaxonTreeNode taxonNode =treeNode;
89 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
90 if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
91 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.")){
92 return null;
93 }
94 } else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
95 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
96 return null;
97 }
98 } else {
99
100 if (taxonNode.hasChildNodes()){
101 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
102 config,
103 HandlerUtil.getActiveShell(event),
104 "Confirm Deletion",
105 null,
106 "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
107 MessageDialog.WARNING, new String[] { "Delete all children",
108 "Move children to parent node", "Skip" }, 0);
109 int result = dialog.open();
110
111 if (result == 0){
112 //delete all children
113 configNodes.setChildHandling(ChildHandling.DELETE);
114 config.setTaxonNodeConfig(configNodes);
115 } else if (result == 1){
116 //move children
117 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
118 config.setTaxonNodeConfig(configNodes);
119 } else if (result == 2){
120 //skip
121 return null;
122
123 }
124 }else{
125 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
126 return null;
127 }
128 }
129 }
130
131 if (allEditorsClosed){
132 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
133 return null;
134 }*/
135 operation = new DeleteOperation(
136 event.getCommand().getName(), NavigationUtil.getUndoContext(),
137 taxonNode, config, taxonNavigator, taxonNavigator);
138
139 AbstractUtility.executeOperation(operation);
140 //}
141 }
142
143
144
145 } catch (NotDefinedException e) {
146 MessagingUtils.warn(getClass(), "Command name not set");
147 }
148 } else{
149 try{
150 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
151 return null;
152 }
153 if (allEditorsClosed){
154 operation = new DeleteOperation(
155 event.getCommand().getName(), NavigationUtil.getUndoContext(),
156 treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
157
158 AbstractUtility.executeOperation(operation);
159 }
160 }catch (NotDefinedException e) {
161 MessagingUtils.warn(getClass(), "Command name not set");
162 }
163 }
164 return null;
165 }
166
167 protected boolean closeObsoleteEditor(TaxonNode taxonNode){
168 boolean result = true;
169 for (IEditorReference ref : activePage.getEditorReferences()) {
170 try {
171 String treeIndex = ((ITreeNode)taxonNode).treeIndex();
172
173
174 IEditorInput input = ref.getEditorInput();
175 if (input instanceof TaxonEditorInput) {
176 TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
177 //if node is a child of taxonNode then close the editor
178 if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
179 //if (taxonNode.equals(node)) {
180 result &= activePage.closeEditor(ref.getEditor(false), true);
181
182 }
183 }
184 } catch (PartInitException e) {
185 continue;
186 }
187 }
188 return result;
189 }
190 }