Project

General

Profile

Download (3.69 KB) Statistics
| Branch: | Tag: | Revision:
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.e4.handler;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.core.commands.ExecutionEvent;
14
import org.eclipse.core.commands.operations.AbstractOperation;
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.core.runtime.Status;
17
import org.eclipse.e4.core.di.annotations.CanExecute;
18
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19
import org.eclipse.e4.ui.services.IServiceConstants;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.ui.IEditorPart;
22
import org.eclipse.ui.handlers.HandlerUtil;
23

    
24
import eu.etaxonomy.cdm.model.description.PolytomousKey;
25
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
26
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
27
import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
28
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
29
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
30
import eu.etaxonomy.taxeditor.editor.key.polytomous.handler.AbstractPolytomousKeyNodeHandler;
31
import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.InsertPolytomousKeyNodeOperation;
32

    
33
/**
34
 * @author k.luther
35
 * @date 17.11.2016
36
 *
37
 */
38
public class InsertNewNodeHandlerE4 extends AbstractPolytomousKeyNodeHandler{
39

    
40
    private PolytomousKeyNode parentNode ;
41

    
42

    
43
    public InsertNewNodeHandlerE4() {
44
        super(PolytomousKeyEditorLabels.INSERT_NODE_POLYTOMOUS_KEY_NODE_LABEL);
45
    }
46

    
47

    
48
    /**
49
     * {@inheritDoc}
50
     */
51
    @Override
52
    public IStatus allowOperations(ExecutionEvent event) {
53
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
54

    
55
        if (editor instanceof KeyEditor) {
56
            editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
57
                    .getActiveEditor();
58

    
59
            if (editorPage instanceof PolytomousKeyListEditor) {
60
                PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
61
                if(klEditor.getTableItemCount() == 0) {
62
                    PolytomousKey pk = klEditor.getViewerInputKey();
63
                    parentNode = pk.getRoot();
64

    
65
                } else {
66
                    IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
67
                    if (selection.getFirstElement() instanceof PolytomousKeyNode) {
68
                        parentNode = ((PolytomousKeyNode) selection.getFirstElement()).getParent();
69
                    } else {
70
                        return new Status(IStatus.ERROR,
71
                                "unknown", //$NON-NLS-1$
72
                                PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE);
73
                    }
74
                }
75
            }
76
        }
77
        return Status.OK_STATUS;
78
    }
79

    
80
    /**
81
     * {@inheritDoc}
82
     */
83
    @Override
84
    public AbstractOperation prepareOperation(ExecutionEvent event) {
85
        return new InsertPolytomousKeyNodeOperation(event.getTrigger(), false, parentNode, editorPage);
86
    }
87

    
88
    /**
89
     * {@inheritDoc}
90
     */
91
    @Override
92
    public void onComplete() {
93
        // TODO Auto-generated method stub
94

    
95
    }
96

    
97
    @CanExecute
98
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
99
            MHandledMenuItem menuItem){
100
        boolean canExecute = false;
101
        canExecute = selection instanceof PolytomousKeyNode;
102
        menuItem.setVisible(canExecute);
103
        return canExecute;
104
    }
105

    
106
}
(2-2/2)