Merge branch 'release/5.29.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / e4 / handler / MoveDescriptionElementsHandlerE4.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.editor.view.descriptive.e4.handler;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import javax.inject.Named;
21
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.e4.core.di.annotations.CanExecute;
24 import org.eclipse.e4.core.di.annotations.Execute;
25 import org.eclipse.e4.core.di.annotations.Optional;
26 import org.eclipse.e4.ui.di.UISynchronize;
27 import org.eclipse.e4.ui.model.application.MApplication;
28 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
29 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
30 import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
31 import org.eclipse.e4.ui.services.IServiceConstants;
32 import org.eclipse.e4.ui.workbench.modeling.EModelService;
33 import org.eclipse.e4.ui.workbench.modeling.EPartService;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Shell;
39
40 import eu.etaxonomy.cdm.api.service.IDescriptionElementService;
41 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
42 import eu.etaxonomy.cdm.model.description.DescriptionBase;
43 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
44 import eu.etaxonomy.cdm.model.description.TaxonDescription;
45 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
46 import eu.etaxonomy.cdm.model.taxon.Classification;
47 import eu.etaxonomy.cdm.model.taxon.Taxon;
48 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49 import eu.etaxonomy.taxeditor.editor.EditorUtil;
50 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
51 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
52 import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
53 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
54 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
55 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
56 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
57 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
58 import eu.etaxonomy.taxeditor.store.CdmStore;
59 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
60 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
61 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
62
63
64 /**
65 * @author pplitzner
66 * @date 15.08.2017
67 */
68 public class MoveDescriptionElementsHandlerE4 implements IPostOperationEnabled{
69 private UUID newAcceptedTaxonNodeUuid;
70 private UUID oldAcceptedTaxonNodeUuid;
71 private TaxonNameEditorE4 editor;
72 private MApplication application;
73 private EModelService modelService;
74 private EPartService partService;
75
76 @Execute
77 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
78 @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MMenuItem menuItem, EPartService partService,
79 EModelService modelService, MApplication application, UISynchronize sync) {
80
81 this.modelService = modelService;
82 this.application = application;
83 this.partService = partService;
84
85 FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
86 IStructuredSelection viewselection = (IStructuredSelection) factualDataPart.getViewer().getSelection();
87
88 Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
89 if(e4WrappedPart instanceof TaxonNameEditorE4){
90 editor = (TaxonNameEditorE4) e4WrappedPart;
91 }
92 Taxon actualTaxon= null;
93
94 List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
95 DescriptionBase<?> description = null;
96 for (Iterator<?> iter = viewselection.iterator(); iter.hasNext();) {
97 Object object = iter.next();
98 if (object instanceof DescriptionElementBase) {
99 UUID uuid = ((DescriptionElementBase) object).getUuid();
100 description = ((DescriptionElementBase) object).getInDescription();
101 DescriptionElementBase element ;
102 if (((DescriptionElementBase) object).getId() > 0){
103 element = CdmStore.getService(IDescriptionElementService.class).load(uuid, null);
104 }else{
105 element = ((DescriptionElementBase) object);
106 }
107
108 elements.add(element);
109 } else if (object instanceof FeatureNodeContainer) {
110 for (DescriptionElementBase de : ((FeatureNodeContainer) object).getDescriptionElements()) {
111 DescriptionElementBase element ;
112 if (de.getId() > 0){
113 element = CdmStore.getService(IDescriptionElementService.class).load(de.getUuid(), null);
114 }else{
115 element = (de);
116 }
117 description = de.getInDescription();
118 elements.add(element);
119 }
120 }
121 }
122
123 if(elements.size() == 0 ){
124 return;
125 }
126
127 if (elements.get(0) != null){
128 description = elements.get(0).getInDescription();
129 }
130 Set<UUID> excludeTaxa = new HashSet<>();
131 if (description!=null && description.isInstanceOf(TaxonDescription.class)){
132 TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
133 actualTaxon = taxonDescription.getTaxon();
134
135 }
136 Classification classification = null;
137 TaxonNode actualNode = null;
138 if (actualTaxon != null){
139 if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
140 actualNode = actualTaxon.getTaxonNodes().iterator().next();
141 oldAcceptedTaxonNodeUuid = actualNode.getUuid();
142 excludeTaxa.add(oldAcceptedTaxonNodeUuid);
143 classification = actualNode.getClassification();
144
145 }
146 }
147 UUID classificationUuid = null;
148 if (classification != null){
149 classificationUuid = classification.getUuid();
150 }
151 TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
152 Messages.MoveDescriptionElementsHandler_CHOOSE_ACC_TAXON,
153 excludeTaxa,
154 null, classificationUuid
155 );
156 if (newAcceptedTaxonNode != null){
157 Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
158
159 if(targetTaxon == null){
160 // canceled
161 return;
162 }
163 newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
164
165 Collection<IE4SavablePart> targetEditors = EditorUtil.checkForTaxonChanges(targetTaxon.getUuid(), partService);
166
167 if (!targetEditors.isEmpty() || this.editor.isDirty()){
168 if(MessageDialog.openQuestion(null,Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE)){
169 this.editor.save(new NullProgressMonitor());
170 targetEditors.forEach(editor->editor.save(new NullProgressMonitor()));
171 return;
172 }
173 else{
174 return;
175 }
176 }
177
178 String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getTaxon());
179
180 AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(
181 menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(),
182 targetTaxon.getUuid(), moveMessage, elements, false, this, null, false);
183 editor.getEditorInput().addOperation(operation);
184 for (DescriptionElementBase element: elements)
185 {
186 element.getInDescription().removeElement(element);
187 }
188 editor.redraw();
189 editor.setDirty();
190
191 //AbstractUtility.executeOperation(operation, sync);
192 }
193 }
194
195 @Override
196 public boolean postOperation(Object objectAffectedByOperation) {
197
198 editor.getConversationHolder().bind();
199 editor.getConversationHolder().commit(true);
200 Display.getDefault().asyncExec(new Runnable(){
201
202 @Override
203 public void run() {
204 //AbstractUtility.close(editor.getMultiPageTaxonEditor());
205
206 //FIXME E4 migrate
207 // MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
208 // if(possibleOpenEditor != null){
209 // AbstractUtility.close(possibleOpenEditor);
210 // }
211 EditorUtil.openTaxonNodeE4(newAcceptedTaxonNodeUuid, modelService, partService, application);
212 EditorUtil.openTaxonNodeE4(oldAcceptedTaxonNodeUuid, modelService, partService, application);
213 }
214
215 });
216
217
218 return true;
219 }
220
221 @Override
222 public boolean onComplete() {
223 return false;
224 }
225
226 @CanExecute
227 public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
228 MHandledMenuItem menuItem){
229 boolean canExecute = false;
230 FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
231 boolean isComputedDisabled = PreferencesUtil.isComputedDesciptionHandlingDisabled();
232 Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
233 if(e4WrappedPart instanceof TaxonNameEditorE4){
234 ISelection selection = factualDataPart.getViewer().getSelection();
235 if(selection instanceof IStructuredSelection){
236 boolean selectionProviding = factualDataPart.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4;
237 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
238
239 canExecute = ((firstElement instanceof FeatureNodeContainer && !(((FeatureNodeContainer)firstElement).getContainerTree().getDescription() instanceof TaxonNameDescription))
240 || (firstElement instanceof DescriptionElementBase && !(((DescriptionElementBase)firstElement).getInDescription() instanceof TaxonNameDescription)))
241 && ((firstElement instanceof FeatureNodeContainer && !((((FeatureNodeContainer)firstElement).getDescription().isComputed() || ((FeatureNodeContainer)firstElement).getDescription().isCloneForSource())&& isComputedDisabled))
242 || (firstElement instanceof DescriptionElementBase && !((((DescriptionElementBase)firstElement).getInDescription().isComputed() || ((DescriptionElementBase)firstElement).getInDescription().isCloneForSource())&& isComputedDisabled))) ;
243 menuItem.setVisible(canExecute);
244 }
245 }
246 return canExecute;
247 }
248
249
250 }