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
.ArrayList
;
12 import java
.util
.List
;
13 import java
.util
.UUID
;
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
.TreeSelection
;
26 import org
.eclipse
.swt
.widgets
.Shell
;
28 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolderMock
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
30 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
31 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
32 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigatorLabels
;
33 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TreeNodeDropAdapter
.MovingType
;
34 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.RemotingMoveTaxonOperation
;
35 import eu
.etaxonomy
.taxeditor
.operation
.e4
.RemotingCdmHandlerE4
;
36 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
37 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
44 public class RemotingMoveTaxonNodeHandlerE4
extends RemotingCdmHandlerE4
{
46 private TaxonNode oldTaxonNode
;
48 public RemotingMoveTaxonNodeHandlerE4() {
49 super(TaxonNavigatorLabels
.MOVE_TAXON_LABEL
);
53 public IStatus
allowOperations(TreeSelection selection
,
56 MHandledMenuItem menuItem
) {
57 // check that only a single taxon tree node has been selected
58 if(selection
.size() > 1) {
59 return new Status(IStatus
.ERROR
,
60 "unknown", //$NON-NLS-1$
61 TaxonNavigatorLabels
.SINGLE_TAXON_SELECTION_MESSAGE
);
64 // check for no taxon tree node selected
65 if(selection
.size() == 0) {
66 return new Status(IStatus
.ERROR
,
67 "unknown", //$NON-NLS-1$
68 TaxonNavigatorLabels
.NO_TAXON_SELECTION_MESSAGE
);
70 // check that selected object is a taxon node
71 Object obj
= selection
.iterator().next();
72 if(obj
instanceof TaxonNode
) {
73 oldTaxonNode
= (TaxonNode
)obj
;
75 return new Status(IStatus
.ERROR
,
76 "unknown", //$NON-NLS-1$
77 TaxonNavigatorLabels
.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE
);
79 return Status
.OK_STATUS
;
83 public AbstractOperation
prepareOperation(TreeSelection selection
,
86 MHandledMenuItem menuItem
) {
87 TaxonNode parentTaxonNode
;
89 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
90 excludeTaxa
.add(oldTaxonNode
.getTaxon().getUuid());
92 MovingType moveToNewParent
= MovingType
.CHILD
;
94 if (PreferencesUtil
.getSortNodesNaturally()){
97 parentTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
98 new ConversationHolderMock(),
99 Messages
.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON
,
102 oldTaxonNode
.getClassification());
103 String
[] buttonLables
= {Messages
.RemotingMoveTaxonNodeHandler_CHILD
, Messages
.RemotingMoveTaxonNodeHandler_BEHIND
,Messages
.RemotingMoveTaxonNodeHandler_CANCEL
};
104 MessageDialog dialog
= new MessageDialog(null, Messages
.RemotingMoveTaxonNodeHandler_TARGET_NODE
, null, Messages
.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE
, MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
106 int returnCode
= dialog
.getReturnCode();
107 if (returnCode
== 0){
108 moveToNewParent
= MovingType
.CHILD
;
109 } else if (returnCode
== 1){
110 moveToNewParent
= MovingType
.BEHIND
;
113 parentTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
114 new ConversationHolderMock(),
115 Messages
.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT
,
118 oldTaxonNode
.getClassification());
122 if(parentTaxonNode
!= null){
123 if(NavigationUtil
.isDirty(parentTaxonNode
)){
124 MessageDialog
.openWarning(shell
,
125 Messages
.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT
,
126 Messages
.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE
);
130 return new RemotingMoveTaxonOperation(getTrigger(),
132 oldTaxonNode
.getUuid(),
133 parentTaxonNode
.getUuid(),
141 private boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)TreeSelection selection
, MHandledMenuItem menuItem
){
142 boolean canExecute
= false;
143 canExecute
= selection
.getFirstElement() instanceof TaxonNode
144 && ((TaxonNode
) selection
.getFirstElement()).getTaxon()!=null;
145 menuItem
.setVisible(canExecute
);
150 public void onComplete() {
157 protected Object
getTrigger() {