d7463cc5a1f6950af9aa6c609ee4ddd05675f41c
[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)) || (oldTaxonSecUUIDs.size() == 1 && 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 = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, message, options);
153
154 // int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
155 // new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
156 if (result == 1){
157 //select new reference
158 Reference sec = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
159 newSecUuid = sec != null? sec.getUuid(): null;
160 }else if (result == 2){
161 //use parent sec
162 secHandling = SecReferenceHandlingEnum.UseNewParentSec;
163 newSecUuid = parentTaxonSecUuid;
164 }else if (result == 0){
165 //keep sec (also homotypic synonyms with different sec will keep the secundum)
166 secHandling = SecReferenceHandlingEnum.KeepOrWarn;
167 }else{
168 return null;
169 }
170 }
171
172
173
174 if(parentTaxonNode != null){
175 if(NavigationUtil.isDirty(parentTaxonNode, partService)){
176 MessageDialog.openWarning(shell,
177 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
178 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
179 return null;
180 }
181
182 return new MoveTaxonOperation(getTrigger(),
183 false,
184 oldTaxonNodeUUIDs,
185 parentTaxonNode.getUuid(),
186 moveToNewParent,
187 secHandling,
188 newSecUuid);
189 }
190
191 return null;
192 }
193
194 @CanExecute
195 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
196 boolean canExecute = false;
197 canExecute = selection.getFirstElement() instanceof TaxonNodeDto
198 && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
199 menuItem.setVisible(canExecute);
200 return canExecute;
201 }
202
203 @Override
204 public void onComplete() {
205 }
206
207 @Override
208 protected Object getTrigger() {
209 return this;
210 }
211 }