Project

General

Profile

Download (4.41 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.view;
2

    
3
import java.util.List;
4
import java.util.Map;
5
import java.util.Map.Entry;
6
import java.util.UUID;
7

    
8
import javax.inject.Inject;
9
import javax.inject.Named;
10

    
11
import org.eclipse.core.commands.Command;
12
import org.eclipse.core.commands.common.NotDefinedException;
13
import org.eclipse.e4.core.commands.ECommandService;
14
import org.eclipse.e4.core.commands.EHandlerService;
15
import org.eclipse.e4.ui.di.AboutToShow;
16
import org.eclipse.e4.ui.model.application.commands.MCommand;
17
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
18
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
20
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
21
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.jface.viewers.TreeNode;
25

    
26
import eu.etaxonomy.cdm.model.common.ICdmBase;
27
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
28
import eu.etaxonomy.taxeditor.l10n.Messages;
29
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
30

    
31
/**
32
 * Generic context menu for opening elements in the taxeditor.
33
 *
34
 */
35
public class CdmViewerContextMenuE4 {
36

    
37
    @Inject
38
    private EHandlerService handlerService;
39

    
40
    @Inject
41
    private ECommandService commandService;
42

    
43
    @AboutToShow
44
    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selectedObject) {
45

    
46

    
47
        if(selectedObject instanceof IStructuredSelection){
48
            selectedObject = ((IStructuredSelection) selectedObject).getFirstElement();
49
        }
50

    
51
        if (selectedObject instanceof FeatureNodeContainer){
52
            return ;
53
        }
54
        if(selectedObject instanceof TreeNode){
55
            selectedObject = ((TreeNode) selectedObject).getValue();
56
        }
57
        Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(selectedObject, commandService, handlerService);
58

    
59
        //get UUID
60
        UUID uuid = null;
61
        //for generic UuidAndTitleCache objects try to load the object
62
        if (selectedObject instanceof UuidAndTitleCache){
63
            uuid = ((UuidAndTitleCache)selectedObject).getUuid();
64
        }
65
        if(selectedObject instanceof ICdmBase){
66
            uuid = ((ICdmBase) selectedObject).getUuid();
67
        }
68

    
69
        //check if only one or multiple viewers/commands are available
70
        if(enabledCommands.size()==1){
71
            Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
72
            final Command command = entry.getKey();
73
            String viewerName = entry.getValue();
74
            MMenu menu = MMenuFactory.INSTANCE.createMenu();
75
            menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
76
            items.add(menu);
77
           // items.add(addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName)));
78
            menu.getChildren().add(addCommand(uuid, command, viewerName));
79
        }
80
        else if(enabledCommands.size()>1){
81
            MMenu menu = MMenuFactory.INSTANCE.createMenu();
82
            menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
83
            items.add(menu);
84
            for(Entry<Command, String> entry:enabledCommands.entrySet()){
85
                String viewerName = entry.getValue();
86
                Command command = entry.getKey();
87
                menu.getChildren().add(addCommand(uuid, command, viewerName));
88
            }
89
        }
90
    }
91

    
92
    public MHandledMenuItem addCommand(UUID uuid, Command command, String commandLabel) {
93
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
94
        menuItem.setLabel(commandLabel);
95
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
96
        mCommand.setElementId(command.getId());
97
        mCommand.setCommandName(commandLabel);
98
        try {
99
            mCommand.setCommandName(command.getName());
100
        } catch (NotDefinedException e) {
101
            e.printStackTrace();
102
        }
103
        //set params
104
        menuItem.getTransientData().put(command.getId()+".uuid", uuid);
105
//        MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
106
//        parameter.setElementId(command.getId()+".uuid");
107
//        parameter.setValue(uuid.toString());
108
//        menuItem.getParameters().add(parameter);
109

    
110
        menuItem.setCommand(mCommand);
111
        return menuItem;
112
    }
113

    
114
}
(2-2/4)