ref #7793: adapt taxonnode wizard and add menu item to set unplaced in navigator
[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.HashSet;
7 import java.util.Iterator;
8 import java.util.Set;
9 import java.util.UUID;
10
11 import javax.inject.Named;
12
13 import org.eclipse.core.commands.operations.AbstractOperation;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.e4.core.di.annotations.CanExecute;
17 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19 import org.eclipse.e4.ui.services.IServiceConstants;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.TreeSelection;
22 import org.eclipse.swt.widgets.Shell;
23
24 import eu.etaxonomy.cdm.api.service.IClassificationService;
25 import eu.etaxonomy.cdm.model.taxon.Classification;
26 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
30 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
31 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingChangeAcceptedTaxonToSynonymOperation;
32 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
35
36 /**
37 *
38 * @author pplitzner
39 * @since Sep 6, 2017
40 *
41 */
42 public class RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHandlerE4 {
43
44
45 private Set<TaxonNodeDto> oldTaxonNodes = new HashSet();
46 private Classification classification;
47
48 public RemotingChangeAcceptedTaxonToSynonymHandlerE4() {
49 super(TaxonNavigatorLabels.CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL);
50 }
51
52 @Override
53 public IStatus allowOperations(IStructuredSelection 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
71 // check that selected object is a taxon node
72 Iterator it = selection.iterator();
73 Classification nextClassification;
74 oldTaxonNodes = new HashSet<>();
75 while(it.hasNext()){
76 Object obj = it.next();
77 if(obj instanceof TaxonNodeDto) {
78 oldTaxonNodes.add((TaxonNodeDto)obj);
79 nextClassification = CdmStore.getService(IClassificationService.class).find(((TaxonNodeDto)obj).getClassificationUUID());
80 if (classification == null){
81 classification = nextClassification;
82 }else if (!classification.equals(nextClassification)){
83 return new Status(IStatus.ERROR,
84 "unknown", //$NON-NLS-1$
85 TaxonNavigatorLabels.ACCEPTED_TAXA_NEED_TO_BE_FROM_SAME_CLASSIFICATION);
86 }
87 } else {
88 if (obj instanceof TaxonNode && !((TaxonNode)obj).hasTaxon()){
89 return new Status(IStatus.ERROR,
90 "Operation not available for Classifications", //$NON-NLS-1$
91 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
92 }
93 return new Status(IStatus.ERROR,
94 "unknown", //$NON-NLS-1$
95 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
96 }
97 // check that the source taxon node does not have children
98 if(((TaxonNodeDto)obj).getTaxonomicChildrenCount() > 0) {
99 return new Status(IStatus.ERROR,
100 "unknown", //$NON-NLS-1$
101 TaxonNavigatorLabels.SOURCE_TAXON_HAS_CHILDREN_MESSAGE);
102
103 }
104
105 // check if corresponding name editor is closed
106 EditorUtil.closeObsoleteEditor((TaxonNodeDto)obj, partService);
107 }
108 return Status.OK_STATUS;
109 }
110
111 @Override
112 public AbstractOperation prepareOperation(IStructuredSelection selection,
113 Shell shell,
114 MPart activePart,
115 MHandledMenuItem menuItem) {
116 Set<UUID> excludeTaxa = new HashSet<>();
117 for (TaxonNodeDto oldNode:oldTaxonNodes){
118 excludeTaxa.add(oldNode.getTaxonUuid());
119 }
120 TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
121 // new ConversationHolderMock(),
122 Messages.RemotingChangeAcceptedTaxonToSynonymHandler_CHOOSE_TAXON,
123 excludeTaxa,
124 null,
125 classification.getUuid());
126
127 if (newAcceptedTaxonNode == null) {
128 return null;
129 }
130 Set<UUID> nodeUuids = new HashSet();
131 for (TaxonNodeDto node: oldTaxonNodes){
132 nodeUuids.add(node.getUuid());
133 }
134 RemotingChangeAcceptedTaxonToSynonymOperation rcattso =
135 new RemotingChangeAcceptedTaxonToSynonymOperation(getTrigger(),
136 false,
137 nodeUuids,
138 newAcceptedTaxonNode.getUuid(), partService, activePart, application);
139
140 return rcattso;
141 }
142
143 @CanExecute
144 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
145 boolean canExecute = !selection.isEmpty();
146 Object[] array = selection.toArray();
147 for (Object object : array) {
148 canExecute &= (object instanceof TaxonNodeDto) && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
149 }
150 menuItem.setVisible(canExecute);
151 return canExecute;
152 }
153
154 @Override
155 public void onComplete() {
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 protected Object getTrigger() {
163 return this;
164 }
165 }