005265c78b408c6c66f139426c15ae14a286ce84
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / DeleteHandlerE4.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.e4.handler;
11
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import javax.inject.Named;
17
18 import org.eclipse.e4.core.di.annotations.CanExecute;
19 import org.eclipse.e4.core.di.annotations.Execute;
20 import org.eclipse.e4.ui.di.UISynchronize;
21 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23 import org.eclipse.e4.ui.services.IServiceConstants;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.TreeSelection;
26 import org.eclipse.swt.widgets.Shell;
27
28 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
29 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
30 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
31 import eu.etaxonomy.cdm.model.taxon.Synonym;
32 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
33 import eu.etaxonomy.taxeditor.model.AbstractUtility;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
36 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
37 import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
38 import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
39 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
40 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
41
42 /**
43 *
44 * @author pplitzner
45 * @date 05.09.2017
46 *
47 */
48 public class DeleteHandlerE4 {
49
50 protected static final String SKIP = Messages.DeleteHandler_SKIP;
51 protected static final String MOVE_CHILDREN_TO_PARENT_NODE = Messages.DeleteHandler_MOVE_TO_PARENT;
52 protected static final String DELETE_ALL_CHILDREN = Messages.DeleteHandler_DELETE_ALL;
53 protected static final String THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDNODES;
54 protected static final String THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDREN;
55
56 protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S = Messages.DeleteHandler_DELETE_NODES;
57 protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE = Messages.DeleteHandler_DELETE_NODE;
58
59 protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION = Messages.DeleteHandler_DELETE_CLASSIFICATION;
60 protected static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM_DELETE;
61 protected static final String DELETE = Messages.DeleteHandler_DELETE;
62
63 @Execute
64 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
65 @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
66 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
67 MHandledMenuItem menuItem, UISynchronize sync) {
68
69 TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
70
71 Iterator selectionIterator = selection.iterator();
72 Set<TaxonNodeDto> treeNodes = new HashSet();
73
74 while (selectionIterator.hasNext()){
75 Object object = selectionIterator.next();
76 if(object instanceof TaxonNodeDto) {
77 treeNodes.add((TaxonNodeDto) object);
78 }
79 }
80 boolean allEditorsClosed = true;
81 for (TaxonNodeDto treeNode : treeNodes){
82 allEditorsClosed &= closeObsoleteEditor(treeNode);
83 }
84 AbstractPostOperation operation = null;
85 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
86 config.setDeleteInAllClassifications(false);
87
88 if (treeNodes.size() == 1 ){
89 try {
90
91 TaxonNodeDto taxonNode = treeNodes.iterator().next();
92 // TaxonNodeDto taxonNode = treeNode;
93 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
94
95 //configNodes.setDeleteTaxon(false);
96 if (taxonNode.getTaxonUuid() == null && taxonNode.getTaxonomicChildrenCount()>0){
97 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO)){
98 return;
99 }
100 } else if (taxonNode.getTaxonUuid() != null && taxonNode.getTaxonomicChildrenCount()==0){
101 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION)){
102 return;
103 }
104 } else {
105
106 if (taxonNode.getTaxonomicChildrenCount()>0){
107 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
108 config,
109 shell,
110 CONFIRM_DELETION,
111 null,
112 DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE+THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
113 MessageDialog.WARNING, new String[] { DELETE_ALL_CHILDREN,
114 MOVE_CHILDREN_TO_PARENT_NODE, SKIP }, 0);
115 int dialog_result = dialog.open();
116
117 if (dialog_result == 0){
118 //delete all children
119 configNodes.setChildHandling(ChildHandling.DELETE);
120 config.setTaxonNodeConfig(configNodes);
121 } else if (dialog_result == 1){
122 //move children
123 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
124 config.setTaxonNodeConfig(configNodes);
125 } else if (dialog_result == 2){
126 //skip
127 return;
128
129 }
130 }else{
131 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
132 return;
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(menuItem.getLocalizedLabel(),
144 NavigationUtil.getUndoContext(),
145 taxonNode,
146 config,
147 taxonNavigator,
148 taxonNavigator,
149 taxonNavigator);
150
151 AbstractUtility.executeOperation(operation, sync);
152
153 //}
154 }
155
156
157
158 } catch (Exception e){
159 MessagingUtils.error(getClass(), e);
160 }
161 } else{
162 try{
163 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
164 return;
165 }
166 if (allEditorsClosed){
167
168 operation = new DeleteOperation(menuItem.getLocalizedLabel(),
169 NavigationUtil.getUndoContext(),
170 treeNodes,
171 new TaxonDeletionConfigurator(),
172 taxonNavigator,
173 taxonNavigator,
174 taxonNavigator);
175
176 AbstractUtility.executeOperation(operation, sync);
177
178 }
179 }catch (Exception e){
180 MessagingUtils.error(getClass(), e);
181 }
182 }
183 return;
184 }
185
186 @CanExecute
187 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
188 boolean canExecute = false;
189 canExecute = !selection.isEmpty();
190 Object[] array = selection.toArray();
191 for (Object object : array) {
192 canExecute &= !(object instanceof Synonym);
193 }
194 menuItem.setVisible(canExecute);
195 return canExecute;
196 }
197
198 protected boolean closeObsoleteEditor(TaxonNodeDto taxonNode){
199 //FIXME E4 migrate
200 boolean result = true;
201 // for (IEditorReference ref : activePage.getEditorReferences()) {
202 // try {
203 // String treeIndex = ((ITreeNode)taxonNode).treeIndex();
204 //
205 //
206 // IEditorInput input = ref.getEditorInput();
207 // if (input instanceof TaxonEditorInput) {
208 // TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
209 // //if node is a child of taxonNode then close the editor
210 // if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
211 // //if (taxonNode.equals(node)) {
212 // result &= activePage.closeEditor(ref.getEditor(false), true);
213 //
214 // }
215 // }
216 // } catch (PartInitException e) {
217 // continue;
218 // }
219 // }
220 return result;
221 }
222 }