ref #6909 Implement canExecute() for remoting handler
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / RemotingChangeAcceptedTaxonToSynonymHandlerE4.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.UUID;
9
10 import javax.inject.Named;
11
12 import org.eclipse.core.commands.operations.AbstractOperation;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.e4.core.di.annotations.CanExecute;
16 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18 import org.eclipse.e4.ui.services.IServiceConstants;
19 import org.eclipse.jface.viewers.TreeSelection;
20 import org.eclipse.swt.widgets.Shell;
21
22 import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
23 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24 import eu.etaxonomy.taxeditor.editor.EditorUtil;
25 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
26 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
27 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingChangeAcceptedTaxonToSynonymOperation;
28 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
29 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
30
31 /**
32 *
33 * @author pplitzner
34 * @since Sep 6, 2017
35 *
36 */
37 public class RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHandlerE4 {
38
39
40 private TaxonNode oldTaxonNode;
41
42 public RemotingChangeAcceptedTaxonToSynonymHandlerE4() {
43 super(TaxonNavigatorLabels.CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL);
44 }
45
46 @Override
47 public IStatus allowOperations(TreeSelection selection,
48 Shell shell,
49 MPart activePart,
50 MHandledMenuItem menuItem) {
51 // check that only a single taxon tree node has been selected
52 if(selection.size() > 1) {
53 return new Status(IStatus.ERROR,
54 "unknown", //$NON-NLS-1$
55 TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
56 }
57
58 // check for no taxon tree node selected
59 if(selection.size() == 0) {
60 return new Status(IStatus.ERROR,
61 "unknown", //$NON-NLS-1$
62 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
63 }
64
65 // check that selected object is a taxon node
66 Object obj = selection.iterator().next();
67 if(obj instanceof TaxonNode && ((TaxonNode)obj).hasTaxon()) {
68 oldTaxonNode = (TaxonNode)obj;
69 } else {
70 if (obj instanceof TaxonNode && !((TaxonNode)obj).hasTaxon()){
71 return new Status(IStatus.ERROR,
72 "Operation not available for Classifications", //$NON-NLS-1$
73 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
74 }
75 return new Status(IStatus.ERROR,
76 "unknown", //$NON-NLS-1$
77 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
78 }
79 // check that the source taxon node does not have children
80 if(oldTaxonNode.getCountChildren() > 0) {
81 return new Status(IStatus.ERROR,
82 "unknown", //$NON-NLS-1$
83 TaxonNavigatorLabels.SOURCE_TAXON_HAS_CHILDREN_MESSAGE);
84
85 }
86
87 // check if corresponding name editor is closed
88 EditorUtil.closeObsoleteEditor(oldTaxonNode, partService);
89
90 return Status.OK_STATUS;
91 }
92
93 @Override
94 public AbstractOperation prepareOperation(TreeSelection selection,
95 Shell shell,
96 MPart activePart,
97 MHandledMenuItem menuItem) {
98 List<UUID> excludeTaxa = new ArrayList<UUID>();
99 excludeTaxa.add(oldTaxonNode.getTaxon().getUuid());
100 TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
101 new ConversationHolderMock(),
102 Messages.RemotingChangeAcceptedTaxonToSynonymHandler_CHOOSE_TAXON,
103 excludeTaxa,
104 oldTaxonNode,
105 oldTaxonNode.getClassification());
106
107 if (newAcceptedTaxonNode == null) {
108 return null;
109 }
110
111 RemotingChangeAcceptedTaxonToSynonymOperation rcattso =
112 new RemotingChangeAcceptedTaxonToSynonymOperation(getTrigger(),
113 false,
114 oldTaxonNode.getUuid(),
115 newAcceptedTaxonNode.getUuid());
116
117 return rcattso;
118 }
119
120 @CanExecute
121 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
122 boolean canExecute = false;
123 menuItem.setVisible(canExecute);
124 canExecute = selection.getFirstElement() instanceof TaxonNode;
125 System.out.println(canExecute);
126 return canExecute;
127 }
128
129 @Override
130 public void onComplete() {
131 }
132
133 /**
134 * {@inheritDoc}
135 */
136 @Override
137 protected Object getTrigger() {
138 return this;
139 }
140 }