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.IDescriptionService;
|
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.store.CdmStore;
|
58
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
|
59
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
60
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
61
|
|
62
|
|
63
|
/**
|
64
|
*
|
65
|
* @author pplitzner
|
66
|
* @date 15.08.2017
|
67
|
*
|
68
|
*/
|
69
|
public class MoveDescriptionElementsSetNameInSourceHandlerE5 implements IPostOperationEnabled{
|
70
|
private UUID newAcceptedTaxonNodeUuid;
|
71
|
private UUID oldAcceptedTaxonNodeUuid;
|
72
|
private TaxonNameEditorE4 editor;
|
73
|
private MApplication application;
|
74
|
private EModelService modelService;
|
75
|
private EPartService partService;
|
76
|
|
77
|
@Execute
|
78
|
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
79
|
@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MMenuItem menuItem, EPartService partService,
|
80
|
EModelService modelService, MApplication application, UISynchronize sync) {
|
81
|
|
82
|
this.modelService = modelService;
|
83
|
this.application = application;
|
84
|
this.partService = partService;
|
85
|
|
86
|
FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
|
87
|
IStructuredSelection viewselection = (IStructuredSelection) factualDataPart.getViewer().getSelection();
|
88
|
|
89
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
|
90
|
if(e4WrappedPart instanceof TaxonNameEditorE4){
|
91
|
editor = (TaxonNameEditorE4) e4WrappedPart;
|
92
|
}
|
93
|
Taxon actualTaxon= null;
|
94
|
|
95
|
List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
|
96
|
DescriptionBase description = null;
|
97
|
for (Iterator iter = viewselection.iterator(); iter.hasNext();) {
|
98
|
Object object = iter.next();
|
99
|
if (object instanceof DescriptionElementBase) {
|
100
|
UUID uuid = ((DescriptionElementBase) object).getUuid();
|
101
|
description = ((DescriptionElementBase) object).getInDescription();
|
102
|
DescriptionElementBase element ;
|
103
|
if (((DescriptionElementBase) object).getId() > 0){
|
104
|
element = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null);
|
105
|
}else{
|
106
|
element = ((DescriptionElementBase) object);
|
107
|
}
|
108
|
|
109
|
elements.add(element);
|
110
|
} else if (object instanceof FeatureNodeContainer) {
|
111
|
for (DescriptionElementBase de : ((FeatureNodeContainer) object).getDescriptionElements()) {
|
112
|
DescriptionElementBase element ;
|
113
|
if (de.getId() > 0){
|
114
|
element = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null);
|
115
|
}else{
|
116
|
element = (de);
|
117
|
}
|
118
|
description = de.getInDescription();
|
119
|
elements.add(element);
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
if(elements.size() == 0 ){
|
125
|
return;
|
126
|
}
|
127
|
|
128
|
if (elements.get(0) != null){
|
129
|
description = elements.get(0).getInDescription();
|
130
|
}
|
131
|
Set<UUID> excludeTaxa = new HashSet<>();
|
132
|
if (description!=null && description.isInstanceOf(TaxonDescription.class)){
|
133
|
TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
|
134
|
actualTaxon = taxonDescription.getTaxon();
|
135
|
|
136
|
}
|
137
|
Classification classification = null;
|
138
|
TaxonNode actualNode = null;
|
139
|
if (actualTaxon != null){
|
140
|
if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
|
141
|
actualNode = actualTaxon.getTaxonNodes().iterator().next();
|
142
|
oldAcceptedTaxonNodeUuid = actualNode.getUuid();
|
143
|
excludeTaxa.add(oldAcceptedTaxonNodeUuid);
|
144
|
classification = actualNode.getClassification();
|
145
|
|
146
|
}
|
147
|
}
|
148
|
UUID classificationUuid = null;
|
149
|
if (classification != null){
|
150
|
classificationUuid = classification.getUuid();
|
151
|
}
|
152
|
TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
|
153
|
Messages.MoveDescriptionElementsHandler_CHOOSE_ACC_TAXON,
|
154
|
excludeTaxa,
|
155
|
null, classificationUuid
|
156
|
);
|
157
|
if (newAcceptedTaxonNode != null){
|
158
|
Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
|
159
|
|
160
|
if(targetTaxon == null){
|
161
|
// canceled
|
162
|
return;
|
163
|
}
|
164
|
newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
|
165
|
|
166
|
Collection<IE4SavablePart> targetEditors = EditorUtil.checkForTaxonChanges(targetTaxon.getUuid(), partService);
|
167
|
|
168
|
if (!targetEditors.isEmpty() || this.editor.isDirty()){
|
169
|
if(MessageDialog.openQuestion(null,Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE)){
|
170
|
this.editor.save(new NullProgressMonitor());
|
171
|
targetEditors.forEach(editor->editor.save(new NullProgressMonitor()));
|
172
|
return;
|
173
|
}
|
174
|
else{
|
175
|
return;
|
176
|
}
|
177
|
}
|
178
|
|
179
|
String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getTaxon());
|
180
|
|
181
|
AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(
|
182
|
menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(),
|
183
|
targetTaxon.getUuid(), moveMessage, elements, false, this, null, true);
|
184
|
editor.getEditorInput().addOperation(operation);
|
185
|
for (DescriptionElementBase element: elements)
|
186
|
{
|
187
|
element.getInDescription().removeElement(element);
|
188
|
}
|
189
|
editor.redraw();
|
190
|
editor.setDirty();
|
191
|
|
192
|
//AbstractUtility.executeOperation(operation, sync);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
/** {@inheritDoc} */
|
197
|
@Override
|
198
|
public boolean postOperation(Object objectAffectedByOperation) {
|
199
|
|
200
|
editor.getConversationHolder().bind();
|
201
|
editor.getConversationHolder().commit(true);
|
202
|
Display.getDefault().asyncExec(new Runnable(){
|
203
|
|
204
|
@Override
|
205
|
public void run() {
|
206
|
//AbstractUtility.close(editor.getMultiPageTaxonEditor());
|
207
|
|
208
|
//FIXME E4 migrate
|
209
|
// MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
|
210
|
// if(possibleOpenEditor != null){
|
211
|
// AbstractUtility.close(possibleOpenEditor);
|
212
|
// }
|
213
|
EditorUtil.openTaxonNodeE4(newAcceptedTaxonNodeUuid, modelService, partService, application);
|
214
|
EditorUtil.openTaxonNodeE4(oldAcceptedTaxonNodeUuid, modelService, partService, application);
|
215
|
}
|
216
|
|
217
|
});
|
218
|
|
219
|
|
220
|
return true;
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public boolean onComplete() {
|
225
|
return false;
|
226
|
}
|
227
|
|
228
|
@CanExecute
|
229
|
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
230
|
MHandledMenuItem menuItem){
|
231
|
boolean canExecute = false;
|
232
|
FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
|
233
|
|
234
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
|
235
|
if(e4WrappedPart instanceof TaxonNameEditorE4){
|
236
|
ISelection selection = factualDataPart.getViewer().getSelection();
|
237
|
if(selection instanceof IStructuredSelection){
|
238
|
boolean selectionProviding = factualDataPart.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4;
|
239
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
240
|
canExecute = (firstElement instanceof FeatureNodeContainer && !(((FeatureNodeContainer)firstElement).getContainerTree().getDescription() instanceof TaxonNameDescription))
|
241
|
|| (firstElement instanceof DescriptionElementBase && !(((DescriptionElementBase)firstElement).getInDescription() instanceof TaxonNameDescription)) ;
|
242
|
menuItem.setVisible(canExecute);
|
243
|
}
|
244
|
}
|
245
|
return canExecute;
|
246
|
}
|
247
|
|
248
|
|
249
|
}
|