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
.handlers
.IHandlerService
;
25 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
26 import eu
.etaxonomy
.taxeditor
.Messages
;
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
<Command
, String
> enabledCommands
= CdmViewerUtil
.getAvailableViewers(firstElement
);
47 //check if only one or multiple viewers/commands are available
48 if(enabledCommands
.size()==1){
49 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
50 final Command command
= entry
.getKey();
51 String viewerName
= entry
.getValue();
53 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
54 addItem
.setText(String
.format(Messages
.CdmViewerContextMenu_OPEN
, viewerName
));
55 addItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
57 else if(enabledCommands
.size()>1){
58 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
59 addItem
.setText(Messages
.CdmViewerContextMenu_OPEN_IN
);
60 Menu addMenu
= new Menu(menu
);
61 addItem
.setMenu(addMenu
);
62 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
63 final Command command
= entry
.getKey();
64 String viewerName
= entry
.getValue();
66 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
67 menuItem
.setText(viewerName
);
68 menuItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
76 return contributionItems
;
79 private final class CommandInvoker
extends SelectionAdapter
{
80 private final Command command
;
81 private final Object selectedObject
;
83 private CommandInvoker(Command command
, Object selectedObject
) {
84 this.command
= command
;
85 this.selectedObject
= selectedObject
;
89 public void widgetSelected(SelectionEvent e
) {
90 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
91 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
92 if(selectedObject
instanceof ICdmBase
){
93 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid()); //$NON-NLS-1$
95 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
97 if(parameterizedCommand
!=null){
98 handlerService
.executeCommand(parameterizedCommand
, null);
101 handlerService
.executeCommand(command
.getId(), null);
103 } catch (NotDefinedException nde
) {
104 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
105 } catch (Exception exception
) {
106 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+command
.getId(), exception
); //$NON-NLS-1$
108 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();