1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.view;
|
10
|
|
11
|
import java.util.List;
|
12
|
import java.util.Map;
|
13
|
import java.util.Map.Entry;
|
14
|
|
15
|
import javax.inject.Inject;
|
16
|
import javax.inject.Named;
|
17
|
|
18
|
import org.eclipse.core.commands.Command;
|
19
|
import org.eclipse.core.commands.common.NotDefinedException;
|
20
|
import org.eclipse.e4.core.commands.ECommandService;
|
21
|
import org.eclipse.e4.core.commands.EHandlerService;
|
22
|
import org.eclipse.e4.ui.di.AboutToShow;
|
23
|
import org.eclipse.e4.ui.model.application.commands.MCommand;
|
24
|
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
|
25
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
26
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
|
27
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
|
28
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
|
29
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
32
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
33
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
34
|
|
35
|
/**
|
36
|
* Generic context menu for opening elements in the taxeditor.
|
37
|
*/
|
38
|
public class CdmViewerContextMenuE4 {
|
39
|
|
40
|
@Inject
|
41
|
private EHandlerService handlerService;
|
42
|
|
43
|
@Inject
|
44
|
private ECommandService commandService;
|
45
|
|
46
|
@AboutToShow
|
47
|
public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selectedObject) {
|
48
|
|
49
|
if (selectedObject == null){
|
50
|
return;
|
51
|
}
|
52
|
|
53
|
List<UuidAndTitleCache<? extends ICdmBase>> uuidTypes = CdmViewerUtil.transformSelectionToUuidAndTitleCacheList(selectedObject);
|
54
|
|
55
|
Map<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(uuidTypes, commandService, handlerService);
|
56
|
|
57
|
//check if only one or multiple viewers/commands are available
|
58
|
if (!enabledCommands.isEmpty()){
|
59
|
MMenu menu = MMenuFactory.INSTANCE.createMenu();
|
60
|
menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
|
61
|
items.add(menu);
|
62
|
for(Entry<Command, String> entry: enabledCommands.entrySet()){
|
63
|
String viewerName = entry.getValue();
|
64
|
Command command = entry.getKey();
|
65
|
menu.getChildren().add(addCommand(uuidTypes, command, viewerName));
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
|
70
|
|
71
|
public MHandledMenuItem addCommand(List<UuidAndTitleCache<? extends ICdmBase>> selectedUuids, Command command, String commandLabel) {
|
72
|
MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
|
73
|
menuItem.setLabel(commandLabel);
|
74
|
MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
|
75
|
mCommand.setElementId(command.getId());
|
76
|
mCommand.setCommandName(commandLabel);
|
77
|
try {
|
78
|
mCommand.setCommandName(command.getName());
|
79
|
} catch (NotDefinedException e) {
|
80
|
e.printStackTrace();
|
81
|
}
|
82
|
//set params
|
83
|
|
84
|
menuItem.getTransientData().put(command.getId()+".uuid", selectedUuids);
|
85
|
|
86
|
// MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
|
87
|
// parameter.setElementId(command.getId()+".uuid");
|
88
|
// parameter.setValue(uuid.toString());
|
89
|
// menuItem.getParameters().add(parameter);
|
90
|
|
91
|
menuItem.setCommand(mCommand);
|
92
|
return menuItem;
|
93
|
}
|
94
|
}
|