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
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Moved SelectTraceTypeContributionItem to this class
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.ui.project.handlers;
15
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25
26 import org.eclipse.core.runtime.IConfigurationElement;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.jface.action.IContributionItem;
29 import org.eclipse.jface.action.MenuManager;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
34 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
35 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
36 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
37 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
38 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
39 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType.TraceElementType;
40 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
41 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
42 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
43 import org.eclipse.ui.IWorkbenchPage;
44 import org.eclipse.ui.IWorkbenchWindow;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.actions.CompoundContributionItem;
47 import org.eclipse.ui.menus.CommandContributionItem;
48 import org.eclipse.ui.menus.CommandContributionItemParameter;
49
50 /**
51 * ContributionItem for the element type selection.
52 *
53 * @author Patrick Tassé
54 */
55 public class SelectElementTypeContributionItem extends CompoundContributionItem {
56
57 private static final ImageDescriptor SELECTED_ICON = Activator.getDefault().getImageDescripterFromPath("icons/elcl16/bullet.gif"); //$NON-NLS-1$
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$
62 private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
63
64 @Override
65 protected IContributionItem[] getContributionItems() {
66
67 /*
68 * Fill the selected trace types and verify if selection applies only to
69 * either traces or experiments
70 */
71 Set<String> selectedTraceTypes = new HashSet<>();
72 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
73 IWorkbenchPage page = window.getActivePage();
74 ISelection selection = page.getSelection();
75 boolean forTraces = false, forExperiments = false;
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());
81 forTraces = true;
82 } else if (element instanceof TmfExperimentElement) {
83 TmfExperimentElement exp = (TmfExperimentElement) element;
84 selectedTraceTypes.add(exp.getTraceType());
85 forExperiments = true;
86 }
87 }
88 }
89
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;
112 TraceElementType elementType = forExperiments ? TraceElementType.EXPERIMENT : TraceElementType.TRACE;
113
114 List<IContributionItem> list = new LinkedList<>();
115
116 Map<String, MenuManager> categoriesMap = new HashMap<>();
117 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
118 TmfTraceType.TMF_TRACE_TYPE_ID);
119 for (IConfigurationElement ce : config) {
120 if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) {
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);
125 list.add(subMenu);
126 }
127 }
128
129 for (IConfigurationElement ce : config) {
130 if (ce.getName().equals(ceType)) {
131 String traceBundle = ce.getContributor().getName();
132 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
133 String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
134 boolean selected = selectedTraceTypes.contains(traceTypeId);
135 MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR));
136
137 /* Get the icon from the tmftracetypeui extension, if it exists */
138 String traceIcon = null;
139 IConfigurationElement uiCE = TmfTraceTypeUIUtils.getTraceUIAttributes(traceTypeId, elementType);
140 if (uiCE != null) {
141 traceIcon = uiCE.getAttribute(TmfTraceTypeUIUtils.ICON_ATTR);
142 }
143
144 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
145 }
146 }
147
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
168 if (forExperiments) {
169 Collections.sort(list, comparator);
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 */
177 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
178 String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
179 String traceTypeId = CustomTxtTrace.buildTraceTypeId(def.categoryName, def.definitionName);
180 String traceIcon = DEFAULT_TRACE_ICON_PATH;
181 String label = def.definitionName;
182 boolean selected = selectedTraceTypes.contains(traceTypeId);
183 MenuManager subMenu = getCategorySubMenu(list, categoriesMap, def.categoryName, selected);
184
185 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
186 }
187 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
188 String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
189 String traceTypeId = CustomXmlTrace.buildTraceTypeId(def.categoryName, def.definitionName);
190 String traceIcon = DEFAULT_TRACE_ICON_PATH;
191 String label = def.definitionName;
192 boolean selected = selectedTraceTypes.contains(traceTypeId);
193 MenuManager subMenu = getCategorySubMenu(list, categoriesMap, def.categoryName, selected);
194
195 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
196 }
197
198 Collections.sort(list, comparator);
199 return list.toArray(new IContributionItem[list.size()]);
200 }
201
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
220 private static void addContributionItem(List<IContributionItem> list,
221 String traceBundle, String traceTypeId, String traceIcon,
222 String label, boolean selected,
223 MenuManager subMenu) {
224 Map<String, String> params;
225
226 params = new HashMap<>();
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
236 CommandContributionItemParameter param = new CommandContributionItemParameter(
237 PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator
238 "my.parameterid", // id //$NON-NLS-1$
239 SELECT_TRACE_TYPE_COMMAND_ID, // commandId
240 CommandContributionItem.STYLE_PUSH // style
241 );
242 param.parameters = params;
243 param.icon = icon;
244 param.disabledIcon = icon;
245 param.hoverIcon = icon;
246 param.label = label;
247 param.visibleEnabled = true;
248
249 if (subMenu != null) {
250 subMenu.add(new CommandContributionItem(param));
251 } else {
252 list.add(new CommandContributionItem(param));
253 }
254 }
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 }
269 }
This page took 0.040473 seconds and 6 git commands to generate.