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