Project

General

Profile

Download (5.68 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
        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
        Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(firstElement, commandService, handlerService);
61

    
62
        //get UUID
63
//        UUID uuid = null;
64
//        List<UUID> uuids = null;
65
//        //for generic UuidAndTitleCache objects try to load the object
66
//        if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection && ((IStructuredSelection)selectedObject).size() == 1){
67
//            uuid = ((UuidAndTitleCache)firstElement).getUuid();
68
//        }else if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection){
69
//            uuids = new ArrayList<>();
70
//            Iterator<IStructuredSelection> iterator = ((IStructuredSelection)selectedObject).iterator();
71
//            UuidAndTitleCache sel;
72
//            UUID uuidForList;
73
//            while(iterator.hasNext()){
74
//                sel = (UuidAndTitleCache)iterator.next();
75
//                uuidForList = sel.getUuid();
76
//                uuids.add(uuidForList);
77
//
78
//            }
79
//
80
//        }
81

    
82
        //TODO!!
83
//        if(selectedObject instanceof ICdmBase){
84
//            uuid = ((ICdmBase) selectedObject).getUuid();
85
//        }
86

    
87
        //check if only one or multiple viewers/commands are available
88
        if(enabledCommands.size()==1){
89
            Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
90
            final Command command = entry.getKey();
91
            String viewerName = entry.getValue();
92
            MMenu menu = MMenuFactory.INSTANCE.createMenu();
93
            menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
94
            items.add(menu);
95
           // items.add(addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName)));
96
            menu.getChildren().add(addCommand(selectedObject, command, viewerName));
97
        }
98
        else if(enabledCommands.size()>1){
99
            MMenu menu = MMenuFactory.INSTANCE.createMenu();
100
            menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
101
            items.add(menu);
102
            for(Entry<Command, String> entry:enabledCommands.entrySet()){
103
                String viewerName = entry.getValue();
104
                Command command = entry.getKey();
105
                if (selectedObject instanceof IStructuredSelection){
106
                    menu.getChildren().add(addCommand(selectedObject, command, viewerName));
107
                }else if(selectedObject instanceof ICdmBase){
108
                    UUID uuid = ((ICdmBase) selectedObject).getUuid();
109
                    menu.getChildren().add(addCommand(uuid, command, viewerName));
110
                }else if (selectedObject instanceof UuidAndTitleCache){
111
                   menu.getChildren().add(addCommand(selectedObject, command, viewerName));
112
                }
113
            }
114
        }
115
    }
116

    
117
    public MHandledMenuItem addCommand(Object selection, Command command, String commandLabel) {
118
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
119
        menuItem.setLabel(commandLabel);
120
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
121
        mCommand.setElementId(command.getId());
122
        mCommand.setCommandName(commandLabel);
123
        try {
124
            mCommand.setCommandName(command.getName());
125
        } catch (NotDefinedException e) {
126
            e.printStackTrace();
127
        }
128
        //set params
129

    
130
          menuItem.getTransientData().put(command.getId()+".uuid", selection);
131

    
132
//        MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
133
//        parameter.setElementId(command.getId()+".uuid");
134
//        parameter.setValue(uuid.toString());
135
//        menuItem.getParameters().add(parameter);
136

    
137

    
138
        menuItem.setCommand(mCommand);
139
        return menuItem;
140
    }
141

    
142
}
(2-2/4)