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
.jface
.viewers
.TreeNode
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.events
.SelectionAdapter
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.widgets
.Menu
;
20 import org
.eclipse
.swt
.widgets
.MenuItem
;
21 import org
.eclipse
.ui
.IWorkbenchPage
;
22 import org
.eclipse
.ui
.IWorkbenchWindow
;
23 import org
.eclipse
.ui
.PlatformUI
;
24 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
25 import org
.eclipse
.ui
.handlers
.IHandlerService
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
29 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
30 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
31 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
32 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
35 * Generic context menu for opening elements in the taxeditor.
38 public class CdmViewerContextMenu
extends CompoundContributionItem
{
41 protected IContributionItem
[] getContributionItems() {
42 IContributionItem
[] contributionItems
= new IContributionItem
[] {
43 new ContributionItem() {
45 public void fill(Menu menu
, int index
) {
46 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
47 IWorkbenchPage activePage
= window
.getActivePage();
49 final ISelection selection
= activePage
.getSelection();
50 if(selection
instanceof IStructuredSelection
){
51 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
52 Map
<Command
, String
> enabledCommands
= CdmViewerUtil
.getAvailableViewers(firstElement
);
54 //check if only one or multiple viewers/commands are available
55 if(enabledCommands
.size()==1){
56 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
57 final Command command
= entry
.getKey();
58 String viewerName
= entry
.getValue();
60 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
61 addItem
.setText(String
.format(Messages
.CdmViewerContextMenu_OPEN
, viewerName
));
62 addItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
64 else if(enabledCommands
.size()>1){
65 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
66 addItem
.setText(Messages
.CdmViewerContextMenu_OPEN_IN
);
67 Menu addMenu
= new Menu(menu
);
68 addItem
.setMenu(addMenu
);
69 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
70 final Command command
= entry
.getKey();
71 String viewerName
= entry
.getValue();
73 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
74 menuItem
.setText(viewerName
);
75 menuItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
84 return contributionItems
;
87 private final class CommandInvoker
extends SelectionAdapter
{
88 private final Command command
;
89 private Object selectedObject
;
91 private CommandInvoker(Command command
, Object selectedObject
) {
92 this.command
= command
;
93 this.selectedObject
= selectedObject
;
97 public void widgetSelected(SelectionEvent e
) {
98 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
99 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
100 //for generic UuidAndTitleCache objects try to load the object
101 if (selectedObject
instanceof UuidAndTitleCache
){
102 selectedObject
= CdmStore
.getCommonService().find(CdmBase
.class, ((UuidAndTitleCache
)selectedObject
).getUuid());
104 //for tree nodes get the value resp. the object of the node
105 else if (selectedObject
instanceof TreeNode
){
106 selectedObject
= ((TreeNode
) selectedObject
).getValue();
108 if(selectedObject
instanceof ICdmBase
){
109 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid()); //$NON-NLS-1$
111 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
113 if(parameterizedCommand
!=null){
114 handlerService
.executeCommand(parameterizedCommand
, null);
117 handlerService
.executeCommand(command
.getId(), null);
119 } catch (NotDefinedException nde
) {
120 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
121 } catch (Exception exception
) {
122 MessagingUtils
.error(getClass(), "An exception occured while trying to execute "+command
.getId(), exception
); //$NON-NLS-1$