1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
import java.util.UUID;
|
9
|
|
10
|
import javax.lang.model.element.Element;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.eclipse.core.commands.AbstractHandler;
|
14
|
import org.eclipse.core.commands.ExecutionEvent;
|
15
|
import org.eclipse.core.commands.ExecutionException;
|
16
|
import org.eclipse.core.commands.IHandler;
|
17
|
import org.eclipse.core.commands.common.NotDefinedException;
|
18
|
import org.eclipse.jface.viewers.ISelection;
|
19
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
20
|
import org.eclipse.swt.widgets.Display;
|
21
|
import org.eclipse.ui.PartInitException;
|
22
|
import org.eclipse.ui.handlers.HandlerUtil;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
27
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
28
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
29
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
30
|
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
|
31
|
import eu.etaxonomy.taxeditor.editor.Page;
|
32
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
33
|
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
|
34
|
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeAcceptedTaxonToSynonymOperation;
|
35
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionToOtherTaxonOperation;
|
36
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
37
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
38
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
|
39
|
|
40
|
/**
|
41
|
* <p>ChangeAcceptedTaxonToSynonymHandler class.</p>
|
42
|
*
|
43
|
* @author a.kohlbecker
|
44
|
* @created Okt. 11, 2013
|
45
|
* @version 1.0
|
46
|
*
|
47
|
*/
|
48
|
public class MoveDescriptionToOtherTaxonHandler extends AbstractHandler
|
49
|
implements IHandler, IPostOperationEnabled {
|
50
|
private static final Logger logger = Logger
|
51
|
.getLogger(MoveDescriptionToOtherTaxonHandler.class);
|
52
|
private MoveDescriptionToOtherTaxonOperation operation;
|
53
|
|
54
|
private UUID newAcceptedTaxonNodeUuid;
|
55
|
private TaxonNameEditor editor;
|
56
|
|
57
|
/* (non-Javadoc)
|
58
|
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
59
|
*/
|
60
|
/** {@inheritDoc} */
|
61
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
62
|
|
63
|
ISelection selection = HandlerUtil.getCurrentSelection(event);
|
64
|
|
65
|
if(selection instanceof IStructuredSelection){
|
66
|
|
67
|
List<TaxonDescription> descriptions = new ArrayList<TaxonDescription>();
|
68
|
|
69
|
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
|
70
|
|
71
|
for(Object element : structuredSelection.toArray()){
|
72
|
if (element instanceof TaxonDescription){
|
73
|
UUID uuid = ((TaxonDescription)element).getUuid();
|
74
|
descriptions.add((TaxonDescription) CdmStore.getService(IDescriptionService.class).load(uuid, null));
|
75
|
}
|
76
|
}
|
77
|
if(descriptions.size() == 0){
|
78
|
return null;
|
79
|
}
|
80
|
|
81
|
// Choose the target taxon
|
82
|
List<UUID> excludeTaxa = new ArrayList<UUID>();
|
83
|
editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
|
84
|
excludeTaxa.add(descriptions.get(0).getTaxon().getUuid());
|
85
|
TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
|
86
|
editor.getConversationHolder(),
|
87
|
"Choose the accepted taxon",
|
88
|
excludeTaxa,
|
89
|
null,
|
90
|
null);
|
91
|
|
92
|
if (newAcceptedTaxonNode == null) {
|
93
|
return null;
|
94
|
}
|
95
|
|
96
|
newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
|
97
|
|
98
|
try {
|
99
|
for(TaxonDescription description : descriptions){
|
100
|
operation = new MoveDescriptionToOtherTaxonOperation(event.getCommand().getName(),
|
101
|
editor.getUndoContext(), description, newAcceptedTaxonNode, this, editor);
|
102
|
EditorUtil.executeOperation(operation);
|
103
|
}
|
104
|
|
105
|
} catch (NotDefinedException e) {
|
106
|
logger.warn("Command name not set");
|
107
|
}
|
108
|
}
|
109
|
|
110
|
return null;
|
111
|
|
112
|
}
|
113
|
|
114
|
/* (non-Javadoc)
|
115
|
* @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
|
116
|
*/
|
117
|
/** {@inheritDoc} */
|
118
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
119
|
Display.getDefault().asyncExec(new Runnable(){
|
120
|
|
121
|
public void run() {
|
122
|
EditorUtil.close(editor.getMultiPageTaxonEditor());
|
123
|
|
124
|
try {
|
125
|
MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
|
126
|
if(possibleOpenEditor != null){
|
127
|
EditorUtil.close(possibleOpenEditor);
|
128
|
}
|
129
|
EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
|
130
|
} catch (PartInitException e) {
|
131
|
EditorUtil.error(this.getClass(), e);
|
132
|
throw new RuntimeException(e);
|
133
|
} catch (Exception e) {
|
134
|
EditorUtil.warningDialog("Could not create Taxon", this, e.getMessage());
|
135
|
}
|
136
|
}
|
137
|
|
138
|
});
|
139
|
|
140
|
|
141
|
return true;
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* <p>onComplete</p>
|
146
|
*
|
147
|
* @return a boolean.
|
148
|
*/
|
149
|
public boolean onComplete() {
|
150
|
// TODO Auto-generated method stub
|
151
|
return false;
|
152
|
}
|
153
|
}
|