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