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
.Messages
;
28 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
31 * Generic context menu for opening elements in the taxeditor.
34 public class CdmViewerContextMenu
extends CompoundContributionItem
{
37 protected IContributionItem
[] getContributionItems() {
38 IContributionItem
[] contributionItems
= new IContributionItem
[] {
39 new ContributionItem() {
41 public void fill(Menu menu
, int index
) {
42 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
43 final ISelection selection
= window
.getActivePage().getSelection();
44 if(selection
instanceof IStructuredSelection
){
45 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
46 Map
<String
, String
> availableViewers
= CdmViewerUtil
.getAvailableViewers(firstElement
);
47 Map
<Command
, String
> enabledCommands
= getEnabledCommands(availableViewers
);
49 //check if only one or multiple viewers/commands are available
50 if(enabledCommands
.size()==1){
51 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
52 final Command command
= entry
.getKey();
53 String viewerName
= entry
.getValue();
55 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
56 addItem
.setText(String
.format(Messages
.CdmViewerContextMenu_OPEN
, viewerName
));
57 addItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
59 else if(enabledCommands
.size()>1){
60 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
61 addItem
.setText(Messages
.CdmViewerContextMenu_OPEN_IN
);
62 Menu addMenu
= new Menu(menu
);
63 addItem
.setMenu(addMenu
);
64 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
65 final Command command
= entry
.getKey();
66 String viewerName
= entry
.getValue();
68 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
69 menuItem
.setText(viewerName
);
70 menuItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
78 return contributionItems
;
81 private Map
<Command
, String
> getEnabledCommands(Map
<String
, String
> availableViewers
) {
82 Map
<Command
, String
> enabledCommands
= new HashMap
<Command
, String
>();
83 for(Entry
<String
, String
> entry
:availableViewers
.entrySet()){
84 final String commandId
= entry
.getKey();
85 ICommandService commandService
= (ICommandService
)PlatformUI
.getWorkbench().getService(ICommandService
.class);
86 Command command
= commandService
.getCommand(commandId
);
87 if(command
.isEnabled()){
88 enabledCommands
.put(command
, entry
.getValue());
91 return enabledCommands
;
94 private final class CommandInvoker
extends SelectionAdapter
{
95 private final Command command
;
96 private final Object selectedObject
;
98 private CommandInvoker(Command command
, Object selectedObject
) {
99 this.command
= command
;
100 this.selectedObject
= selectedObject
;
104 public void widgetSelected(SelectionEvent e
) {
105 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
106 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
107 if(selectedObject
instanceof ICdmBase
){
108 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid()); //$NON-NLS-1$
110 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
112 if(parameterizedCommand
!=null){
113 handlerService
.executeCommand(parameterizedCommand
, null);
116 handlerService
.executeCommand(command
.getId(), null);
118 } catch (NotDefinedException nde
) {
119 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
120 } catch (Exception exception
) {
121 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+command
.getId(), exception
); //$NON-NLS-1$
123 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();