1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.key.polytomous.handler;
|
5
|
|
6
|
import org.eclipse.core.commands.AbstractHandler;
|
7
|
import org.eclipse.core.commands.ExecutionEvent;
|
8
|
import org.eclipse.core.commands.ExecutionException;
|
9
|
import org.eclipse.core.commands.common.NotDefinedException;
|
10
|
import org.eclipse.core.commands.operations.IUndoContext;
|
11
|
import org.eclipse.jface.dialogs.MessageDialog;
|
12
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
13
|
import org.eclipse.ui.IEditorPart;
|
14
|
import org.eclipse.ui.handlers.HandlerUtil;
|
15
|
|
16
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
17
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
18
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
19
|
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
|
20
|
import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
|
21
|
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
|
22
|
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
|
23
|
import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.CreateNodeOperation;
|
24
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
25
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
26
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
27
|
|
28
|
/**
|
29
|
* @author n.hoffmann
|
30
|
*
|
31
|
*/
|
32
|
public class CreateChildNodeHandler extends AbstractHandler {
|
33
|
|
34
|
/*
|
35
|
* (non-Javadoc)
|
36
|
*
|
37
|
* @see
|
38
|
* org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
|
39
|
* ExecutionEvent)
|
40
|
*/
|
41
|
@Override
|
42
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
43
|
|
44
|
IEditorPart editor = HandlerUtil.getActiveEditor(event);
|
45
|
|
46
|
if (editor instanceof KeyEditor) {
|
47
|
IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
|
48
|
.getActiveEditor();
|
49
|
|
50
|
if (editorPage instanceof PolytomousKeyListEditor) {
|
51
|
PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
|
52
|
if(klEditor.getTableItemCount() == 0) {
|
53
|
PolytomousKey pk = klEditor.getViewerInputKey();
|
54
|
try {
|
55
|
String label = event.getCommand().getName();
|
56
|
IUndoContext undoContext = EditorUtil.getUndoContext();
|
57
|
|
58
|
|
59
|
AbstractPostOperation operation = new CreateNodeOperation(
|
60
|
label, undoContext, pk.getRoot(), editorPage);
|
61
|
AbstractUtility.executeOperation(operation);
|
62
|
} catch (NotDefinedException e) {
|
63
|
MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
|
64
|
}
|
65
|
} else {
|
66
|
|
67
|
IStructuredSelection selection = (IStructuredSelection) HandlerUtil
|
68
|
.getCurrentSelection(event);
|
69
|
|
70
|
if (selection.getFirstElement() instanceof PolytomousKeyNode) {
|
71
|
try {
|
72
|
String label = event.getCommand().getName();
|
73
|
IUndoContext undoContext = EditorUtil.getUndoContext();
|
74
|
|
75
|
PolytomousKeyNode keyNode = (PolytomousKeyNode) selection
|
76
|
.getFirstElement();
|
77
|
|
78
|
AbstractPostOperation operation = new CreateNodeOperation(
|
79
|
label, undoContext, keyNode, editorPage);
|
80
|
AbstractUtility.executeOperation(operation);
|
81
|
} catch (NotDefinedException e) {
|
82
|
MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
|
83
|
}
|
84
|
} else {
|
85
|
MessageDialog.openInformation(
|
86
|
AbstractUtility.getShell(),
|
87
|
PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
|
88
|
PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
|
89
|
);
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
return null;
|
96
|
}
|
97
|
|
98
|
}
|