1 package eu
.etaxonomy
.taxeditor
.view
;
5 import java
.util
.Map
.Entry
;
7 import javax
.inject
.Inject
;
8 import javax
.inject
.Named
;
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
;
25 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
26 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
27 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
28 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
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
) {
45 Object firstElement
= null;
46 if(selectedObject
instanceof IStructuredSelection
){
47 firstElement
= ((IStructuredSelection
) selectedObject
).getFirstElement();
49 firstElement
= selectedObject
;
52 if (firstElement
instanceof FeatureNodeContainer
){
55 if(firstElement
instanceof TreeNode
){
56 firstElement
= ((TreeNode
) firstElement
).getValue();
59 Map
<Command
, String
> enabledCommands
= CdmViewerUtilE4
.getAvailableViewers(firstElement
, commandService
, handlerService
);
63 // List<UUID> uuids = null;
64 // //for generic UuidAndTitleCache objects try to load the object
65 // if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection && ((IStructuredSelection)selectedObject).size() == 1){
66 // uuid = ((UuidAndTitleCache)firstElement).getUuid();
67 // }else if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection){
68 // uuids = new ArrayList<>();
69 // Iterator<IStructuredSelection> iterator = ((IStructuredSelection)selectedObject).iterator();
70 // UuidAndTitleCache sel;
72 // while(iterator.hasNext()){
73 // sel = (UuidAndTitleCache)iterator.next();
74 // uuidForList = sel.getUuid();
75 // uuids.add(uuidForList);
82 // if(selectedObject instanceof ICdmBase){
83 // uuid = ((ICdmBase) selectedObject).getUuid();
86 //check if only one or multiple viewers/commands are available
87 if(enabledCommands
.size()==1){
88 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
89 final Command command
= entry
.getKey();
90 String viewerName
= entry
.getValue();
91 MMenu menu
= MMenuFactory
.INSTANCE
.createMenu();
92 menu
.setLabel(Messages
.CdmViewerContextMenu_OPEN_IN
);
94 // items.add(addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName)));
95 menu
.getChildren().add(addCommand(selectedObject
, command
, viewerName
));
97 else if(enabledCommands
.size()>1){
98 MMenu menu
= MMenuFactory
.INSTANCE
.createMenu();
99 menu
.setLabel(Messages
.CdmViewerContextMenu_OPEN_IN
);
101 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
102 String viewerName
= entry
.getValue();
103 Command command
= entry
.getKey();
104 if (selectedObject
instanceof IStructuredSelection
){
105 menu
.getChildren().add(addCommand(selectedObject
, command
, viewerName
));
106 }else if(selectedObject
instanceof ICdmBase
){
107 // UUID uuid = ((ICdmBase) selectedObject).getUuid();
108 menu
.getChildren().add(addCommand(selectedObject
, command
, viewerName
));
109 }else if (selectedObject
instanceof UuidAndTitleCache
){
110 menu
.getChildren().add(addCommand(selectedObject
, command
, viewerName
));
116 public MHandledMenuItem
addCommand(Object selection
, Command command
, String commandLabel
) {
117 MHandledMenuItem menuItem
= MMenuFactory
.INSTANCE
.createHandledMenuItem();
118 menuItem
.setLabel(commandLabel
);
119 MCommand mCommand
= MCommandsFactory
.INSTANCE
.createCommand();
120 mCommand
.setElementId(command
.getId());
121 mCommand
.setCommandName(commandLabel
);
123 mCommand
.setCommandName(command
.getName());
124 } catch (NotDefinedException e
) {
129 menuItem
.getTransientData().put(command
.getId()+".uuid", selection
);
131 // MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
132 // parameter.setElementId(command.getId()+".uuid");
133 // parameter.setValue(uuid.toString());
134 // menuItem.getParameters().add(parameter);
137 menuItem
.setCommand(mCommand
);