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
.model
.taxon
.TaxonNode
;
30 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
31 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
33 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
36 * Generic context menu for opening elements in the taxeditor.
39 public class CdmViewerContextMenu
extends CompoundContributionItem
{
42 protected IContributionItem
[] getContributionItems() {
43 IContributionItem
[] contributionItems
= new IContributionItem
[] {
44 new ContributionItem() {
46 public void fill(Menu menu
, int index
) {
47 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
48 IWorkbenchPage activePage
= window
.getActivePage();
50 final ISelection selection
= activePage
.getSelection();
51 if(selection
instanceof IStructuredSelection
){
52 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
53 if (firstElement
instanceof TaxonNode
&& !((TaxonNode
)firstElement
).hasTaxon()){
54 firstElement
= ((TaxonNode
)firstElement
).getClassification();
56 Map
<Command
, String
> enabledCommands
= CdmViewerUtil
.getAvailableViewers(firstElement
);
58 //check if only one or multiple viewers/commands are available
59 if(enabledCommands
.size()==1){
60 Entry
<Command
, String
> entry
= enabledCommands
.entrySet().iterator().next();
61 final Command command
= entry
.getKey();
62 String viewerName
= entry
.getValue();
64 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
65 addItem
.setText(String
.format(Messages
.CdmViewerContextMenu_OPEN
, viewerName
));
66 addItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
68 else if(enabledCommands
.size()>1){
69 MenuItem addItem
= new MenuItem(menu
, SWT
.CASCADE
);
70 addItem
.setText(Messages
.CdmViewerContextMenu_OPEN_IN
);
71 Menu addMenu
= new Menu(menu
);
72 addItem
.setMenu(addMenu
);
73 for(Entry
<Command
, String
> entry
:enabledCommands
.entrySet()){
74 final Command command
= entry
.getKey();
75 String viewerName
= entry
.getValue();
77 MenuItem menuItem
= new MenuItem(addMenu
, SWT
.NONE
);
78 menuItem
.setText(viewerName
);
79 menuItem
.addSelectionListener(new CommandInvoker(command
, firstElement
)) ;
88 return contributionItems
;
91 private final class CommandInvoker
extends SelectionAdapter
{
92 private final Command command
;
93 private Object selectedObject
;
95 private CommandInvoker(Command command
, Object selectedObject
) {
96 this.command
= command
;
97 this.selectedObject
= selectedObject
;
101 public void widgetSelected(SelectionEvent e
) {
102 IHandlerService handlerService
= PlatformUI
.getWorkbench().getService(IHandlerService
.class);
103 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
104 //for generic UuidAndTitleCache objects try to load the object
105 if (selectedObject
instanceof UuidAndTitleCache
){
106 selectedObject
= CdmStore
.getCommonService().find(CdmBase
.class, ((UuidAndTitleCache
)selectedObject
).getUuid());
108 //for tree nodes get the value resp. the object of the node
109 else if (selectedObject
instanceof TreeNode
){
110 selectedObject
= ((TreeNode
) selectedObject
).getValue();
112 if (selectedObject
instanceof TaxonNode
&& !((TaxonNode
)selectedObject
).hasTaxon()){
113 selectedObject
= ((TaxonNode
)selectedObject
).getClassification();
115 if(selectedObject
instanceof ICdmBase
){
116 params
.put(command
.getId()+".uuid", ((ICdmBase
) selectedObject
).getUuid()); //$NON-NLS-1$
118 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
120 if(parameterizedCommand
!=null){
121 handlerService
.executeCommand(parameterizedCommand
, null);
124 handlerService
.executeCommand(command
.getId(), null);
126 } catch (NotDefinedException nde
) {
127 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
128 } catch (Exception exception
) {
129 MessagingUtils
.error(getClass(), "An exception occured while trying to execute "+command
.getId(), exception
); //$NON-NLS-1$