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.taxeditor.l10n.Messages;
|
26
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
27
|
|
28
|
/**
|
29
|
* Generic context menu for opening elements in the taxeditor.
|
30
|
*
|
31
|
*/
|
32
|
public class CdmViewerContextMenuE4 {
|
33
|
|
34
|
@Inject
|
35
|
private EHandlerService handlerService;
|
36
|
|
37
|
@Inject
|
38
|
private ECommandService commandService;
|
39
|
|
40
|
@AboutToShow
|
41
|
public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selectedObject) {
|
42
|
|
43
|
Object firstElement = null;
|
44
|
if(selectedObject instanceof IStructuredSelection){
|
45
|
firstElement = ((IStructuredSelection) selectedObject).getFirstElement();
|
46
|
}else{
|
47
|
firstElement = selectedObject;
|
48
|
}
|
49
|
|
50
|
if (firstElement instanceof FeatureNodeContainer){
|
51
|
return ;
|
52
|
}
|
53
|
if(firstElement instanceof TreeNode){
|
54
|
firstElement = ((TreeNode) selectedObject).getValue();
|
55
|
}
|
56
|
|
57
|
Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(firstElement, commandService, handlerService);
|
58
|
|
59
|
//get UUID
|
60
|
// UUID uuid = null;
|
61
|
// List<UUID> uuids = null;
|
62
|
// //for generic UuidAndTitleCache objects try to load the object
|
63
|
// if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection && ((IStructuredSelection)selectedObject).size() == 1){
|
64
|
// uuid = ((UuidAndTitleCache)firstElement).getUuid();
|
65
|
// }else if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection){
|
66
|
// uuids = new ArrayList<>();
|
67
|
// Iterator<IStructuredSelection> iterator = ((IStructuredSelection)selectedObject).iterator();
|
68
|
// UuidAndTitleCache sel;
|
69
|
// UUID uuidForList;
|
70
|
// while(iterator.hasNext()){
|
71
|
// sel = (UuidAndTitleCache)iterator.next();
|
72
|
// uuidForList = sel.getUuid();
|
73
|
// uuids.add(uuidForList);
|
74
|
//
|
75
|
// }
|
76
|
//
|
77
|
// }
|
78
|
|
79
|
//TODO!!
|
80
|
// if(selectedObject instanceof ICdmBase){
|
81
|
// uuid = ((ICdmBase) selectedObject).getUuid();
|
82
|
// }
|
83
|
|
84
|
//check if only one or multiple viewers/commands are available
|
85
|
if(enabledCommands.size()==1){
|
86
|
Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
|
87
|
final Command command = entry.getKey();
|
88
|
String viewerName = entry.getValue();
|
89
|
MMenu menu = MMenuFactory.INSTANCE.createMenu();
|
90
|
menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
|
91
|
items.add(menu);
|
92
|
// items.add(addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName)));
|
93
|
menu.getChildren().add(addCommand((IStructuredSelection)selectedObject, command, viewerName));
|
94
|
}
|
95
|
else if(enabledCommands.size()>1){
|
96
|
MMenu menu = MMenuFactory.INSTANCE.createMenu();
|
97
|
menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
|
98
|
items.add(menu);
|
99
|
for(Entry<Command, String> entry:enabledCommands.entrySet()){
|
100
|
String viewerName = entry.getValue();
|
101
|
Command command = entry.getKey();
|
102
|
menu.getChildren().add(addCommand((IStructuredSelection)selectedObject, command, viewerName));
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
|
107
|
public MHandledMenuItem addCommand(IStructuredSelection selection, Command command, String commandLabel) {
|
108
|
MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
|
109
|
menuItem.setLabel(commandLabel);
|
110
|
MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
|
111
|
mCommand.setElementId(command.getId());
|
112
|
mCommand.setCommandName(commandLabel);
|
113
|
try {
|
114
|
mCommand.setCommandName(command.getName());
|
115
|
} catch (NotDefinedException e) {
|
116
|
e.printStackTrace();
|
117
|
}
|
118
|
//set params
|
119
|
|
120
|
menuItem.getTransientData().put(command.getId()+".uuid", selection);
|
121
|
|
122
|
// MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
|
123
|
// parameter.setElementId(command.getId()+".uuid");
|
124
|
// parameter.setValue(uuid.toString());
|
125
|
// menuItem.getParameters().add(parameter);
|
126
|
|
127
|
|
128
|
menuItem.setCommand(mCommand);
|
129
|
return menuItem;
|
130
|
}
|
131
|
|
132
|
}
|