analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / handlers / SelectElementTypeContributionItem.java
CommitLineData
12c155f5 1/*******************************************************************************
8a917424 2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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
8a917424 11 * Geneviève Bastien - Moved SelectTraceTypeContributionItem to this class
12c155f5
FC
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.internal.tmf.ui.project.handlers;
12c155f5 15
24b7c980
PT
16import java.util.Collections;
17import java.util.Comparator;
12c155f5
FC
18import java.util.HashMap;
19import java.util.HashSet;
20import java.util.LinkedList;
9fa32496 21import java.util.List;
12c155f5 22import java.util.Map;
4b3b667b 23import java.util.Map.Entry;
9fa32496 24import java.util.Set;
12c155f5
FC
25
26import org.eclipse.core.runtime.IConfigurationElement;
27import org.eclipse.core.runtime.Platform;
28import org.eclipse.jface.action.IContributionItem;
29import org.eclipse.jface.action.MenuManager;
30import org.eclipse.jface.resource.ImageDescriptor;
31import org.eclipse.jface.viewers.ISelection;
32import org.eclipse.jface.viewers.StructuredSelection;
2bdf0193
AM
33import org.eclipse.tracecompass.internal.tmf.ui.Activator;
34import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
35import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
36import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
37import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
38import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
39import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType.TraceElementType;
40import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
41import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
42import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
12c155f5
FC
43import org.eclipse.ui.IWorkbenchPage;
44import org.eclipse.ui.IWorkbenchWindow;
45import org.eclipse.ui.PlatformUI;
46import org.eclipse.ui.actions.CompoundContributionItem;
47import org.eclipse.ui.menus.CommandContributionItem;
48import org.eclipse.ui.menus.CommandContributionItemParameter;
49
a0a88f65 50/**
8a917424 51 * ContributionItem for the element type selection.
a0a88f65
AM
52 *
53 * @author Patrick Tassé
54 */
8a917424 55public class SelectElementTypeContributionItem extends CompoundContributionItem {
12c155f5 56
8a917424 57 private static final ImageDescriptor SELECTED_ICON = Activator.getDefault().getImageDescripterFromPath("icons/elcl16/bullet.gif"); //$NON-NLS-1$
37d150dd
FC
58 private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$
59 private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$
60 private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$
61 private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.select_trace_type"; //$NON-NLS-1$
4bf17f4a 62 private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
12c155f5
FC
63
64 @Override
65 protected IContributionItem[] getContributionItems() {
587660fc 66
8a917424
GB
67 /*
68 * Fill the selected trace types and verify if selection applies only to
69 * either traces or experiments
70 */
507b1336 71 Set<String> selectedTraceTypes = new HashSet<>();
587660fc
PT
72 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
73 IWorkbenchPage page = window.getActivePage();
74 ISelection selection = page.getSelection();
8a917424 75 boolean forTraces = false, forExperiments = false;
587660fc
PT
76 if (selection instanceof StructuredSelection) {
77 for (Object element : ((StructuredSelection) selection).toList()) {
78 if (element instanceof TmfTraceElement) {
79 TmfTraceElement trace = (TmfTraceElement) element;
80 selectedTraceTypes.add(trace.getTraceType());
8a917424
GB
81 forTraces = true;
82 } else if (element instanceof TmfExperimentElement) {
83 TmfExperimentElement exp = (TmfExperimentElement) element;
84 selectedTraceTypes.add(exp.getTraceType());
85 forExperiments = true;
587660fc
PT
86 }
87 }
88 }
89
8a917424
GB
90 if (forTraces && forExperiments) {
91 /* This should never happen anyways */
92 throw new RuntimeException("You must select only experiments or only traces to set the element type"); //$NON-NLS-1$
93 }
94
95 return getContributionItems(selectedTraceTypes, forExperiments);
96 }
97
98 /**
99 * Get the contribution items for traces
100 *
101 * @param selectedTraceTypes
102 * The set of selected trace types
103 * @param forExperiments
104 * <code>true</code> if the contribution items are requested for
105 * experiments, <code>false</code> for traces
106 *
107 * @return The list of contribution items
108 */
109 protected IContributionItem[] getContributionItems(Set<String> selectedTraceTypes, boolean forExperiments) {
110
111 String ceType = forExperiments ? TmfTraceType.EXPERIMENT_ELEM : TmfTraceType.TYPE_ELEM;
50a41a0d 112 TraceElementType elementType = forExperiments ? TraceElementType.EXPERIMENT : TraceElementType.TRACE;
8a917424 113
507b1336 114 List<IContributionItem> list = new LinkedList<>();
12c155f5 115
507b1336 116 Map<String, MenuManager> categoriesMap = new HashMap<>();
12c155f5 117 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
4bf17f4a 118 TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 119 for (IConfigurationElement ce : config) {
4bf17f4a 120 if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) {
587660fc
PT
121 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
122 ImageDescriptor icon = isSelectedCategory(categoryId, config, selectedTraceTypes) ? SELECTED_ICON : null;
123 MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR), icon, null);
124 categoriesMap.put(categoryId, subMenu);
12c155f5
FC
125 list.add(subMenu);
126 }
127 }
128
12c155f5 129 for (IConfigurationElement ce : config) {
8a917424 130 if (ce.getName().equals(ceType)) {
12c155f5 131 String traceBundle = ce.getContributor().getName();
4bf17f4a 132 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
4bf17f4a 133 String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
8a917424 134 boolean selected = selectedTraceTypes.contains(traceTypeId);
4bf17f4a 135 MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR));
12c155f5 136
a926c25c
AM
137 /* Get the icon from the tmftracetypeui extension, if it exists */
138 String traceIcon = null;
50a41a0d 139 IConfigurationElement uiCE = TmfTraceTypeUIUtils.getTraceUIAttributes(traceTypeId, elementType);
a926c25c
AM
140 if (uiCE != null) {
141 traceIcon = uiCE.getAttribute(TmfTraceTypeUIUtils.ICON_ATTR);
142 }
143
4bf17f4a 144 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
12c155f5
FC
145 }
146 }
147
24b7c980
PT
148 Comparator<IContributionItem> comparator = new Comparator<IContributionItem>() {
149 @Override
150 public int compare(IContributionItem o1, IContributionItem o2) {
151 if (o1 instanceof MenuManager) {
152 if (o2 instanceof MenuManager) {
153 MenuManager m1 = (MenuManager) o1;
154 MenuManager m2 = (MenuManager) o2;
155 return m1.getMenuText().compareTo(m2.getMenuText());
156 }
157 return -1;
158 }
159 if (o2 instanceof MenuManager) {
160 return 1;
161 }
162 CommandContributionItem c1 = (CommandContributionItem) o1;
163 CommandContributionItem c2 = (CommandContributionItem) o2;
164 return c1.getData().label.compareTo(c2.getData().label);
165 }
166 };
167
8a917424 168 if (forExperiments) {
24b7c980 169 Collections.sort(list, comparator);
8a917424
GB
170 return list.toArray(new IContributionItem[list.size()]);
171 }
172
173 /*
174 * Add the custom txt and xml trace type to the contribution items for
175 * traces
176 */
4b3b667b 177 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
8fd82db5 178 String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
c9b31f60 179 String traceTypeId = CustomTxtTrace.buildTraceTypeId(def.categoryName, def.definitionName);
4bf17f4a 180 String traceIcon = DEFAULT_TRACE_ICON_PATH;
181 String label = def.definitionName;
182 boolean selected = selectedTraceTypes.contains(traceTypeId);
4b3b667b 183 MenuManager subMenu = getCategorySubMenu(list, categoriesMap, def.categoryName, selected);
4bf17f4a 184
185 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
186 }
4b3b667b 187 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
8fd82db5 188 String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
c9b31f60 189 String traceTypeId = CustomXmlTrace.buildTraceTypeId(def.categoryName, def.definitionName);
4bf17f4a 190 String traceIcon = DEFAULT_TRACE_ICON_PATH;
191 String label = def.definitionName;
192 boolean selected = selectedTraceTypes.contains(traceTypeId);
4b3b667b 193 MenuManager subMenu = getCategorySubMenu(list, categoriesMap, def.categoryName, selected);
4bf17f4a 194
195 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
196 }
197
24b7c980 198 Collections.sort(list, comparator);
12c155f5
FC
199 return list.toArray(new IContributionItem[list.size()]);
200 }
201
4b3b667b
PT
202 private static MenuManager getCategorySubMenu(List<IContributionItem> list,
203 Map<String, MenuManager> categoriesMap, String categoryName, boolean selected) {
204 for (Entry<String, MenuManager> entry : categoriesMap.entrySet()) {
205 MenuManager subMenu = entry.getValue();
206 if (subMenu.getMenuText().equals(categoryName)) {
207 if (selected) {
208 subMenu.setImageDescriptor(SELECTED_ICON);
209 }
210 return subMenu;
211 }
212 }
213 ImageDescriptor icon = selected ? SELECTED_ICON : null;
214 MenuManager subMenu = new MenuManager(categoryName, icon, null);
215 categoriesMap.put(categoryName, subMenu);
216 list.add(subMenu);
217 return subMenu;
218 }
219
abbdd66a
AM
220 private static void addContributionItem(List<IContributionItem> list,
221 String traceBundle, String traceTypeId, String traceIcon,
222 String label, boolean selected,
4bf17f4a 223 MenuManager subMenu) {
224 Map<String, String> params;
abbdd66a 225
507b1336 226 params = new HashMap<>();
4bf17f4a 227 params.put(BUNDLE_PARAMETER, traceBundle);
228 params.put(TYPE_PARAMETER, traceTypeId);
229 params.put(ICON_PARAMETER, traceIcon);
230
231 ImageDescriptor icon = null;
232 if (selected) {
233 icon = SELECTED_ICON;
234 }
235
828e5592
PT
236 CommandContributionItemParameter param = new CommandContributionItemParameter(
237 PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator
238 "my.parameterid", // id //$NON-NLS-1$
4bf17f4a 239 SELECT_TRACE_TYPE_COMMAND_ID, // commandId
828e5592 240 CommandContributionItem.STYLE_PUSH // style
4bf17f4a 241 );
828e5592
PT
242 param.parameters = params;
243 param.icon = icon;
244 param.disabledIcon = icon;
245 param.hoverIcon = icon;
246 param.label = label;
247 param.visibleEnabled = true;
4bf17f4a 248
249 if (subMenu != null) {
250 subMenu.add(new CommandContributionItem(param));
251 } else {
252 list.add(new CommandContributionItem(param));
253 }
254 }
587660fc
PT
255
256 private static boolean isSelectedCategory(String categoryId, IConfigurationElement[] config, Set<String> selectedTraceTypes) {
257 for (IConfigurationElement ce : config) {
258 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
259 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
260 if (selectedTraceTypes.contains(traceTypeId)) {
261 if (categoryId.equals(ce.getAttribute(TmfTraceType.CATEGORY_ATTR))) {
262 return true;
263 }
264 }
265 }
266 }
267 return false;
268 }
12c155f5 269}
This page took 0.093377 seconds and 5 git commands to generate.