ref #6598 Fix menu item visibility
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / RemotingMoveTaxonNodeHandlerE4.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import javax.inject.Named;
16
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;
27
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;
38
39 /**
40 * @author cmathew
41 * @date 19 Jun 2015
42 *
43 */
44 public class RemotingMoveTaxonNodeHandlerE4 extends RemotingCdmHandlerE4 {
45
46 private TaxonNode oldTaxonNode;
47
48 public RemotingMoveTaxonNodeHandlerE4() {
49 super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
50 }
51
52 @Override
53 public IStatus allowOperations(TreeSelection selection,
54 Shell shell,
55 MPart activePart,
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);
62 }
63
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);
69 }
70 // check that selected object is a taxon node
71 Object obj = selection.iterator().next();
72 if(obj instanceof TaxonNode) {
73 oldTaxonNode = (TaxonNode)obj;
74 } else {
75 return new Status(IStatus.ERROR,
76 "unknown", //$NON-NLS-1$
77 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
78 }
79 return Status.OK_STATUS;
80 }
81
82 @Override
83 public AbstractOperation prepareOperation(TreeSelection selection,
84 Shell shell,
85 MPart activePart,
86 MHandledMenuItem menuItem) {
87 TaxonNode parentTaxonNode;
88
89 List<UUID> excludeTaxa = new ArrayList<UUID>();
90 excludeTaxa.add(oldTaxonNode.getTaxon().getUuid());
91
92 MovingType moveToNewParent = MovingType.CHILD;
93
94 if (PreferencesUtil.getSortNodesNaturally()){
95
96
97 parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
98 new ConversationHolderMock(),
99 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON,
100 excludeTaxa,
101 null,
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);
105 dialog.open();
106 int returnCode = dialog.getReturnCode();
107 if (returnCode == 0){
108 moveToNewParent = MovingType.CHILD;
109 } else if (returnCode == 1){
110 moveToNewParent = MovingType.BEHIND;
111 }
112 } else {
113 parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
114 new ConversationHolderMock(),
115 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT,
116 excludeTaxa,
117 oldTaxonNode,
118 oldTaxonNode.getClassification());
119 }
120
121
122 if(parentTaxonNode != null){
123 if(NavigationUtil.isDirty(parentTaxonNode)){
124 MessageDialog.openWarning(shell,
125 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
126 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
127 return null;
128 }
129
130 return new RemotingMoveTaxonOperation(getTrigger(),
131 false,
132 oldTaxonNode.getUuid(),
133 parentTaxonNode.getUuid(),
134 moveToNewParent);
135 }
136
137 return null;
138 }
139
140 @CanExecute
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);
146 return canExecute;
147 }
148
149 @Override
150 public void onComplete() {
151 }
152
153 /**
154 * {@inheritDoc}
155 */
156 @Override
157 protected Object getTrigger() {
158 return this;
159 }
160 }