Project

General

Profile

Download (2.37 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.featuretree.e4.handler;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.e4.core.di.annotations.CanExecute;
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.core.di.annotations.Optional;
16
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18
import org.eclipse.e4.ui.services.IServiceConstants;
19
import org.eclipse.jface.viewers.IStructuredSelection;
20

    
21
import eu.etaxonomy.cdm.model.description.FeatureNode;
22
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
23
import eu.etaxonomy.taxeditor.featuretree.e4.operation.RemoveFeatureOperation;
24
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
25

    
26
/**
27
 * @author pplitzner
28
 * @since Jul 12, 2017
29
 *
30
 */
31
public class RemoveFeatureHandler {
32

    
33
    @Execute
34
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
35
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection){
36
        FeatureTreeEditor editor = (FeatureTreeEditor) thisPart.getObject();
37
        for (Object selectedObject : selection.toArray()) {
38
            FeatureNode featureNode = (FeatureNode) selectedObject;
39
            RemoveFeatureOperation operation = new RemoveFeatureOperation(featureNode, editor, editor);
40
            editor.addOperation(operation);
41
            FeatureNode parent = featureNode.getParent();
42
            parent.removeChild(featureNode);
43
        }
44
        editor.setDirty(true);
45
        editor.refresh();
46
    }
47

    
48
    @CanExecute
49
    public boolean canExecute(
50
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
51
            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
52
            MHandledMenuItem menuItem) {
53
        boolean canExecute = selection!=null && !selection.isEmpty();
54
        if(selection!=null){
55
            for(Object object:selection.toArray()){
56
                canExecute &= object instanceof FeatureNode;
57
            }
58
        }
59
        canExecute &= thisPart.getObject() instanceof IE4ViewerPart;
60
        menuItem.setVisible(canExecute);
61
        return canExecute;
62
    }
63

    
64
}
(5-5/6)