Merge branch 'develop' into treeTable
[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.application.CdmApplicationState;
30 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
31 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
32 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
33 import eu.etaxonomy.cdm.model.taxon.Classification;
34 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
35 import eu.etaxonomy.cdm.model.taxon.Synonym;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.taxeditor.editor.EditorUtil;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
40 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
41 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
42 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
43 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
44
45 /**
46 * @author cmathew
47 * @date 22 Jun 2015
48 *
49 */
50 public class RemotingDeleteTaxonNodeHandlerE4 extends RemotingCdmHandlerE4 {
51
52 private TaxonDeletionConfigurator config;
53 private Set<ITaxonTreeNode> treeNodes;
54
55 public RemotingDeleteTaxonNodeHandlerE4() {
56 super(TaxonNavigatorLabels.DELETE_TAXON_NODE_LABEL);
57 }
58
59 @Override
60 public IStatus allowOperations(IStructuredSelection selection,
61 Shell shell,
62 MPart activePart,
63 MHandledMenuItem menuItem) {
64
65 Iterator<?> selectionIterator = selection.iterator();
66 treeNodes = new HashSet<ITaxonTreeNode>();
67
68 while (selectionIterator.hasNext()){
69 Object object = selectionIterator.next();
70 if(object instanceof ITaxonTreeNode) {
71 treeNodes.add((ITaxonTreeNode) object);
72 }
73 }
74 for (ITaxonTreeNode treeNode : treeNodes) {
75 if(treeNode instanceof TaxonNode) {
76 EditorUtil.closeObsoleteEditor((TaxonNode) treeNode, partService);
77 }
78 }
79
80 config = new TaxonDeletionConfigurator();
81 Iterator it = treeNodes.iterator();
82 boolean containsNodeWithChildren = false;
83 boolean containsClassification = false;
84 boolean containsTaxonNodes = false;
85 treeNodes = new HashSet<ITaxonTreeNode>();
86 while (it.hasNext()){
87 ITaxonTreeNode treeNode = (ITaxonTreeNode) it.next();
88
89 if (treeNode instanceof TaxonNode && !((TaxonNode)treeNode).hasTaxon()){
90 treeNode = CdmApplicationState.getCurrentAppConfig().getClassificationService().load(((TaxonNode)treeNode).getClassification().getUuid());
91 }else{
92 treeNode = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(treeNode.getUuid());
93 }
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 Classification){
102 containsClassification = true;
103 if (((Classification)treeNode).getRootNode() != null &&treeNode.hasChildNodes()) {
104 containsNodeWithChildren = true;
105 }
106 }else if (treeNode instanceof TaxonNode){
107 containsTaxonNodes = true;
108 if (treeNode.hasChildNodes()){
109 containsNodeWithChildren = true;
110 }
111 }
112
113
114 }
115 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
116 if (containsClassification && !containsTaxonNodes) {
117 String message;
118 if (containsClassification && containsNodeWithChildren) {
119 message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+DeleteHandlerE4.THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO;
120 } else {
121 message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION;
122 }
123 if (!MessageDialog.openConfirm(
124 shell, DeleteHandlerE4.CONFIRM_DELETION,
125 message)) {
126 return Status.CANCEL_STATUS;
127 }
128 }
129 else {
130 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;
131 if (containsNodeWithChildren){
132
133 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
134 config,
135 shell,
136 DeleteHandlerE4.CONFIRM_DELETION,
137 null,
138 DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE+DeleteHandlerE4.THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
139 MessageDialog.WARNING, new String[] { DeleteHandlerE4.DELETE_ALL_CHILDREN,
140 DeleteHandlerE4.MOVE_CHILDREN_TO_PARENT_NODE, DeleteHandlerE4.SKIP }, 0);
141 int result = dialog.open();
142
143 if (result == 0){
144 //delete all children
145 configNodes.setChildHandling(ChildHandling.DELETE);
146 config.setTaxonNodeConfig(configNodes);
147 } else if (result == 1){
148 //move children
149 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
150 config.setTaxonNodeConfig(configNodes);
151 } else if (result == 2){
152 return Status.CANCEL_STATUS;
153 }
154 } else{
155
156 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
157 config,
158 shell,
159 DeleteHandlerE4.CONFIRM_DELETION,
160 null,
161 confirmMessage,
162 MessageDialog.WARNING, new String[] { DeleteHandlerE4.DELETE, DeleteHandlerE4.SKIP }, 0);
163 int result = dialog.open();
164 if (result == 0){
165 //delete all children
166 configNodes.setChildHandling(ChildHandling.DELETE);
167 config.setTaxonNodeConfig(configNodes);
168 } else {
169 return Status.CANCEL_STATUS;
170 }
171 // if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, DeleteHandlerE4.CONFIRM_DELETION, DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
172 // return Status.CANCEL_STATUS;
173 // }
174 // config.setTaxonNodeConfig(configNodes);
175 }
176 }
177 // }
178 return Status.OK_STATUS;
179 }
180
181 @Override
182 public AbstractOperation prepareOperation(IStructuredSelection selection,
183 Shell shell,
184 MPart activePart,
185 MHandledMenuItem menuItem) {
186 return new RemotingDeleteTaxonNodeOperation(getTrigger(),
187 false,
188 treeNodes,
189 config);
190 }
191
192 @CanExecute
193 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
194 boolean canExecute = false;
195 canExecute = !selection.isEmpty() && !(selection.getFirstElement() instanceof Synonym);
196 menuItem.setVisible(canExecute);
197 return canExecute;
198 }
199
200 @Override
201 public void onComplete() {
202 }
203
204 /**
205 * {@inheritDoc}
206 */
207 @Override
208 protected Object getTrigger() {
209 return this;
210 }
211
212 }