2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
.e4
.handler
;
11 import java
.util
.HashSet
;
12 import java
.util
.Iterator
;
15 import javax
.inject
.Named
;
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
;
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
;
48 public class RemotingDeleteTaxonNodeHandlerE4
extends RemotingCdmHandlerE4
{
50 private TaxonDeletionConfigurator config
;
51 private Set
<TaxonNodeDto
> treeNodes
;
52 private Set
<TaxonNodeDto
> classifications
;
54 private Set
<TaxonNode
> taxonNodes
;
55 private Set
<Classification
> classificationList
;
57 public RemotingDeleteTaxonNodeHandlerE4() {
58 super(TaxonNavigatorLabels
.DELETE_TAXON_NODE_LABEL
);
62 public IStatus
allowOperations(IStructuredSelection selection
,
65 MHandledMenuItem menuItem
) {
67 Iterator
<?
> selectionIterator
= selection
.iterator();
68 treeNodes
= new HashSet();
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
);
84 for (TaxonNodeDto treeNode
: treeNodes
) {
85 EditorUtil
.closeObsoleteEditor(treeNode
, partService
);
88 config
= new TaxonDeletionConfigurator();
89 Iterator it
= treeNodes
.iterator();
91 treeNodes
= new HashSet();
93 TaxonNodeDto treeNode
= (TaxonNodeDto
) it
.next();
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
;
101 if (treeNode
instanceof TaxonNodeDto
){
102 containsTaxonNodes
= true;
103 if (treeNode
.getTaxonomicChildrenCount()>0){
104 containsNodeWithChildren
= true;
111 for (TaxonNodeDto rootNode
: classifications
){
113 if (rootNode
.getTaxonomicChildrenCount() > 0) {
114 containsNodeWithChildren
= true;
118 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
119 if (containsClassification
&& !containsTaxonNodes
) {
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
;
124 message
= DeleteHandlerE4
.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION
;
126 if (!MessageDialog
.openConfirm(
127 shell
, DeleteHandlerE4
.CONFIRM_DELETION
,
129 return Status
.CANCEL_STATUS
;
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
){
136 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
139 DeleteHandlerE4
.CONFIRM_DELETION
,
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();
147 //delete all children
148 configNodes
.setChildHandling(ChildHandling
.DELETE
);
149 config
.setTaxonNodeConfig(configNodes
);
150 } else if (result
== 1){
152 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
153 config
.setTaxonNodeConfig(configNodes
);
155 return Status
.CANCEL_STATUS
;
159 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
162 DeleteHandlerE4
.CONFIRM_DELETION
,
165 MessageDialog
.WARNING
, new String
[] { DeleteHandlerE4
.DELETE
, DeleteHandlerE4
.SKIP
}, 0);
166 int result
= dialog
.open();
168 //delete all children
169 configNodes
.setChildHandling(ChildHandling
.DELETE
);
170 config
.setTaxonNodeConfig(configNodes
);
172 return Status
.CANCEL_STATUS
;
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;
177 // config.setTaxonNodeConfig(configNodes);
181 return Status
.OK_STATUS
;
185 public AbstractOperation
prepareOperation(IStructuredSelection selection
,
188 MHandledMenuItem menuItem
) {
189 return new RemotingDeleteTaxonNodeOperation(getTrigger(),
197 private boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)TreeSelection selection
, MHandledMenuItem menuItem
){
198 boolean canExecute
= false;
199 Object selectionElement
= selection
.getFirstElement();
201 canExecute
= (selectionElement
instanceof TaxonNodeDto
) ||
202 (selectionElement
instanceof TaxonNode
&& ((TaxonNode
)selectionElement
).getTaxon() == null);
203 menuItem
.setVisible(canExecute
);
208 public void onComplete() {
210 // CdmApplicationState.getCurrentDataChangeService()
211 // .fireChangeEvent(new CdmChangeEvent(Action.Delete, treeNodes, this), true);
218 protected Object
getTrigger() {