1 package eu
.etaxonomy
.taxeditor
.view
;
4 import java
.util
.Map
.Entry
;
6 import org
.eclipse
.core
.commands
.Command
;
7 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
8 import org
.eclipse
.jface
.action
.ContributionItem
;
9 import org
.eclipse
.jface
.action
.IContributionItem
;
10 import org
.eclipse
.jface
.viewers
.ISelection
;
11 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.SelectionAdapter
;
14 import org
.eclipse
.swt
.events
.SelectionEvent
;
15 import org
.eclipse
.swt
.widgets
.Menu
;
16 import org
.eclipse
.swt
.widgets
.MenuItem
;
17 import org
.eclipse
.ui
.IWorkbenchWindow
;
18 import org
.eclipse
.ui
.PlatformUI
;
19 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
20 import org
.eclipse
.ui
.commands
.ICommandService
;
21 import org
.eclipse
.ui
.handlers
.IHandlerService
;
23 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
26 * Generic context menu for opening elements in the taxeditor.
29 public class CdmViewerContextMenu
extends CompoundContributionItem
{
32 protected IContributionItem
[] getContributionItems() {
33 IContributionItem
[] contributionItems
= new IContributionItem
[] {
34 new ContributionItem() {
36 public void fill(Menu menu
, int index
) {
37 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
38 final ISelection selection
= window
.getActivePage().getSelection();
40 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
41 addItem
.setText("Open in...");
42 Menu addMenu
= new Menu(menu
);
43 addItem
.setMenu(addMenu
);
44 if(selection
instanceof IStructuredSelection
){
45 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
46 Map
<String
, String
> availableViewers
= CdmViewerUtil
.getAvailableViewers(firstElement
);
47 for(Entry
<String
, String
> entry
:availableViewers
.entrySet()){
48 final String commandId
= entry
.getKey();
49 String viewerName
= entry
.getValue();
50 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
51 menuItem
.setText(viewerName
);
52 menuItem
.addSelectionListener(new SelectionAdapter() {
55 public void widgetSelected(SelectionEvent e
) {
56 ICommandService commandService
= (ICommandService
)PlatformUI
.getWorkbench().getService(ICommandService
.class);
58 Command command
= commandService
.getCommand(commandId
);
59 if(command
.isEnabled()) {
60 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
62 handlerService
.executeCommand(commandId
, null);
63 } catch (NotDefinedException nde
) {
64 throw new RuntimeException("Could not find open command: " + commandId
);
65 } catch (Exception exception
) {
66 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+commandId
, exception
);
76 return contributionItems
;
79 private class OpenInViewerListener
extends SelectionAdapter
{
81 private final ICdmViewer cdmViewer
;
82 private final Object input
;
83 private final Class
<?
> viewerClass
;
85 public OpenInViewerListener(ICdmViewer cdmViewer
, Object input
, Class
<?
> viewerClass
) {
87 this.cdmViewer
= cdmViewer
;
89 this.viewerClass
= viewerClass
;
93 public void widgetSelected(SelectionEvent e
) {
94 cdmViewer
.show(input
, viewerClass
);