cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / MoveTaxonNodeHandlerE4.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.HashSet;
12 import java.util.Iterator;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import javax.inject.Named;
17
18 import org.eclipse.core.commands.operations.AbstractOperation;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.e4.core.di.annotations.CanExecute;
22 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
24 import org.eclipse.e4.ui.services.IServiceConstants;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.TreeSelection;
28 import org.eclipse.swt.widgets.Shell;
29
30 import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
31 import eu.etaxonomy.cdm.model.reference.Reference;
32 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
34 import eu.etaxonomy.taxeditor.model.AbstractUtility;
35 import eu.etaxonomy.taxeditor.model.MessagingUtils;
36 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
37 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
38 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
39 import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
40 import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
41 import eu.etaxonomy.taxeditor.operation.e4.CdmHandlerE4;
42 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
43 import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
44 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
45
46 /**
47 * @author cmathew
48 * @date 19 Jun 2015
49 */
50 public class MoveTaxonNodeHandlerE4 extends CdmHandlerE4 {
51
52 private Set<UUID> oldTaxonNodeUUIDs = new HashSet<>();
53 private UUID classificationUuid = null;
54 boolean isPublish = true;
55 SecReferenceHandlingEnum secHandling;
56 UUID newSecUuid;
57 private Set<UUID> oldTaxonSecUUIDs = new HashSet<>();
58
59 public MoveTaxonNodeHandlerE4() {
60 super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
61 }
62
63 @Override
64 public IStatus allowOperations(IStructuredSelection selection,
65 Shell shell,
66 MPart activePart,
67 MHandledMenuItem menuItem) {
68 // check for no taxon tree node selected
69 if(selection.size() == 0) {
70 return new Status(IStatus.ERROR,
71 "unknown", //$NON-NLS-1$
72 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
73 }
74 // check that selected object is a taxon node
75
76 Object obj ;
77 Iterator<?> iter = selection.iterator();
78 oldTaxonNodeUUIDs = new HashSet<>();
79 isPublish = true;
80 while (iter.hasNext()){
81 obj = iter.next();
82 if(obj instanceof TaxonNodeDto) {
83 oldTaxonNodeUUIDs.add(((TaxonNodeDto)obj).getUuid());
84 oldTaxonSecUUIDs.add(((TaxonNodeDto)obj).getSecUuid());
85 isPublish &= ((TaxonNodeDto)obj).isPublish();
86 if (classificationUuid == null){
87 classificationUuid = ((TaxonNodeDto)obj).getClassificationUUID();
88 }
89 } else {
90 return new Status(IStatus.ERROR,
91 "unknown", //$NON-NLS-1$
92 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
93 }
94 }
95 return Status.OK_STATUS;
96 }
97
98 @Override
99 public AbstractOperation prepareOperation(IStructuredSelection selection,
100 Shell shell,
101 MPart activePart,
102 MHandledMenuItem menuItem) {
103 TaxonNode parentTaxonNode;
104 MovingType moveToNewParent = MovingType.CHILD;
105
106 if (PreferencesUtil.isNodesSortedNaturally()){
107 parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
108 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON,
109 oldTaxonNodeUUIDs,
110 null,
111 classificationUuid, true);
112 String[] buttonLables = {Messages.RemotingMoveTaxonNodeHandler_CHILD, Messages.RemotingMoveTaxonNodeHandler_BEHIND,Messages.RemotingMoveTaxonNodeHandler_CANCEL};
113 MessageDialog dialog = new MessageDialog(null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE, null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
114 dialog.open();
115 int returnCode = dialog.getReturnCode();
116 if (returnCode == 0){
117 moveToNewParent = MovingType.CHILD;
118 } else if (returnCode == 1){
119 moveToNewParent = MovingType.BEHIND;
120 }
121 } else {
122 parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
123 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT,
124 oldTaxonNodeUUIDs,
125 null,
126 classificationUuid, true);
127 }
128 if (parentTaxonNode == null){
129 return null;
130 }
131
132 if (parentTaxonNode.getTaxon() != null && parentTaxonNode.getTaxon().isPublish() != isPublish){
133 MessageDialog.openWarning(shell,
134 Messages.RemotingMoveTaxonNodeHandler_DIFFERENT_PUBLISH_TITLE,
135 Messages.RemotingMoveTaxonNodeHandler_DIFFERENT_PUBLISH_MESSAGE);
136 }
137
138 secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
139 newSecUuid = null;
140 UUID parentTaxonSecUuid= parentTaxonNode.getTaxon() != null && parentTaxonNode.getTaxon().getSec() != null ? parentTaxonNode.getTaxon().getSec().getUuid(): null;
141 if ( secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect) || (oldTaxonSecUUIDs.size() > 1 && secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect) || secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) ) || (oldTaxonSecUUIDs.size() == 1 && (secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) ||secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect)) && !oldTaxonSecUUIDs.contains(parentTaxonSecUuid))){
142 //The moved nodes have different secundum references
143 String message = null;
144 String[] options = null;
145 if (secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect) && (parentTaxonSecUuid != null && oldTaxonSecUUIDs.contains(parentTaxonSecUuid))){
146 message = Messages.TreeNodeDropAdapter_Select_Sec_Reference_Handling_message_always_select;
147 options = new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select};
148 }else {
149 message = Messages.TreeNodeDropAdapter_Select_Sec_Reference_Handling_message;
150 options = new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select, Messages.TreeNodeDropAdapter_Select_Sec_Reference_Parent, };
151 }
152 int result = 0;
153 if (secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) ){
154 MessageDialog.openWarning(null, "Secundum references differ", Messages.MoveTaxon_Different_Secundum_References);
155 }else{
156 result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, message, options);
157 }
158
159
160 // int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
161 // new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
162 if (result == 1){
163 //select new reference
164 Reference sec = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
165 newSecUuid = sec != null? sec.getUuid(): null;
166 }else if (result == 2){
167 //use parent sec
168 secHandling = SecReferenceHandlingEnum.UseNewParentSec;
169 newSecUuid = parentTaxonSecUuid;
170 }else if (result == 0){
171 //keep sec (also homotypic synonyms with different sec will keep the secundum)
172 secHandling = SecReferenceHandlingEnum.KeepOrWarn;
173 }else{
174 return null;
175 }
176 }
177
178 if(parentTaxonNode != null){
179 if(NavigationUtil.isDirty(parentTaxonNode, partService)){
180 MessageDialog.openWarning(shell,
181 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
182 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
183 return null;
184 }
185
186 return new MoveTaxonOperation(getTrigger(),
187 false,
188 oldTaxonNodeUUIDs,
189 parentTaxonNode.getUuid(),
190 moveToNewParent,
191 secHandling,
192 newSecUuid);
193 }
194
195 return null;
196 }
197
198 @CanExecute
199 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
200 boolean canExecute = false;
201 canExecute = selection.getFirstElement() instanceof TaxonNodeDto
202 && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
203 menuItem.setVisible(canExecute);
204 return canExecute;
205 }
206
207 @Override
208 public void onComplete() {
209 }
210
211 @Override
212 protected Object getTrigger() {
213 return this;
214 }
215 }