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
;
14 import java
.util
.UUID
;
16 import javax
.inject
.Named
;
18 import org
.eclipse
.core
.commands
.operations
.AbstractOperation
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
22 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
24 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
25 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.TreeSelection
;
28 import org
.eclipse
.swt
.widgets
.Shell
;
30 import eu
.etaxonomy
.cdm
.model
.metadata
.SecReferenceHandlingEnum
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
34 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
35 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigatorLabels
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.e4
.TreeNodeDropAdapterE4
.MovingType
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
41 import eu
.etaxonomy
.taxeditor
.operation
.e4
.CdmHandlerE4
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
43 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.ReferenceSelectionDialog
;
44 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
50 public class MoveTaxonNodeHandlerE4
extends CdmHandlerE4
{
52 private Set
<UUID
> oldTaxonNodeUUIDs
= new HashSet
<>();
53 private UUID classificationUuid
= null;
54 boolean isPublish
= true;
55 SecReferenceHandlingEnum secHandling
;
57 private Set
<UUID
> oldTaxonSecUUIDs
= new HashSet
<>();
59 public MoveTaxonNodeHandlerE4() {
60 super(TaxonNavigatorLabels
.MOVE_TAXON_LABEL
);
64 public IStatus
allowOperations(IStructuredSelection selection
,
67 MHandledMenuItem menuItem
) {
68 // check for no taxon tree node selected
69 if(selection
.size() == 0) {
70 return new Status(IStatus
.ERROR
,
71 "unknown", //$NON-NLS-1$
72 TaxonNavigatorLabels
.NO_TAXON_SELECTION_MESSAGE
);
74 // check that selected object is a taxon node
77 Iterator
<?
> iter
= selection
.iterator();
78 oldTaxonNodeUUIDs
= new HashSet
<>();
80 while (iter
.hasNext()){
82 if(obj
instanceof TaxonNodeDto
) {
83 oldTaxonNodeUUIDs
.add(((TaxonNodeDto
)obj
).getUuid());
84 oldTaxonSecUUIDs
.add(((TaxonNodeDto
)obj
).getSecUuid());
85 isPublish
&= ((TaxonNodeDto
)obj
).isPublish();
86 if (classificationUuid
== null){
87 classificationUuid
= ((TaxonNodeDto
)obj
).getClassificationUUID();
90 return new Status(IStatus
.ERROR
,
91 "unknown", //$NON-NLS-1$
92 TaxonNavigatorLabels
.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE
);
95 return Status
.OK_STATUS
;
99 public AbstractOperation
prepareOperation(IStructuredSelection selection
,
102 MHandledMenuItem menuItem
) {
103 TaxonNode parentTaxonNode
;
104 MovingType moveToNewParent
= MovingType
.CHILD
;
106 if (PreferencesUtil
.isNodesSortedNaturally()){
107 parentTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
108 Messages
.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON
,
111 classificationUuid
, true);
112 String
[] buttonLables
= {Messages
.RemotingMoveTaxonNodeHandler_CHILD
, Messages
.RemotingMoveTaxonNodeHandler_BEHIND
,Messages
.RemotingMoveTaxonNodeHandler_CANCEL
};
113 MessageDialog dialog
= new MessageDialog(null, Messages
.RemotingMoveTaxonNodeHandler_TARGET_NODE
, null, Messages
.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE
, MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
115 int returnCode
= dialog
.getReturnCode();
116 if (returnCode
== 0){
117 moveToNewParent
= MovingType
.CHILD
;
118 } else if (returnCode
== 1){
119 moveToNewParent
= MovingType
.BEHIND
;
122 parentTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
123 Messages
.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT
,
126 classificationUuid
, true);
128 if (parentTaxonNode
== null){
132 if (parentTaxonNode
.getTaxon() != null && parentTaxonNode
.getTaxon().isPublish() != isPublish
){
133 MessageDialog
.openWarning(shell
,
134 Messages
.RemotingMoveTaxonNodeHandler_DIFFERENT_PUBLISH_TITLE
,
135 Messages
.RemotingMoveTaxonNodeHandler_DIFFERENT_PUBLISH_MESSAGE
);
138 secHandling
= PreferencesUtil
.getSecReferenceHandlingPreference();
140 UUID parentTaxonSecUuid
= parentTaxonNode
.getTaxon() != null && parentTaxonNode
.getTaxon().getSec() != null ? parentTaxonNode
.getTaxon().getSec().getUuid(): null;
141 if (secHandling
.equals(SecReferenceHandlingEnum
.AlwaysSelect
) || (oldTaxonSecUUIDs
.size() > 1 && secHandling
.equals(SecReferenceHandlingEnum
.KeepOrSelect
)) || (oldTaxonSecUUIDs
.size() == 1 && secHandling
.equals(SecReferenceHandlingEnum
.KeepOrSelect
) && !oldTaxonSecUUIDs
.contains(parentTaxonSecUuid
))){
142 //The moved nodes have different secundum references
143 String message
= null;
144 String
[] options
= null;
145 if (secHandling
.equals(SecReferenceHandlingEnum
.AlwaysSelect
) && (parentTaxonSecUuid
!= null && oldTaxonSecUUIDs
.contains(parentTaxonSecUuid
))){
146 message
= Messages
.TreeNodeDropAdapter_Select_Sec_Reference_Handling_message_always_select
;
147 options
= new String
[]{Messages
.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep
, Messages
.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select
};
149 message
= Messages
.TreeNodeDropAdapter_Select_Sec_Reference_Handling_message
;
150 options
= new String
[]{Messages
.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep
, Messages
.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select
, Messages
.TreeNodeDropAdapter_Select_Sec_Reference_Parent
, };
152 int result
= MessagingUtils
.confirmDialog(Messages
.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title
, message
, options
);
154 // int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
155 // new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
157 //select new reference
158 Reference sec
= ReferenceSelectionDialog
.select(AbstractUtility
.getShell(), null);
159 newSecUuid
= sec
!= null? sec
.getUuid(): null;
160 }else if (result
== 2){
162 secHandling
= SecReferenceHandlingEnum
.UseNewParentSec
;
163 newSecUuid
= parentTaxonSecUuid
;
164 }else if (result
== 0){
165 //keep sec (also homotypic synonyms with different sec will keep the secundum)
166 secHandling
= SecReferenceHandlingEnum
.KeepOrWarn
;
174 if(parentTaxonNode
!= null){
175 if(NavigationUtil
.isDirty(parentTaxonNode
, partService
)){
176 MessageDialog
.openWarning(shell
,
177 Messages
.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT
,
178 Messages
.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE
);
182 return new MoveTaxonOperation(getTrigger(),
185 parentTaxonNode
.getUuid(),
195 private boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)TreeSelection selection
, MHandledMenuItem menuItem
){
196 boolean canExecute
= false;
197 canExecute
= selection
.getFirstElement() instanceof TaxonNodeDto
198 && ((TaxonNodeDto
)selection
.getFirstElement()).getTaxonUuid() != null;
199 menuItem
.setVisible(canExecute
);
204 public void onComplete() {
208 protected Object
getTrigger() {