tmf: Move the tmftracetype extension point to tmf.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / SelectTraceTypeContributionItem.java
CommitLineData
12c155f5 1/*******************************************************************************
c8422608 2 * Copyright (c) 2011, 2013 Ericsson
abbdd66a 3 *
12c155f5
FC
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
abbdd66a 8 *
12c155f5
FC
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
d34665f9 13package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
12c155f5
FC
14
15import java.util.HashMap;
16import java.util.HashSet;
17import java.util.LinkedList;
9fa32496 18import java.util.List;
12c155f5 19import java.util.Map;
9fa32496 20import java.util.Set;
12c155f5
FC
21
22import org.eclipse.core.runtime.IConfigurationElement;
23import org.eclipse.core.runtime.Platform;
24import org.eclipse.jface.action.IContributionItem;
25import org.eclipse.jface.action.MenuManager;
26import org.eclipse.jface.resource.ImageDescriptor;
27import org.eclipse.jface.viewers.ISelection;
28import org.eclipse.jface.viewers.StructuredSelection;
8fd82db5 29import org.eclipse.linuxtools.internal.tmf.ui.Activator;
47aafe74
AM
30import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace;
31import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTraceDefinition;
32import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace;
33import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition;
34import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
12c155f5 35import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
a926c25c 36import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils;
12c155f5
FC
37import org.eclipse.ui.IWorkbenchPage;
38import org.eclipse.ui.IWorkbenchWindow;
39import org.eclipse.ui.PlatformUI;
40import org.eclipse.ui.actions.CompoundContributionItem;
41import org.eclipse.ui.menus.CommandContributionItem;
42import org.eclipse.ui.menus.CommandContributionItemParameter;
43
a0a88f65
AM
44/**
45 * ContributionItem for the trace type selection.
46 *
47 * @author Patrick Tassé
48 */
12c155f5
FC
49public class SelectTraceTypeContributionItem extends CompoundContributionItem {
50
51 //private static final ImageDescriptor SELECTED_ICON = ImageDescriptor.createFromImage(TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/bullet.gif")); //$NON-NLS-1$
8fd82db5 52 private static final ImageDescriptor SELECTED_ICON = Activator.getDefault().getImageDescripterFromPath(
12c155f5 53 "icons/elcl16/bullet.gif"); //$NON-NLS-1$
37d150dd
FC
54 private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$
55 private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$
56 private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$
57 private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.select_trace_type"; //$NON-NLS-1$
4bf17f4a 58 private static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$
59 private static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
60 private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
12c155f5
FC
61
62 @Override
63 protected IContributionItem[] getContributionItems() {
587660fc 64
507b1336 65 Set<String> selectedTraceTypes = new HashSet<>();
587660fc
PT
66 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
67 IWorkbenchPage page = window.getActivePage();
68 ISelection selection = page.getSelection();
69 if (selection instanceof StructuredSelection) {
70 for (Object element : ((StructuredSelection) selection).toList()) {
71 if (element instanceof TmfTraceElement) {
72 TmfTraceElement trace = (TmfTraceElement) element;
73 selectedTraceTypes.add(trace.getTraceType());
74 }
75 }
76 }
77
507b1336 78 List<IContributionItem> list = new LinkedList<>();
12c155f5 79
507b1336 80 Map<String, MenuManager> categoriesMap = new HashMap<>();
12c155f5 81 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
4bf17f4a 82 TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 83 for (IConfigurationElement ce : config) {
4bf17f4a 84 if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) {
587660fc
PT
85 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
86 ImageDescriptor icon = isSelectedCategory(categoryId, config, selectedTraceTypes) ? SELECTED_ICON : null;
87 MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR), icon, null);
88 categoriesMap.put(categoryId, subMenu);
12c155f5
FC
89 list.add(subMenu);
90 }
91 }
587660fc
PT
92 CustomTxtTraceDefinition[] customTxtTraceDefinitions = CustomTxtTraceDefinition.loadAll();
93 if (customTxtTraceDefinitions.length > 0) {
94 ImageDescriptor icon = isSelectedCategory(customTxtTraceDefinitions, selectedTraceTypes) ? SELECTED_ICON : null;
95 MenuManager subMenu = new MenuManager(CUSTOM_TXT_CATEGORY, icon, null);
4bf17f4a 96 categoriesMap.put(CUSTOM_TXT_CATEGORY, subMenu);
97 list.add(subMenu);
98 }
587660fc
PT
99 CustomXmlTraceDefinition[] customXmlTraceDefinitions = CustomXmlTraceDefinition.loadAll();
100 if (customXmlTraceDefinitions.length > 0) {
101 ImageDescriptor icon = isSelectedCategory(customXmlTraceDefinitions, selectedTraceTypes) ? SELECTED_ICON : null;
102 MenuManager subMenu = new MenuManager(CUSTOM_XML_CATEGORY, icon, null);
4bf17f4a 103 categoriesMap.put(CUSTOM_XML_CATEGORY, subMenu);
104 list.add(subMenu);
105 }
12c155f5 106
12c155f5 107 for (IConfigurationElement ce : config) {
4bf17f4a 108 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
12c155f5 109 String traceBundle = ce.getContributor().getName();
4bf17f4a 110 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
4bf17f4a 111 String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
112 boolean selected = selectedTraceTypes.contains(traceTypeId);
113 MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR));
12c155f5 114
a926c25c
AM
115 /* Get the icon from the tmftracetypeui extension, if it exists */
116 String traceIcon = null;
117 IConfigurationElement uiCE = TmfTraceTypeUIUtils.getTraceUIAttributes(traceTypeId);
118 if (uiCE != null) {
119 traceIcon = uiCE.getAttribute(TmfTraceTypeUIUtils.ICON_ATTR);
120 }
121
4bf17f4a 122 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
12c155f5
FC
123 }
124 }
125
4bf17f4a 126 // add the custom trace types
587660fc 127 for (CustomTxtTraceDefinition def : customTxtTraceDefinitions) {
8fd82db5 128 String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
4bf17f4a 129 String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
130 String traceIcon = DEFAULT_TRACE_ICON_PATH;
131 String label = def.definitionName;
132 boolean selected = selectedTraceTypes.contains(traceTypeId);
133 MenuManager subMenu = categoriesMap.get(CUSTOM_TXT_CATEGORY);
134
135 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
136 }
587660fc 137 for (CustomXmlTraceDefinition def : customXmlTraceDefinitions) {
8fd82db5 138 String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
4bf17f4a 139 String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
140 String traceIcon = DEFAULT_TRACE_ICON_PATH;
141 String label = def.definitionName;
142 boolean selected = selectedTraceTypes.contains(traceTypeId);
143 MenuManager subMenu = categoriesMap.get(CUSTOM_XML_CATEGORY);
144
145 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
146 }
147
12c155f5
FC
148 return list.toArray(new IContributionItem[list.size()]);
149 }
150
abbdd66a
AM
151 private static void addContributionItem(List<IContributionItem> list,
152 String traceBundle, String traceTypeId, String traceIcon,
153 String label, boolean selected,
4bf17f4a 154 MenuManager subMenu) {
155 Map<String, String> params;
abbdd66a 156
507b1336 157 params = new HashMap<>();
4bf17f4a 158 params.put(BUNDLE_PARAMETER, traceBundle);
159 params.put(TYPE_PARAMETER, traceTypeId);
160 params.put(ICON_PARAMETER, traceIcon);
161
162 ImageDescriptor icon = null;
163 if (selected) {
164 icon = SELECTED_ICON;
165 }
166
828e5592
PT
167 CommandContributionItemParameter param = new CommandContributionItemParameter(
168 PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator
169 "my.parameterid", // id //$NON-NLS-1$
4bf17f4a 170 SELECT_TRACE_TYPE_COMMAND_ID, // commandId
828e5592 171 CommandContributionItem.STYLE_PUSH // style
4bf17f4a 172 );
828e5592
PT
173 param.parameters = params;
174 param.icon = icon;
175 param.disabledIcon = icon;
176 param.hoverIcon = icon;
177 param.label = label;
178 param.visibleEnabled = true;
4bf17f4a 179
180 if (subMenu != null) {
181 subMenu.add(new CommandContributionItem(param));
182 } else {
183 list.add(new CommandContributionItem(param));
184 }
185 }
587660fc
PT
186
187 private static boolean isSelectedCategory(String categoryId, IConfigurationElement[] config, Set<String> selectedTraceTypes) {
188 for (IConfigurationElement ce : config) {
189 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
190 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
191 if (selectedTraceTypes.contains(traceTypeId)) {
192 if (categoryId.equals(ce.getAttribute(TmfTraceType.CATEGORY_ATTR))) {
193 return true;
194 }
195 }
196 }
197 }
198 return false;
199 }
200
201 private static boolean isSelectedCategory(CustomTxtTraceDefinition[] customTxtTraceDefinitions, Set<String> selectedTraceTypes) {
202 for (CustomTxtTraceDefinition def : customTxtTraceDefinitions) {
203 String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
204 if (selectedTraceTypes.contains(traceTypeId)) {
205 return true;
206 }
207 }
208 return false;
209 }
210
211 private static boolean isSelectedCategory(CustomXmlTraceDefinition[] customXmlTraceDefinitions, Set<String> selectedTraceTypes) {
212 for (CustomXmlTraceDefinition def : customXmlTraceDefinitions) {
213 String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
214 if (selectedTraceTypes.contains(traceTypeId)) {
215 return true;
216 }
217 }
218 return false;
219 }
12c155f5 220}
This page took 0.054679 seconds and 5 git commands to generate.