Added TMF statistics feature (Bug 360572)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / project / handlers / SelectParserContributionItem.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.project.handlers;
14
15 import java.util.HashMap;
16 import java.util.LinkedList;
17 import java.util.Map;
18 import java.util.Map.Entry;
19
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.action.IContributionItem;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
28 import org.eclipse.linuxtools.tmf.ui.parsers.ParserProviderManager;
29 import org.eclipse.linuxtools.tmf.ui.views.project.ProjectView;
30 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfExperimentNode;
31 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfTraceNode;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.actions.CompoundContributionItem;
36 import org.eclipse.ui.menus.CommandContributionItem;
37 import org.eclipse.ui.menus.CommandContributionItemParameter;
38
39
40 public class SelectParserContributionItem extends CompoundContributionItem {
41
42 private static final ImageDescriptor SELECTED_ICON = ImageDescriptor.createFromImage(TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/bullet.gif")); //$NON-NLS-1$
43
44 @Override
45 protected IContributionItem[] getContributionItems() {
46 Map<String, String> params;
47 LinkedList<IContributionItem> list = new LinkedList<IContributionItem>();
48
49 ParserProviderManager.getParserMap();
50
51 String parser = null;
52 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
53 IWorkbenchPage page = window.getActivePage();
54 ISelection selection = page.getSelection(ProjectView.ID);
55 if (selection instanceof StructuredSelection) {
56 Object element = ((StructuredSelection) selection).getFirstElement();
57 if (! (element instanceof TmfTraceNode)) {
58 return new IContributionItem[0];
59 }
60 TmfTraceNode trace = (TmfTraceNode) element;
61 IResource resource = trace.getResource();
62 if (trace.getParent() instanceof TmfExperimentNode) {
63 resource = trace.getProject().getTracesFolder().getTraceForLocation(resource.getLocation()).getResource();
64 }
65 try {
66 parser = resource.getPersistentProperty(ParserProviderManager.PARSER_PROPERTY);
67 } catch (CoreException e) {
68 e.printStackTrace();
69 }
70 }
71
72 for(Entry<String, Map<String, String>> providerEntry : ParserProviderManager.getParserMap().entrySet()) {
73 MenuManager subMenu = new MenuManager(providerEntry.getKey());
74 for(Entry<String, String> entry : providerEntry.getValue().entrySet()) {
75 params = new HashMap<String, String>();
76 params.put("org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.selectparser.parser", entry.getValue()); //$NON-NLS-1$
77
78 ImageDescriptor icon = null;
79 if (entry.getValue().equals(parser)) {
80 icon = SELECTED_ICON;
81 }
82
83 CommandContributionItemParameter param = new CommandContributionItemParameter(PlatformUI.getWorkbench().getActiveWorkbenchWindow(),
84 "my.parameterid", //$NON-NLS-1$
85 "org.eclipse.linuxtools.tmf.ui.command.project.trace.selectparser", //$NON-NLS-1$
86 params,
87 icon, // icon
88 null, // disabled icon
89 null, // hover icon
90 entry.getKey().replaceAll("&", "&&"), // label //$NON-NLS-1$//$NON-NLS-2$
91 null, // mnemonic
92 null, // tooltip
93 CommandContributionItem.STYLE_PUSH,
94 null, // help context id
95 true // visibleEnable
96 );
97
98 subMenu.add(new CommandContributionItem(param));
99 }
100 list.add(subMenu);
101 }
102
103 return (IContributionItem[]) list.toArray(new IContributionItem[list.size()]);
104 }
105
106 }
This page took 0.031905 seconds and 5 git commands to generate.