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
.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
;
50 public class RemotingDeleteTaxonNodeHandlerE4
extends RemotingCdmHandlerE4
{
52 private TaxonDeletionConfigurator config
;
53 private Set
<ITaxonTreeNode
> treeNodes
;
55 public RemotingDeleteTaxonNodeHandlerE4() {
56 super(TaxonNavigatorLabels
.DELETE_TAXON_NODE_LABEL
);
60 public IStatus
allowOperations(IStructuredSelection selection
,
63 MHandledMenuItem menuItem
) {
65 Iterator
<?
> selectionIterator
= selection
.iterator();
66 treeNodes
= new HashSet
<ITaxonTreeNode
>();
68 while (selectionIterator
.hasNext()){
69 Object object
= selectionIterator
.next();
70 if(object
instanceof ITaxonTreeNode
) {
71 treeNodes
.add((ITaxonTreeNode
) object
);
74 for (ITaxonTreeNode treeNode
: treeNodes
) {
75 if(treeNode
instanceof TaxonNode
) {
76 EditorUtil
.closeObsoleteEditor((TaxonNode
) treeNode
, partService
);
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
>();
87 ITaxonTreeNode treeNode
= (ITaxonTreeNode
) it
.next();
89 if (treeNode
instanceof TaxonNode
&& !((TaxonNode
)treeNode
).hasTaxon()){
90 treeNode
= CdmApplicationState
.getCurrentAppConfig().getClassificationService().load(((TaxonNode
)treeNode
).getClassification().getUuid());
92 treeNode
= CdmApplicationState
.getCurrentAppConfig().getTaxonNodeService().load(treeNode
.getUuid());
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 Classification
){
102 containsClassification
= true;
103 if (((Classification
)treeNode
).getRootNode() != null &&treeNode
.hasChildNodes()) {
104 containsNodeWithChildren
= true;
106 }else if (treeNode
instanceof TaxonNode
){
107 containsTaxonNodes
= true;
108 if (treeNode
.hasChildNodes()){
109 containsNodeWithChildren
= true;
115 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
116 if (containsClassification
&& !containsTaxonNodes
) {
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
;
121 message
= DeleteHandlerE4
.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION
;
123 if (!MessageDialog
.openConfirm(
124 shell
, DeleteHandlerE4
.CONFIRM_DELETION
,
126 return Status
.CANCEL_STATUS
;
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
){
133 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
136 DeleteHandlerE4
.CONFIRM_DELETION
,
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();
144 //delete all children
145 configNodes
.setChildHandling(ChildHandling
.DELETE
);
146 config
.setTaxonNodeConfig(configNodes
);
147 } else if (result
== 1){
149 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
150 config
.setTaxonNodeConfig(configNodes
);
151 } else if (result
== 2){
152 return Status
.CANCEL_STATUS
;
156 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
159 DeleteHandlerE4
.CONFIRM_DELETION
,
162 MessageDialog
.WARNING
, new String
[] { DeleteHandlerE4
.DELETE
, DeleteHandlerE4
.SKIP
}, 0);
163 int result
= dialog
.open();
165 //delete all children
166 configNodes
.setChildHandling(ChildHandling
.DELETE
);
167 config
.setTaxonNodeConfig(configNodes
);
169 return Status
.CANCEL_STATUS
;
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;
174 // config.setTaxonNodeConfig(configNodes);
178 return Status
.OK_STATUS
;
182 public AbstractOperation
prepareOperation(IStructuredSelection selection
,
185 MHandledMenuItem menuItem
) {
186 return new RemotingDeleteTaxonNodeOperation(getTrigger(),
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
);
201 public void onComplete() {
208 protected Object
getTrigger() {