Project

General

Profile

Download (2.82 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.core.runtime.NullProgressMonitor;
7
import org.eclipse.e4.core.di.annotations.CanExecute;
8
import org.eclipse.e4.core.di.annotations.Execute;
9
import org.eclipse.e4.core.di.annotations.Optional;
10
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
11
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
12
import org.eclipse.e4.ui.services.IServiceConstants;
13
import org.eclipse.jface.util.LocalSelectionTransfer;
14
import org.eclipse.jface.viewers.ISelection;
15
import org.eclipse.jface.viewers.IStructuredSelection;
16

    
17
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
18
import eu.etaxonomy.cdm.model.description.FeatureNode;
19
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDropAdapter;
20
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
21
import eu.etaxonomy.taxeditor.model.MessagingUtils;
22
import eu.etaxonomy.taxeditor.store.CdmStore;
23

    
24
public class PasteFeatureHandler {
25

    
26
    @Execute
27
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
28
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection){
29
        FeatureTreeEditor editor = (FeatureTreeEditor)part.getObject();
30

    
31
        if (editor.isDirty()) {
32
            if (MessagingUtils.confirmDialog(FeatureNodeDropAdapter.SAVE_CHANGES_TITLE,
33
                    FeatureNodeDropAdapter.SAVE_CHANGES_MESSAGE)){
34
                editor.save(new NullProgressMonitor());
35
            }
36
            else{
37
                return;
38
            }
39
        }
40

    
41
        ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
42
        FeatureNode parentNode = (FeatureNode) selection.getFirstElement();
43
        FeatureNode copiedNode = (FeatureNode) ((IStructuredSelection)clipBoardSelection).getFirstElement();
44

    
45
        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parentNode.getUuid(), copiedNode.getFeature().getUuid());
46

    
47
        editor.getViewer().reveal(copiedNode);
48
        editor.setDirty(true);
49
        editor.getViewer().refresh();
50
    }
51

    
52
    @CanExecute
53
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
54
            MHandledMenuItem menuItem){
55
        boolean canExecute = false;
56
        ISelection transferSelection = LocalSelectionTransfer.getTransfer().getSelection();
57
        canExecute = selection!=null
58
                && selection.size()==1
59
                && selection.getFirstElement() instanceof FeatureNode
60
                && transferSelection instanceof IStructuredSelection
61
                && ((IStructuredSelection)transferSelection).size()==1
62
                && ((IStructuredSelection)transferSelection).getFirstElement() instanceof FeatureNode;
63
        menuItem.setVisible(canExecute);
64
        return canExecute;
65
    }
66

    
67
}
(6-6/8)