Project

General

Profile

Download (5.59 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.IWorkbenchPage;
21
import org.eclipse.ui.IWorkbenchWindow;
22
import org.eclipse.ui.PlatformUI;
23
import org.eclipse.ui.actions.CompoundContributionItem;
24
import org.eclipse.ui.handlers.IHandlerService;
25

    
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.ICdmBase;
28
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
29
import eu.etaxonomy.taxeditor.Messages;
30
import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32

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

    
39
    @Override
40
    protected IContributionItem[] getContributionItems() {
41
        IContributionItem[] contributionItems = new IContributionItem[] {
42
                new ContributionItem() {
43
                    @Override
44
                    public void fill(Menu menu, int index) {
45
                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
46
                        IWorkbenchPage activePage = window.getActivePage();
47
                        if(activePage!=null){
48
                        	final ISelection selection = activePage.getSelection();
49
                        	if(selection instanceof IStructuredSelection){
50
                        		Object firstElement = ((IStructuredSelection) selection).getFirstElement();
51
                        		Map<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(firstElement);
52

    
53
                        		//check if only one or multiple viewers/commands are available
54
                        		if(enabledCommands.size()==1){
55
                        			Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
56
                        			final Command command = entry.getKey();
57
                        			String viewerName = entry.getValue();
58

    
59
                        			MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
60
                        			addItem.setText(String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
61
                        			addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
62
                        		}
63
                        		else if(enabledCommands.size()>1){
64
                        			MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
65
                        			addItem.setText(Messages.CdmViewerContextMenu_OPEN_IN);
66
                        			Menu addMenu = new Menu(menu);
67
                        			addItem.setMenu(addMenu);
68
                        			for(Entry<Command, String> entry:enabledCommands.entrySet()){
69
                        				final Command command = entry.getKey();
70
                        				String viewerName = entry.getValue();
71

    
72
                        				MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
73
                        				menuItem.setText(viewerName);
74
                        				menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
75
                        			}
76
                        		}
77
                        	}
78
                        }
79
                    }
80

    
81
                }
82
        };
83
        return contributionItems;
84
    }
85

    
86
    private final class CommandInvoker extends SelectionAdapter {
87
        private final Command command;
88
        private Object selectedObject;
89

    
90
        private CommandInvoker(Command command, Object selectedObject) {
91
            this.command = command;
92
            this.selectedObject = selectedObject;
93
        }
94

    
95
        @Override
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
                selectedObject = CdmStore.getCommonService().find(CdmBase.class, ((UuidAndTitleCache)selectedObject).getUuid());
102
            }
103
            if(selectedObject instanceof ICdmBase){
104
                params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid()); //$NON-NLS-1$
105
            }
106
            ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
107
            try {
108
                if(parameterizedCommand!=null){
109
                    handlerService.executeCommand(parameterizedCommand, null);
110
                }
111
                else{
112
                    handlerService.executeCommand(command.getId(), null);
113
                }
114
            } catch (NotDefinedException nde) {
115
                throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
116
            } catch (Exception exception) {
117
                MessagingUtils.error(getClass(), "An exception occured while trying to execute "+command.getId(), exception); //$NON-NLS-1$
118
            }
119
        }
120
    }
121

    
122
}
(6-6/7)