2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
.e4
.handler
;
12 import java
.util
.HashSet
;
13 import java
.util
.Iterator
;
16 import javax
.inject
.Named
;
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
;
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
;
48 public class DeleteHandlerE4
{
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
;
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
;
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
;
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
) {
69 TaxonNavigatorE4 taxonNavigator
= (TaxonNavigatorE4
) activePart
.getObject();
71 Iterator selectionIterator
= selection
.iterator();
72 Set
<TaxonNodeDto
> treeNodes
= new HashSet();
74 while (selectionIterator
.hasNext()){
75 Object object
= selectionIterator
.next();
76 if(object
instanceof TaxonNodeDto
) {
77 treeNodes
.add((TaxonNodeDto
) object
);
80 boolean allEditorsClosed
= true;
81 for (TaxonNodeDto treeNode
: treeNodes
){
82 allEditorsClosed
&= closeObsoleteEditor(treeNode
);
84 AbstractPostOperation operation
= null;
85 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
86 config
.setDeleteInAllClassifications(false);
88 if (treeNodes
.size() == 1 ){
91 TaxonNodeDto taxonNode
= treeNodes
.iterator().next();
92 // TaxonNodeDto taxonNode = treeNode;
93 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
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
)){
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
)){
106 if (taxonNode
.getTaxonomicChildrenCount()>0){
107 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(
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();
117 if (dialog_result
== 0){
118 //delete all children
119 configNodes
.setChildHandling(ChildHandling
.DELETE
);
120 config
.setTaxonNodeConfig(configNodes
);
121 } else if (dialog_result
== 1){
123 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
124 config
.setTaxonNodeConfig(configNodes
);
125 } else if (dialog_result
== 2){
131 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(configNodes
, shell
, CONFIRM_DELETION
, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S
)){
134 config
.setTaxonNodeConfig(configNodes
);
138 if (allEditorsClosed
){
139 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
143 operation
= new DeleteOperation(menuItem
.getLocalizedLabel(),
144 NavigationUtil
.getUndoContext(),
151 AbstractUtility
.executeOperation(operation
, sync
);
158 } catch (Exception e
){
159 MessagingUtils
.error(getClass(), e
);
163 if(!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, shell
, CONFIRM_DELETION
, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S
)){
166 if (allEditorsClosed
){
168 operation
= new DeleteOperation(menuItem
.getLocalizedLabel(),
169 NavigationUtil
.getUndoContext(),
171 new TaxonDeletionConfigurator(),
176 AbstractUtility
.executeOperation(operation
, sync
);
179 }catch (Exception e
){
180 MessagingUtils
.error(getClass(), e
);
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
);
194 menuItem
.setVisible(canExecute
);
198 protected boolean closeObsoleteEditor(TaxonNodeDto taxonNode
){
200 boolean result
= true;
201 // for (IEditorReference ref : activePage.getEditorReferences()) {
203 // String treeIndex = ((ITreeNode)taxonNode).treeIndex();
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);
216 // } catch (PartInitException e) {