Project

General

Profile

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

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

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

    
25
import eu.etaxonomy.cdm.model.common.ICdmBase;
26
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
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
        Object firstElement = null;
47
        if(selectedObject instanceof IStructuredSelection){
48
            firstElement = ((IStructuredSelection) selectedObject).getFirstElement();
49
        }else{
50
            firstElement = selectedObject;
51
        }
52

    
53
        if (firstElement instanceof FeatureNodeContainer){
54
            return ;
55
        }
56
        if(firstElement instanceof TreeNode){
57
            firstElement = ((TreeNode) firstElement).getValue();
58
        }
59

    
60
        if (firstElement instanceof NomenclaturalSource){
61
            firstElement = ((NomenclaturalSource)firstElement).getSourcedName();
62
        }
63
        Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(firstElement, commandService, handlerService);
64

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

    
95
    public MHandledMenuItem addCommand(Object selection, Command command, String commandLabel) {
96
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
97
        menuItem.setLabel(commandLabel);
98
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
99
        mCommand.setElementId(command.getId());
100
        mCommand.setCommandName(commandLabel);
101
        try {
102
            mCommand.setCommandName(command.getName());
103
        } catch (NotDefinedException e) {
104
            e.printStackTrace();
105
        }
106
        //set params
107

    
108
          menuItem.getTransientData().put(command.getId()+".uuid", selection);
109

    
110
//        MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
111
//        parameter.setElementId(command.getId()+".uuid");
112
//        parameter.setValue(uuid.toString());
113
//        menuItem.getParameters().add(parameter);
114

    
115

    
116
        menuItem.setCommand(mCommand);
117
        return menuItem;
118
    }
119

    
120
}
(2-2/4)