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
|
|
92
|
|
93
|
}
|
94
|
Taxon actualTaxon= null;
|
95
|
|
96
|
List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
|
97
|
|
98
|
for (Iterator iter = viewselection.iterator(); iter.hasNext();) {
|
99
|
Object object = iter.next();
|
100
|
if (object instanceof DescriptionElementBase) {
|
101
|
UUID uuid = ((DescriptionElementBase) object).getUuid();
|
102
|
|
103
|
elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
|
104
|
} else if (object instanceof FeatureNodeContainer) {
|
105
|
for (DescriptionElementBase de : ((FeatureNodeContainer) object).getDescriptionElements()) {
|
106
|
elements.add(
|
107
|
CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null));
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
if(elements.size() == 0){
|
113
|
return;
|
114
|
}
|
115
|
|
116
|
DescriptionBase description = elements.get(0).getInDescription();
|
117
|
Set<UUID> excludeTaxa = new HashSet<>();
|
118
|
if (description!=null && description.isInstanceOf(TaxonDescription.class)){
|
119
|
TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
|
120
|
actualTaxon = taxonDescription.getTaxon();
|
121
|
|
122
|
}
|
123
|
Classification classification = null;
|
124
|
TaxonNode actualNode = null;
|
125
|
if (actualTaxon != null){
|
126
|
if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
|
127
|
actualNode = actualTaxon.getTaxonNodes().iterator().next();
|
128
|
oldAcceptedTaxonNodeUuid = actualNode.getUuid();
|
129
|
excludeTaxa.add(oldAcceptedTaxonNodeUuid);
|
130
|
classification = actualNode.getClassification();
|
131
|
|
132
|
}
|
133
|
}
|
134
|
TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
|
135
|
// editor.getConversationHolder(),
|
136
|
Messages.MoveDescriptionElementsHandler_CHOOSE_ACC_TAXON,
|
137
|
excludeTaxa,
|
138
|
null, classification.getUuid()
|
139
|
);
|
140
|
if (newAcceptedTaxonNode != null){
|
141
|
Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
|
142
|
|
143
|
if(targetTaxon == null){
|
144
|
// canceled
|
145
|
return;
|
146
|
}
|
147
|
newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
|
148
|
|
149
|
MPart targetEditor = EditorUtil.checkForChanges(targetTaxon.getUuid(), partService);
|
150
|
|
151
|
if ((targetEditor != null && targetEditor.getElementId().equals("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4")) || this.editor.isDirty()){
|
152
|
boolean proceed = MessageDialog.openQuestion(null,
|
153
|
Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE);
|
154
|
if (proceed) {
|
155
|
if (targetEditor != null){
|
156
|
e4WrappedPart = WorkbenchUtility.getE4WrappedPart(targetEditor);
|
157
|
((TaxonNameEditorE4)e4WrappedPart).save(AbstractUtility.getMonitor());
|
158
|
}
|
159
|
if (editor.isDirty()){
|
160
|
editor.save(AbstractUtility.getMonitor());
|
161
|
}
|
162
|
} else {
|
163
|
return;
|
164
|
}
|
165
|
}
|
166
|
|
167
|
String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getTaxon());
|
168
|
|
169
|
AbstractPostOperation operation = new MoveDescriptionElementsOperation(
|
170
|
menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(),
|
171
|
targetTaxon.getUuid(), moveMessage, elements, false, this, null);
|
172
|
|
173
|
AbstractUtility.executeOperation(operation, sync);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/** {@inheritDoc} */
|
178
|
@Override
|
179
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
180
|
|
181
|
editor.getConversationHolder().bind();
|
182
|
editor.getConversationHolder().commit(true);
|
183
|
Display.getDefault().asyncExec(new Runnable(){
|
184
|
|
185
|
@Override
|
186
|
public void run() {
|
187
|
//AbstractUtility.close(editor.getMultiPageTaxonEditor());
|
188
|
|
189
|
//FIXME E4 migrate
|
190
|
// MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
|
191
|
// if(possibleOpenEditor != null){
|
192
|
// AbstractUtility.close(possibleOpenEditor);
|
193
|
// }
|
194
|
EditorUtil.openTaxonNodeE4(newAcceptedTaxonNodeUuid, modelService, partService, application);
|
195
|
EditorUtil.openTaxonNodeE4(oldAcceptedTaxonNodeUuid, modelService, partService, application);
|
196
|
}
|
197
|
|
198
|
});
|
199
|
|
200
|
|
201
|
return true;
|
202
|
}
|
203
|
|
204
|
@Override
|
205
|
public boolean onComplete() {
|
206
|
return false;
|
207
|
}
|
208
|
|
209
|
@CanExecute
|
210
|
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
211
|
MHandledMenuItem menuItem){
|
212
|
boolean canExecute = false;
|
213
|
FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
|
214
|
|
215
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
|
216
|
if(e4WrappedPart instanceof TaxonNameEditorE4){
|
217
|
ISelection selection = factualDataPart.getViewer().getSelection();
|
218
|
if(selection instanceof IStructuredSelection){
|
219
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
220
|
canExecute = firstElement instanceof FeatureNodeContainer
|
221
|
|| firstElement instanceof DescriptionElementBase;
|
222
|
menuItem.setVisible(canExecute);
|
223
|
}
|
224
|
}
|
225
|
return canExecute;
|
226
|
}
|
227
|
|
228
|
|
229
|
}
|