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