all polytomouskey related handler are now extended from RemotingCdmHandler
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / InsertNewNodeHandler.java
1 /**
2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.editor.key.polytomous.handler;
10
11 import org.eclipse.core.commands.ExecutionEvent;
12 import org.eclipse.core.commands.operations.AbstractOperation;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.handlers.HandlerUtil;
18
19 import eu.etaxonomy.cdm.model.description.PolytomousKey;
20 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
21 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
22 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
23 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
24 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
25 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.InsertPolytomousKeyNodeOperation;
26
27 /**
28 * @author k.luther
29 * @date 17.11.2016
30 *
31 */
32 public class InsertNewNodeHandler extends AbstractPolytomousKeyNodeHandler{
33
34 private PolytomousKeyNode parentNode ;
35
36
37 public InsertNewNodeHandler() {
38 super(PolytomousKeyEditorLabels.INSERT_NODE_POLYTOMOUS_KEY_NODE_LABEL);
39 }
40
41
42 /**
43 * {@inheritDoc}
44 */
45 @Override
46 public IStatus allowOperations(ExecutionEvent event) {
47 IEditorPart editor = HandlerUtil.getActiveEditor(event);
48
49 if (editor instanceof KeyEditor) {
50 editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
51 .getActiveEditor();
52
53 if (editorPage instanceof PolytomousKeyListEditor) {
54 PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
55 if(klEditor.getTableItemCount() == 0) {
56 PolytomousKey pk = klEditor.getViewerInputKey();
57 parentNode = pk.getRoot();
58
59 } else {
60 IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
61 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
62 parentNode = ((PolytomousKeyNode) selection.getFirstElement()).getParent();
63 } else {
64 return new Status(IStatus.ERROR,
65 "unknown",
66 PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_CHILD_SELECTED_MESSAGE);
67 }
68 }
69 }
70 }
71 return Status.OK_STATUS;
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public AbstractOperation prepareOperation(ExecutionEvent event) {
79 return new InsertPolytomousKeyNodeOperation(event.getTrigger(), false, parentNode, editorPage);
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 @Override
86 public void onComplete() {
87 // TODO Auto-generated method stub
88
89 }
90
91 }