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
.api
.service
.IOccurrenceService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
27 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
32 import eu
.etaxonomy
.taxeditor
.Messages
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 * Generic context menu for opening elements in the taxeditor.
40 public class CdmViewerContextMenu
extends CompoundContributionItem
{
43 protected IContributionItem
[] getContributionItems() {
44 IContributionItem
[] contributionItems
= new IContributionItem
[] {
45 new ContributionItem() {
47 public void fill(Menu menu
, int index
) {
48 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
49 final ISelection selection
= window
.getActivePage().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
)) ;
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 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
)selectedObject
;
102 Class type
= uuidAndTitleCache
.getType();
103 if(type
== Taxon
.class || type
== Synonym
.class){
104 selectedObject
= CdmStore
.getService(ITaxonService
.class).load(uuidAndTitleCache
.getUuid());
106 else if(SpecimenOrObservationBase
.class.isAssignableFrom(type
)){
107 selectedObject
= CdmStore
.getService(IOccurrenceService
.class).load(uuidAndTitleCache
.getUuid());
110 if(selectedObject
instanceof ICdmBase
){
111 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid()); //$NON-NLS-1$
113 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
115 if(parameterizedCommand
!=null){
116 handlerService
.executeCommand(parameterizedCommand
, null);
119 handlerService
.executeCommand(command
.getId(), null);
121 } catch (NotDefinedException nde
) {
122 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
123 } catch (Exception exception
) {
124 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+command
.getId(), exception
); //$NON-NLS-1$
126 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();