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