1 package eu
.etaxonomy
.taxeditor
.view
;
5 import java
.util
.Map
.Entry
;
8 import javax
.inject
.Inject
;
9 import javax
.inject
.Named
;
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
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
27 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
28 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
31 * Generic context menu for opening elements in the taxeditor.
34 public class CdmViewerContextMenuE4
{
37 private EHandlerService handlerService
;
40 private ECommandService commandService
;
43 public void aboutToShow(List
<MMenuElement
> items
, @Named(IServiceConstants
.ACTIVE_SELECTION
) Object selectedObject
) {
44 if(selectedObject
instanceof IStructuredSelection
){
45 selectedObject
= ((IStructuredSelection
) selectedObject
).getFirstElement();
47 if(selectedObject
instanceof TreeNode
){
48 selectedObject
= ((TreeNode
) selectedObject
).getValue();
50 Map
<Command
, String
> enabledCommands
= CdmViewerUtilE4
.getAvailableViewers(selectedObject
, commandService
, handlerService
);
54 //for generic UuidAndTitleCache objects try to load the object
55 if (selectedObject
instanceof UuidAndTitleCache
){
56 uuid
= ((UuidAndTitleCache
)selectedObject
).getUuid();
58 if(selectedObject
instanceof ICdmBase
){
59 uuid
= ((ICdmBase
) selectedObject
).getUuid();
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
));
69 else if(enabledCommands
.size()>1){
70 MMenu menu
= MMenuFactory
.INSTANCE
.createMenu();
71 menu
.setLabel(Messages
.CdmViewerContextMenu_OPEN_IN
);
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
));
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
);
88 mCommand
.setCommandName(command
.getName());
89 } catch (NotDefinedException e
) {
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);
99 menuItem
.setCommand(mCommand
);