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
.IWorkbenchPage
;
21 import org
.eclipse
.ui
.IWorkbenchWindow
;
22 import org
.eclipse
.ui
.PlatformUI
;
23 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
24 import org
.eclipse
.ui
.handlers
.IHandlerService
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
28 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
29 import eu
.etaxonomy
.taxeditor
.Messages
;
30 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
31 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
34 * Generic context menu for opening elements in the taxeditor.
37 public class CdmViewerContextMenu
extends CompoundContributionItem
{
40 protected IContributionItem
[] getContributionItems() {
41 IContributionItem
[] contributionItems
= new IContributionItem
[] {
42 new ContributionItem() {
44 public void fill(Menu menu
, int index
) {
45 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
46 IWorkbenchPage activePage
= window
.getActivePage();
48 final ISelection selection
= activePage
.getSelection();
49 if(selection
instanceof IStructuredSelection
){
50 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
51 Map
<Command
, String
> enabledCommands
= CdmViewerUtil
.getAvailableViewers(firstElement
);
53 //check if only one or multiple viewers/commands are available
54 if(enabledCommands
.size()==1){
55 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
56 final Command command
= entry
.getKey();
57 String viewerName
= entry
.getValue();
59 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
60 addItem
.setText(String
.format(Messages
.CdmViewerContextMenu_OPEN
, viewerName
));
61 addItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
63 else if(enabledCommands
.size()>1){
64 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
65 addItem
.setText(Messages
.CdmViewerContextMenu_OPEN_IN
);
66 Menu addMenu
= new Menu(menu
);
67 addItem
.setMenu(addMenu
);
68 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
69 final Command command
= entry
.getKey();
70 String viewerName
= entry
.getValue();
72 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
73 menuItem
.setText(viewerName
);
74 menuItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
83 return contributionItems
;
86 private final class CommandInvoker
extends SelectionAdapter
{
87 private final Command command
;
88 private Object selectedObject
;
90 private CommandInvoker(Command command
, Object selectedObject
) {
91 this.command
= command
;
92 this.selectedObject
= selectedObject
;
96 public void widgetSelected(SelectionEvent e
) {
97 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
98 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
99 //for generic UuidAndTitleCache objects try to load the object
100 if (selectedObject
instanceof UuidAndTitleCache
){
101 selectedObject
= CdmStore
.getCommonService().find(CdmBase
.class, ((UuidAndTitleCache
)selectedObject
).getUuid());
103 if(selectedObject
instanceof ICdmBase
){
104 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid()); //$NON-NLS-1$
106 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
108 if(parameterizedCommand
!=null){
109 handlerService
.executeCommand(parameterizedCommand
, null);
112 handlerService
.executeCommand(command
.getId(), null);
114 } catch (NotDefinedException nde
) {
115 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
116 } catch (Exception exception
) {
117 MessagingUtils
.error(getClass(), "An exception occured while trying to execute "+command
.getId(), exception
); //$NON-NLS-1$