1 package eu
.etaxonomy
.taxeditor
.view
;
3 import java
.util
.HashMap
;
5 import java
.util
.Map
.Entry
;
8 import org
.eclipse
.core
.commands
.Command
;
9 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
10 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
11 import org
.eclipse
.jface
.action
.ContributionItem
;
12 import org
.eclipse
.jface
.action
.IContributionItem
;
13 import org
.eclipse
.jface
.viewers
.ISelection
;
14 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.SelectionAdapter
;
17 import org
.eclipse
.swt
.events
.SelectionEvent
;
18 import org
.eclipse
.swt
.widgets
.Menu
;
19 import org
.eclipse
.swt
.widgets
.MenuItem
;
20 import org
.eclipse
.ui
.IWorkbenchWindow
;
21 import org
.eclipse
.ui
.PlatformUI
;
22 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
23 import org
.eclipse
.ui
.commands
.ICommandService
;
24 import org
.eclipse
.ui
.handlers
.IHandlerService
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
27 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
30 * Generic context menu for opening elements in the taxeditor.
33 public class CdmViewerContextMenu
extends CompoundContributionItem
{
36 protected IContributionItem
[] getContributionItems() {
37 IContributionItem
[] contributionItems
= new IContributionItem
[] {
38 new ContributionItem() {
40 public void fill(Menu menu
, int index
) {
41 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
42 final ISelection selection
= window
.getActivePage().getSelection();
43 if(selection
instanceof IStructuredSelection
){
44 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
45 Map
<String
, String
> availableViewers
= CdmViewerUtil
.getAvailableViewers(firstElement
);
46 Map
<Command
, String
> enabledCommands
= getEnabledCommands(availableViewers
);
48 //check if only one or multiple viewers/commands are available
49 if(enabledCommands
.size()==1){
50 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
51 final Command command
= entry
.getKey();
52 String viewerName
= entry
.getValue();
54 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
55 addItem
.setText(String
.format("Open (%s)", viewerName
));
56 addItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
58 else if(enabledCommands
.size()>1){
59 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
60 addItem
.setText("Open in...");
61 Menu addMenu
= new Menu(menu
);
62 addItem
.setMenu(addMenu
);
63 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
64 final Command command
= entry
.getKey();
65 String viewerName
= entry
.getValue();
67 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
68 menuItem
.setText(viewerName
);
69 menuItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
77 return contributionItems
;
80 private Map
<Command
, String
> getEnabledCommands(Map
<String
, String
> availableViewers
) {
81 Map
<Command
, String
> enabledCommands
= new HashMap
<Command
, String
>();
82 for(Entry
<String
, String
> entry
:availableViewers
.entrySet()){
83 final String commandId
= entry
.getKey();
84 ICommandService commandService
= (ICommandService
)PlatformUI
.getWorkbench().getService(ICommandService
.class);
85 Command command
= commandService
.getCommand(commandId
);
86 if(command
.isEnabled()){
87 enabledCommands
.put(command
, entry
.getValue());
90 return enabledCommands
;
93 private final class CommandInvoker
extends SelectionAdapter
{
94 private final Command command
;
95 private final Object selectedObject
;
97 private CommandInvoker(Command command
, Object selectedObject
) {
98 this.command
= command
;
99 this.selectedObject
= selectedObject
;
103 public void widgetSelected(SelectionEvent e
) {
104 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
105 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
106 if(selectedObject
instanceof ICdmBase
){
107 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid());
109 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
111 handlerService
.executeCommand(parameterizedCommand
, null);
112 } catch (NotDefinedException nde
) {
113 throw new RuntimeException("Could not find open command: " + command
.getId());
114 } catch (Exception exception
) {
115 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+command
.getId(), exception
);
117 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();