1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.view.descriptive.e4.handler;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Collection;
|
8
|
import java.util.HashSet;
|
9
|
import java.util.List;
|
10
|
import java.util.Set;
|
11
|
import java.util.UUID;
|
12
|
|
13
|
import javax.inject.Named;
|
14
|
|
15
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
16
|
import org.eclipse.e4.core.di.annotations.Execute;
|
17
|
import org.eclipse.e4.core.di.annotations.Optional;
|
18
|
import org.eclipse.e4.ui.di.UISynchronize;
|
19
|
import org.eclipse.e4.ui.model.application.MApplication;
|
20
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
21
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
22
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
|
23
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
24
|
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
25
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
26
|
import org.eclipse.jface.dialogs.MessageDialog;
|
27
|
import org.eclipse.jface.viewers.ISelection;
|
28
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
29
|
import org.eclipse.swt.widgets.Display;
|
30
|
import org.eclipse.swt.widgets.Shell;
|
31
|
|
32
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
33
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
37
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
38
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
39
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
40
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
|
41
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionToOtherTaxonOperation;
|
42
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
43
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
44
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
45
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
46
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
|
47
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
48
|
|
49
|
/**
|
50
|
*
|
51
|
* @author pplitzner
|
52
|
* @date 15.08.2017
|
53
|
*
|
54
|
*/
|
55
|
public class MoveDescriptionToOtherTaxonHandlerE4 implements IPostOperationEnabled {
|
56
|
|
57
|
private MoveDescriptionToOtherTaxonOperation operation;
|
58
|
|
59
|
private UUID newAcceptedTaxonNodeUuid;
|
60
|
private UUID oldAcceptedTaxonNodeUuid;
|
61
|
private UUID newAcceptedTaxonUuid;
|
62
|
private UUID oldAcceptedTaxonUuid;
|
63
|
private TaxonNameEditorE4 editor;
|
64
|
private MApplication application;
|
65
|
private EModelService modelService;
|
66
|
private EPartService partService;
|
67
|
|
68
|
@Execute
|
69
|
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
70
|
@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
|
71
|
@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MMenuItem menuItem, EPartService partService,
|
72
|
EModelService modelService, MApplication application, UISynchronize sync) {
|
73
|
|
74
|
this.modelService = modelService;
|
75
|
this.application = application;
|
76
|
this.partService = partService;
|
77
|
|
78
|
FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
|
79
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
|
80
|
if(e4WrappedPart instanceof TaxonNameEditorE4){
|
81
|
editor = (TaxonNameEditorE4) e4WrappedPart;
|
82
|
|
83
|
}
|
84
|
|
85
|
List<TaxonDescription> descriptions = new ArrayList<TaxonDescription>();
|
86
|
TaxonDescription description = null;
|
87
|
Object selectedElement = selection.getFirstElement();
|
88
|
if (selectedElement instanceof FeatureNodeContainer){
|
89
|
FeatureNodeContainer container = (FeatureNodeContainer)selectedElement;
|
90
|
description = (TaxonDescription) container.getDescription();
|
91
|
}else if (selectedElement instanceof TaxonDescription){
|
92
|
description = (TaxonDescription)selectedElement;
|
93
|
}
|
94
|
descriptions.add(description);
|
95
|
|
96
|
// Choose the target taxon
|
97
|
|
98
|
|
99
|
//get current taxon node
|
100
|
TaxonNode node = null;
|
101
|
Classification classification = null;
|
102
|
if(editor!=null){
|
103
|
node = editor.getEditorInput().getTaxonNode();
|
104
|
oldAcceptedTaxonUuid = editor.getTaxon().getUuid();
|
105
|
oldAcceptedTaxonNodeUuid = node.getUuid();
|
106
|
classification = node.getClassification();
|
107
|
}
|
108
|
|
109
|
Set<UUID> excludeTaxa = new HashSet<>();
|
110
|
excludeTaxa.add(oldAcceptedTaxonNodeUuid);
|
111
|
TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
|
112
|
// editor.getConversationHolder(),
|
113
|
Messages.MoveDescriptionToOtherTaxonHandler_CHOOSE_ACC_TAXON,
|
114
|
excludeTaxa,
|
115
|
node,
|
116
|
classification.getUuid());
|
117
|
|
118
|
if (newAcceptedTaxonNode == null) {
|
119
|
return ;
|
120
|
}
|
121
|
|
122
|
newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
|
123
|
newAcceptedTaxonUuid = newAcceptedTaxonNode.getTaxon().getUuid();
|
124
|
|
125
|
Collection<MPart> targetEditors = EditorUtil.checkForChanges(newAcceptedTaxonUuid, partService);
|
126
|
|
127
|
if (targetEditors != null || this.editor.isDirty()){
|
128
|
boolean proceed = MessageDialog.openQuestion(null,
|
129
|
Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE);
|
130
|
if( targetEditors.iterator().hasNext() ){
|
131
|
MPart part = targetEditors.iterator().next();
|
132
|
if (proceed) {
|
133
|
if (part != null){
|
134
|
if (part.getElementId().equals("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4")){
|
135
|
TaxonNameEditorE4 targetEditor = (TaxonNameEditorE4) WorkbenchUtility.getE4WrappedPart(part);
|
136
|
targetEditor.save(AbstractUtility.getMonitor());
|
137
|
}
|
138
|
if (editor.isDirty()){
|
139
|
editor.save(AbstractUtility.getMonitor());
|
140
|
}
|
141
|
} else {
|
142
|
return;
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
for(TaxonDescription taxonDescription : descriptions){
|
149
|
operation = new MoveDescriptionToOtherTaxonOperation(menuItem.getLocalizedLabel(),
|
150
|
editor.getUndoContext(),
|
151
|
taxonDescription,
|
152
|
newAcceptedTaxonNode,
|
153
|
this,
|
154
|
editor,
|
155
|
editor.getEditorInput());
|
156
|
AbstractUtility.executeOperation(operation, sync);
|
157
|
}
|
158
|
|
159
|
|
160
|
}
|
161
|
|
162
|
/** {@inheritDoc} */
|
163
|
@Override
|
164
|
public boolean postOperation(Object objectAffectedByOperation) {
|
165
|
Display.getDefault().asyncExec(new Runnable(){
|
166
|
|
167
|
@Override
|
168
|
public void run() {
|
169
|
//FIXME E4 migrate
|
170
|
// AbstractUtility.close(editor);
|
171
|
//
|
172
|
try {
|
173
|
// MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
|
174
|
// if(possibleOpenEditor != null){
|
175
|
// AbstractUtility.close(possibleOpenEditor);
|
176
|
// }
|
177
|
// EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, oldAcceptedTaxonNodeUuid);
|
178
|
// EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, newAcceptedTaxonNodeUuid);
|
179
|
EditorUtil.openTaxonNodeE4(newAcceptedTaxonNodeUuid, modelService, partService, application);
|
180
|
EditorUtil.openTaxonNodeE4(oldAcceptedTaxonNodeUuid, modelService, partService, application);
|
181
|
// } catch (PartInitException e) {
|
182
|
// MessagingUtils.error(this.getClass(), e);
|
183
|
// throw new RuntimeException(e);
|
184
|
} catch (Exception e) {
|
185
|
MessagingUtils.warningDialog(Messages.MoveDescriptionToOtherTaxonHandler_CREATE_FAILED, this, e.getMessage());
|
186
|
}
|
187
|
}
|
188
|
|
189
|
});
|
190
|
|
191
|
|
192
|
return true;
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
public boolean onComplete() {
|
197
|
return false;
|
198
|
}
|
199
|
|
200
|
@CanExecute
|
201
|
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
202
|
MHandledMenuItem menuItem){
|
203
|
boolean canExecute = false;
|
204
|
FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
|
205
|
|
206
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
|
207
|
if(e4WrappedPart instanceof TaxonNameEditorE4){
|
208
|
ISelection selection = factualDataPart.getViewer().getSelection();
|
209
|
if(selection instanceof IStructuredSelection){
|
210
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
211
|
canExecute = ((IStructuredSelection) selection).size()==1
|
212
|
&& firstElement instanceof DescriptionBase && !(firstElement instanceof TaxonNameDescription);
|
213
|
menuItem.setVisible(canExecute);
|
214
|
}
|
215
|
}
|
216
|
return canExecute;
|
217
|
}
|
218
|
}
|