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