Project

General

Profile

Download (2.33 KB) Statistics
| Branch: | Tag: | Revision:
1

    
2
package eu.etaxonomy.taxeditor.featuretree.e4.handler;
3

    
4
import javax.inject.Named;
5

    
6
import org.eclipse.e4.core.di.annotations.CanExecute;
7
import org.eclipse.e4.core.di.annotations.Execute;
8
import org.eclipse.e4.core.di.annotations.Optional;
9
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
10
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
11
import org.eclipse.e4.ui.services.IServiceConstants;
12
import org.eclipse.jface.util.LocalSelectionTransfer;
13
import org.eclipse.jface.viewers.ISelection;
14
import org.eclipse.jface.viewers.IStructuredSelection;
15

    
16
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
17
import eu.etaxonomy.cdm.model.description.FeatureNode;
18
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
19
import eu.etaxonomy.taxeditor.store.CdmStore;
20

    
21
public class PasteFeatureHandler {
22

    
23
    @Execute
24
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
25
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection){
26
        ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
27
        FeatureTreeEditor editor = (FeatureTreeEditor)part.getObject();
28
        FeatureNode parentNode = (FeatureNode) selection.getFirstElement();
29
        FeatureNode copiedNode = (FeatureNode) ((IStructuredSelection)clipBoardSelection).getFirstElement();
30

    
31
        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parentNode.getUuid(), copiedNode.getFeature().getUuid());
32

    
33
        editor.getViewer().reveal(copiedNode);
34
        editor.setDirty(true);
35
        editor.getViewer().refresh();
36
    }
37

    
38
    @CanExecute
39
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
40
            MHandledMenuItem menuItem){
41
        boolean canExecute = false;
42
        ISelection transferSelection = LocalSelectionTransfer.getTransfer().getSelection();
43
        canExecute = selection!=null
44
                && selection.size()==1
45
                && selection.getFirstElement() instanceof FeatureNode
46
                && transferSelection instanceof IStructuredSelection
47
                && ((IStructuredSelection)transferSelection).size()==1
48
                && ((IStructuredSelection)transferSelection).getFirstElement() instanceof FeatureNode;
49
        menuItem.setVisible(canExecute);
50
        return canExecute;
51
    }
52

    
53
}
(6-6/8)