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