allow multiple selection for moving taxonnodes
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / RemotingDeleteTaxonNodeHandlerE4.java
1 /**
2 * Copyright (C) 2015 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 package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
10
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.Set;
14
15 import javax.inject.Named;
16
17 import org.eclipse.core.commands.operations.AbstractOperation;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.e4.core.di.annotations.CanExecute;
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.IStructuredSelection;
26 import org.eclipse.jface.viewers.TreeSelection;
27 import org.eclipse.swt.widgets.Shell;
28
29 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
30 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
31 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
32 import eu.etaxonomy.cdm.model.taxon.Classification;
33 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
35 import eu.etaxonomy.taxeditor.editor.EditorUtil;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
38 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
39 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
40 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
41 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
42
43 /**
44 * @author cmathew
45 * @date 22 Jun 2015
46 *
47 */
48 public class RemotingDeleteTaxonNodeHandlerE4 extends RemotingCdmHandlerE4 {
49
50 private TaxonDeletionConfigurator config;
51 private Set<TaxonNodeDto> treeNodes;
52 private Set<TaxonNodeDto> classifications;
53
54 private Set<TaxonNode> taxonNodes;
55 private Set<Classification> classificationList;
56
57 public RemotingDeleteTaxonNodeHandlerE4() {
58 super(TaxonNavigatorLabels.DELETE_TAXON_NODE_LABEL);
59 }
60
61 @Override
62 public IStatus allowOperations(IStructuredSelection selection,
63 Shell shell,
64 MPart activePart,
65 MHandledMenuItem menuItem) {
66
67 Iterator<?> selectionIterator = selection.iterator();
68 treeNodes = new HashSet();
69
70 boolean containsNodeWithChildren = false;
71 boolean containsClassification = false;
72 boolean containsTaxonNodes = false;
73 classifications = new HashSet();
74 while (selectionIterator.hasNext()){
75 Object object = selectionIterator.next();
76 if (object instanceof TaxonNodeDto && ((TaxonNodeDto)object).getTaxonUuid() == null){
77 TaxonNodeDto dto = (TaxonNodeDto)object;
78 classifications.add(dto);
79 containsClassification = true;
80 }else if(object instanceof TaxonNodeDto) {
81 treeNodes.add((TaxonNodeDto) object);
82 }
83 }
84 for (TaxonNodeDto treeNode : treeNodes) {
85 EditorUtil.closeObsoleteEditor(treeNode, partService);
86 }
87
88 config = new TaxonDeletionConfigurator();
89 Iterator it = treeNodes.iterator();
90
91 treeNodes = new HashSet();
92 while (it.hasNext()){
93 TaxonNodeDto treeNode = (TaxonNodeDto) it.next();
94
95 treeNodes.add(treeNode);
96 if (treeNode == null){
97 MessagingUtils.informationDialog(Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED, Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED_MESSAGE);
98 return Status.CANCEL_STATUS;
99
100 }
101 if (treeNode instanceof TaxonNodeDto){
102 containsTaxonNodes = true;
103 if (treeNode.getTaxonomicChildrenCount()>0){
104 containsNodeWithChildren = true;
105 }
106 }
107
108
109 }
110
111 for (TaxonNodeDto rootNode: classifications){
112
113 if (rootNode.getTaxonomicChildrenCount() > 0) {
114 containsNodeWithChildren = true;
115 }
116
117 }
118 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
119 if (containsClassification && !containsTaxonNodes) {
120 String message;
121 if (containsClassification && containsNodeWithChildren) {
122 message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+DeleteHandlerE4.THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO;
123 } else {
124 message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION;
125 }
126 if (!MessageDialog.openConfirm(
127 shell, DeleteHandlerE4.CONFIRM_DELETION,
128 message)) {
129 return Status.CANCEL_STATUS;
130 }
131 }
132 else {
133 String confirmMessage= treeNodes.size() == 1?DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE:DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S;
134 if (containsNodeWithChildren){
135
136 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
137 config,
138 shell,
139 DeleteHandlerE4.CONFIRM_DELETION,
140 null,
141 DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE+DeleteHandlerE4.THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
142 MessageDialog.WARNING, new String[] { DeleteHandlerE4.DELETE_ALL_CHILDREN,
143 DeleteHandlerE4.MOVE_CHILDREN_TO_PARENT_NODE, DeleteHandlerE4.SKIP }, 0);
144 int result = dialog.open();
145
146 if (result == 0){
147 //delete all children
148 configNodes.setChildHandling(ChildHandling.DELETE);
149 config.setTaxonNodeConfig(configNodes);
150 } else if (result == 1){
151 //move children
152 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
153 config.setTaxonNodeConfig(configNodes);
154 } else {
155 return Status.CANCEL_STATUS;
156 }
157 } else{
158
159 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
160 config,
161 shell,
162 DeleteHandlerE4.CONFIRM_DELETION,
163 null,
164 confirmMessage,
165 MessageDialog.WARNING, new String[] { DeleteHandlerE4.DELETE, DeleteHandlerE4.SKIP }, 0);
166 int result = dialog.open();
167 if (result == 0){
168 //delete all children
169 configNodes.setChildHandling(ChildHandling.DELETE);
170 config.setTaxonNodeConfig(configNodes);
171 } else {
172 return Status.CANCEL_STATUS;
173 }
174 // if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, DeleteHandlerE4.CONFIRM_DELETION, DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
175 // return Status.CANCEL_STATUS;
176 // }
177 // config.setTaxonNodeConfig(configNodes);
178 }
179 }
180 // }
181 return Status.OK_STATUS;
182 }
183
184 @Override
185 public AbstractOperation prepareOperation(IStructuredSelection selection,
186 Shell shell,
187 MPart activePart,
188 MHandledMenuItem menuItem) {
189 return new RemotingDeleteTaxonNodeOperation(getTrigger(),
190 false,
191 treeNodes,
192 classifications,
193 config);
194 }
195
196 @CanExecute
197 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
198 boolean canExecute = false;
199 Object selectionElement = selection.getFirstElement();
200
201 canExecute = (selectionElement instanceof TaxonNodeDto) ||
202 (selectionElement instanceof TaxonNode && ((TaxonNode)selectionElement).getTaxon() == null);
203 menuItem.setVisible(canExecute);
204 return canExecute;
205 }
206
207 @Override
208 public void onComplete() {
209
210 // CdmApplicationState.getCurrentDataChangeService()
211 // .fireChangeEvent(new CdmChangeEvent(Action.Delete, treeNodes, this), true);
212 }
213
214 /**
215 * {@inheritDoc}
216 */
217 @Override
218 protected Object getTrigger() {
219 return this;
220 }
221
222 }