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
.IStructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.TreeSelection
;
27 import org
.eclipse
.swt
.widgets
.Shell
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
31 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
32 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
33 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
34 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigatorLabels
;
35 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.e4
.TreeNodeDropAdapterE4
.MovingType
;
36 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.RemotingMoveTaxonOperation
;
37 import eu
.etaxonomy
.taxeditor
.operation
.e4
.RemotingCdmHandlerE4
;
38 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
47 public class RemotingMoveTaxonNodeHandlerE4
extends RemotingCdmHandlerE4
{
49 private TaxonNodeDto oldTaxonNode
;
51 public RemotingMoveTaxonNodeHandlerE4() {
52 super(TaxonNavigatorLabels
.MOVE_TAXON_LABEL
);
56 public IStatus
allowOperations(IStructuredSelection selection
,
59 MHandledMenuItem menuItem
) {
60 // check that only a single taxon tree node has been selected
61 if(selection
.size() > 1) {
62 return new Status(IStatus
.ERROR
,
63 "unknown", //$NON-NLS-1$
64 TaxonNavigatorLabels
.SINGLE_TAXON_SELECTION_MESSAGE
);
67 // check for no taxon tree node selected
68 if(selection
.size() == 0) {
69 return new Status(IStatus
.ERROR
,
70 "unknown", //$NON-NLS-1$
71 TaxonNavigatorLabels
.NO_TAXON_SELECTION_MESSAGE
);
73 // check that selected object is a taxon node
74 Object obj
= selection
.iterator().next();
75 if(obj
instanceof TaxonNodeDto
) {
76 oldTaxonNode
= (TaxonNodeDto
)obj
;
78 return new Status(IStatus
.ERROR
,
79 "unknown", //$NON-NLS-1$
80 TaxonNavigatorLabels
.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE
);
82 return Status
.OK_STATUS
;
86 public AbstractOperation
prepareOperation(IStructuredSelection selection
,
89 MHandledMenuItem menuItem
) {
90 TaxonNode parentTaxonNode
;
92 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
93 excludeTaxa
.add(oldTaxonNode
.getTaxonUuid());
95 MovingType moveToNewParent
= MovingType
.CHILD
;
97 if (PreferencesUtil
.getSortNodesNaturally()){
100 parentTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
101 // new ConversationHolderMock(),
102 Messages
.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON
,
105 oldTaxonNode
.getClassificationUUID(), true);
106 String
[] buttonLables
= {Messages
.RemotingMoveTaxonNodeHandler_CHILD
, Messages
.RemotingMoveTaxonNodeHandler_BEHIND
,Messages
.RemotingMoveTaxonNodeHandler_CANCEL
};
107 MessageDialog dialog
= new MessageDialog(null, Messages
.RemotingMoveTaxonNodeHandler_TARGET_NODE
, null, Messages
.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE
, MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
109 int returnCode
= dialog
.getReturnCode();
110 if (returnCode
== 0){
111 moveToNewParent
= MovingType
.CHILD
;
112 } else if (returnCode
== 1){
113 moveToNewParent
= MovingType
.BEHIND
;
116 parentTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
117 // new ConversationHolderMock(),
118 Messages
.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT
,
120 CdmStore
.getService(ITaxonNodeService
.class).find(oldTaxonNode
.getUuid()),
121 oldTaxonNode
.getClassificationUUID(), true);
125 if(parentTaxonNode
!= null){
126 if(NavigationUtil
.isDirty(parentTaxonNode
, partService
)){
127 MessageDialog
.openWarning(shell
,
128 Messages
.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT
,
129 Messages
.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE
);
133 return new RemotingMoveTaxonOperation(getTrigger(),
135 oldTaxonNode
.getUuid(),
136 parentTaxonNode
.getUuid(),
144 private boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)TreeSelection selection
, MHandledMenuItem menuItem
){
145 boolean canExecute
= false;
146 canExecute
= (selection
.getFirstElement() instanceof TaxonNodeDto
) && ((TaxonNodeDto
)selection
.getFirstElement()).getTaxonUuid() != null;
147 menuItem
.setVisible(canExecute
);
152 public void onComplete() {
159 protected Object
getTrigger() {