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