Project

General

Profile

Download (5.62 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.view;
2

    
3
import java.util.HashMap;
4
import java.util.Map;
5
import java.util.Map.Entry;
6
import java.util.UUID;
7

    
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;
24

    
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;
31

    
32
/**
33
 * Generic context menu for opening elements in the taxeditor.
34
 *
35
 */
36
public class CdmViewerContextMenu extends CompoundContributionItem {
37

    
38
    @Override
39
    protected IContributionItem[] getContributionItems() {
40
        IContributionItem[] contributionItems = new IContributionItem[] {
41
                new ContributionItem() {
42
                    @Override
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);
49

    
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();
55

    
56
                                MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
57
                                addItem.setText(String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
58
                                addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
59
                            }
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();
68

    
69
                                    MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
70
                                    menuItem.setText(viewerName);
71
                                    menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
72
                                }
73
                            }
74
                        }
75
                    }
76

    
77
                }
78
        };
79
        return contributionItems;
80
    }
81

    
82
    private final class CommandInvoker extends SelectionAdapter {
83
        private final Command command;
84
        private Object selectedObject;
85

    
86
        private CommandInvoker(Command command, Object selectedObject) {
87
            this.command = command;
88
            this.selectedObject = selectedObject;
89
        }
90

    
91
        @Override
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());
98
            }
99
            if(selectedObject instanceof ICdmBase){
100
                params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid()); //$NON-NLS-1$
101
            }
102
            ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
103
            try {
104
                if(parameterizedCommand!=null){
105
                    handlerService.executeCommand(parameterizedCommand, null);
106
                }
107
                else{
108
                    handlerService.executeCommand(command.getId(), null);
109
                }
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$
114
            }
115
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
116
        }
117
    }
118

    
119
}
(6-6/7)