package eu.etaxonomy.taxeditor.view;
+import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.UUID;
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ParameterizedCommand;
+import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.CompoundContributionItem;
+import org.eclipse.ui.handlers.IHandlerService;
+
+import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.taxeditor.Messages;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
/**
* Generic context menu for opening elements in the taxeditor.
new ContributionItem() {
@Override
public void fill(Menu menu, int index) {
- MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
- addItem.setText("Open in...");
- Menu addMenu = new Menu(menu);
- addItem.setMenu(addMenu);
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final ISelection selection = window.getActivePage().getSelection();
if(selection instanceof IStructuredSelection){
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
- Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap = CdmViewerUtil.getNameViewerMap(firstElement);
- for(Entry<Entry<Class<?>, String>, ICdmViewer> entry:nameViewerMap.entrySet()){
- Entry<Class<?>, String> viewerClass = entry.getKey();
- MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
- menuItem.setText(viewerClass.getValue());
- menuItem.addSelectionListener(new OpenInViewerListener(entry.getValue(), firstElement, viewerClass.getKey()));
+ Map<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(firstElement);
+
+ //check if only one or multiple viewers/commands are available
+ if(enabledCommands.size()==1){
+ Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
+ final Command command = entry.getKey();
+ String viewerName = entry.getValue();
+
+ MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
+ addItem.setText(String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
+ addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
+ }
+ else if(enabledCommands.size()>1){
+ MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
+ addItem.setText(Messages.CdmViewerContextMenu_OPEN_IN);
+ Menu addMenu = new Menu(menu);
+ addItem.setMenu(addMenu);
+ for(Entry<Command, String> entry:enabledCommands.entrySet()){
+ final Command command = entry.getKey();
+ String viewerName = entry.getValue();
+
+ MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
+ menuItem.setText(viewerName);
+ menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
+ }
}
}
}
+
}
};
return contributionItems;
}
- private class OpenInViewerListener extends SelectionAdapter {
-
- private final ICdmViewer cdmViewer;
- private final Object input;
- private final Class<?> viewerClass;
+ private final class CommandInvoker extends SelectionAdapter {
+ private final Command command;
+ private final Object selectedObject;
- public OpenInViewerListener(ICdmViewer cdmViewer, Object input, Class<?> viewerClass) {
- super();
- this.cdmViewer = cdmViewer;
- this.input = input;
- this.viewerClass = viewerClass;
+ private CommandInvoker(Command command, Object selectedObject) {
+ this.command = command;
+ this.selectedObject = selectedObject;
}
@Override
public void widgetSelected(SelectionEvent e) {
- cdmViewer.show(input, viewerClass);
+ IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
+ Map<String, UUID> params = new HashMap<String, UUID>();
+ if(selectedObject instanceof ICdmBase){
+ params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid()); //$NON-NLS-1$
+ }
+ ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
+ try {
+ if(parameterizedCommand!=null){
+ handlerService.executeCommand(parameterizedCommand, null);
+ }
+ else{
+ handlerService.executeCommand(command.getId(), null);
+ }
+ } catch (NotDefinedException nde) {
+ throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
+ } catch (Exception exception) {
+ MessagingUtils.error(getClass(), "An exception occured while trying execute "+command.getId(), exception); //$NON-NLS-1$
+ }
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
}
-
}
}