Internalize some more TMF APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / SelectTraceTypeContributionItem.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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.internal.tmf.ui.project.handlers;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.LinkedList;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.Platform;
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.internal.tmf.core.util.TmfTraceType;
28 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
29 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
30 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
31 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
32 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.actions.CompoundContributionItem;
38 import org.eclipse.ui.menus.CommandContributionItem;
39 import org.eclipse.ui.menus.CommandContributionItemParameter;
40
41 public class SelectTraceTypeContributionItem extends CompoundContributionItem {
42
43 //private static final ImageDescriptor SELECTED_ICON = ImageDescriptor.createFromImage(TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/bullet.gif")); //$NON-NLS-1$
44 private static final ImageDescriptor SELECTED_ICON = TmfUiPlugin.getDefault().getImageDescripterFromPath(
45 "icons/elcl16/bullet.gif"); //$NON-NLS-1$
46 private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.bundle"; //$NON-NLS-1$
47 private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.type"; //$NON-NLS-1$
48 private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.icon"; //$NON-NLS-1$
49 private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.trace.select_trace_type"; //$NON-NLS-1$
50 private static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$
51 private static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
52 private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
53
54 @Override
55 protected IContributionItem[] getContributionItems() {
56 LinkedList<IContributionItem> list = new LinkedList<IContributionItem>();
57
58 HashMap<String, MenuManager> categoriesMap = new HashMap<String, MenuManager>();
59 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
60 TmfTraceType.TMF_TRACE_TYPE_ID);
61 for (IConfigurationElement ce : config) {
62 if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) {
63 MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR));
64 categoriesMap.put(ce.getAttribute(TmfTraceType.ID_ATTR), subMenu);
65 list.add(subMenu);
66 }
67 }
68 if (CustomTxtTraceDefinition.loadAll().length > 0) {
69 MenuManager subMenu = new MenuManager(CUSTOM_TXT_CATEGORY);
70 categoriesMap.put(CUSTOM_TXT_CATEGORY, subMenu);
71 list.add(subMenu);
72 }
73 if (CustomXmlTraceDefinition.loadAll().length > 0) {
74 MenuManager subMenu = new MenuManager(CUSTOM_XML_CATEGORY);
75 categoriesMap.put(CUSTOM_XML_CATEGORY, subMenu);
76 list.add(subMenu);
77 }
78
79 HashSet<String> selectedTraceTypes = new HashSet<String>();
80 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
81 IWorkbenchPage page = window.getActivePage();
82 ISelection selection = page.getSelection();
83 if (selection instanceof StructuredSelection) {
84 for (Object element : ((StructuredSelection) selection).toList()) {
85 if (element instanceof TmfTraceElement) {
86 TmfTraceElement trace = (TmfTraceElement) element;
87 selectedTraceTypes.add(trace.getTraceType());
88 }
89 }
90 }
91
92 for (IConfigurationElement ce : config) {
93 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
94 String traceBundle = ce.getContributor().getName();
95 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
96 String traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR);
97 String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
98 boolean selected = selectedTraceTypes.contains(traceTypeId);
99 MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR));
100
101 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
102 }
103 }
104
105 // add the custom trace types
106 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
107 String traceBundle = TmfUiPlugin.getDefault().getBundle().getSymbolicName();
108 String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
109 String traceIcon = DEFAULT_TRACE_ICON_PATH;
110 String label = def.definitionName;
111 boolean selected = selectedTraceTypes.contains(traceTypeId);
112 MenuManager subMenu = categoriesMap.get(CUSTOM_TXT_CATEGORY);
113
114 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
115 }
116 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
117 String traceBundle = TmfUiPlugin.getDefault().getBundle().getSymbolicName();
118 String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
119 String traceIcon = DEFAULT_TRACE_ICON_PATH;
120 String label = def.definitionName;
121 boolean selected = selectedTraceTypes.contains(traceTypeId);
122 MenuManager subMenu = categoriesMap.get(CUSTOM_XML_CATEGORY);
123
124 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
125 }
126
127 return list.toArray(new IContributionItem[list.size()]);
128 }
129
130 private void addContributionItem(LinkedList<IContributionItem> list,
131 String traceBundle, String traceTypeId, String traceIcon, String label, boolean selected,
132 MenuManager subMenu) {
133 Map<String, String> params;
134
135 params = new HashMap<String, String>();
136 params.put(BUNDLE_PARAMETER, traceBundle);
137 params.put(TYPE_PARAMETER, traceTypeId);
138 params.put(ICON_PARAMETER, traceIcon);
139
140 ImageDescriptor icon = null;
141 if (selected) {
142 icon = SELECTED_ICON;
143 }
144
145 CommandContributionItemParameter param = new CommandContributionItemParameter(
146 PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator
147 "my.parameterid", // id //$NON-NLS-1$
148 SELECT_TRACE_TYPE_COMMAND_ID, // commandId
149 CommandContributionItem.STYLE_PUSH // style
150 );
151 param.parameters = params;
152 param.icon = icon;
153 param.disabledIcon = icon;
154 param.hoverIcon = icon;
155 param.label = label;
156 param.visibleEnabled = true;
157
158 if (subMenu != null) {
159 subMenu.add(new CommandContributionItem(param));
160 } else {
161 list.add(new CommandContributionItem(param));
162 }
163 }
164 }
This page took 0.033561 seconds and 5 git commands to generate.