Project

General

Profile

Download (4.03 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

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

    
36
    @Inject
37
    private EHandlerService handlerService;
38

    
39
    @Inject
40
    private ECommandService commandService;
41

    
42
    @AboutToShow
43
    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selectedObject) {
44
        if(selectedObject instanceof IStructuredSelection){
45
            selectedObject = ((IStructuredSelection) selectedObject).getFirstElement();
46
        }
47
        if(selectedObject instanceof TreeNode){
48
            selectedObject = ((TreeNode) selectedObject).getValue();
49
        }
50
        Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(selectedObject, commandService, handlerService);
51

    
52
        //get UUID
53
        UUID uuid = null;
54
        //for generic UuidAndTitleCache objects try to load the object
55
        if (selectedObject instanceof UuidAndTitleCache){
56
            uuid = ((UuidAndTitleCache)selectedObject).getUuid();
57
        }
58
        if(selectedObject instanceof ICdmBase){
59
            uuid = ((ICdmBase) selectedObject).getUuid();
60
        }
61

    
62
        //check if only one or multiple viewers/commands are available
63
        if(enabledCommands.size()==1){
64
            Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
65
            final Command command = entry.getKey();
66
            String viewerName = entry.getValue();
67
            addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
68
        }
69
        else if(enabledCommands.size()>1){
70
            MMenu menu = MMenuFactory.INSTANCE.createMenu();
71
            menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
72
            items.add(menu);
73
            for(Entry<Command, String> entry:enabledCommands.entrySet()){
74
                String viewerName = entry.getValue();
75
                Command command = entry.getKey();
76
                menu.getChildren().add(addCommand(uuid, command, viewerName));
77
            }
78
        }
79
    }
80

    
81
    public MHandledMenuItem addCommand(UUID uuid, Command command, String commandLabel) {
82
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
83
        menuItem.setLabel(commandLabel);
84
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
85
        mCommand.setElementId(command.getId());
86
        mCommand.setCommandName(commandLabel);
87
        try {
88
            mCommand.setCommandName(command.getName());
89
        } catch (NotDefinedException e) {
90
            e.printStackTrace();
91
        }
92
        //set params
93
        menuItem.getTransientData().put(command.getId()+".uuid", uuid);
94
//        MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
95
//        parameter.setElementId(command.getId()+".uuid");
96
//        parameter.setValue(uuid.toString());
97
//        menuItem.getParameters().add(parameter);
98

    
99
        menuItem.setCommand(mCommand);
100
        return menuItem;
101
    }
102

    
103
}
(2-2/4)