Project

General

Profile

Download (2.39 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 java.util.List;
12

    
13
import javax.inject.Named;
14

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

    
23
import eu.etaxonomy.cdm.model.description.FeatureTree;
24
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
25
import eu.etaxonomy.taxeditor.featuretree.e4.operation.RemoveFeatureTreeOperation;
26

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

    
34
    @Execute
35
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
36
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection){
37
        FeatureTreeEditor editor = (FeatureTreeEditor) thisPart.getObject();
38
        List<FeatureTree> trees = (List<FeatureTree>) editor.getViewer().getInput();
39
        for (Object selectedObject : selection.toArray()) {
40
            FeatureTree featureTree = (FeatureTree) selectedObject;
41
            RemoveFeatureTreeOperation operation = new RemoveFeatureTreeOperation(featureTree, editor, editor);
42
            editor.addOperation(operation);
43
            trees.remove(featureTree);
44
        }
45
        editor.getViewer().setInput(trees);
46
        editor.setDirty(true);
47
    }
48

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

    
65
}
(6-6/6)