1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Moved SelectTraceTypeContributionItem to this class
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.handlers
;
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
;
23 import java
.util
.Map
.Entry
;
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
;
51 * ContributionItem for the element type selection.
53 * @author Patrick Tassé
55 public class SelectElementTypeContributionItem
extends CompoundContributionItem
{
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$
65 protected IContributionItem
[] getContributionItems() {
68 * Fill the selected trace types and verify if selection applies only to
69 * either traces or experiments
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());
82 } else if (element
instanceof TmfExperimentElement
) {
83 TmfExperimentElement exp
= (TmfExperimentElement
) element
;
84 selectedTraceTypes
.add(exp
.getTraceType());
85 forExperiments
= true;
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$
95 return getContributionItems(selectedTraceTypes
, forExperiments
);
99 * Get the contribution items for traces
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
107 * @return The list of contribution items
109 protected IContributionItem
[] getContributionItems(Set
<String
> selectedTraceTypes
, boolean forExperiments
) {
111 String ceType
= forExperiments ? TmfTraceType
.EXPERIMENT_ELEM
: TmfTraceType
.TYPE_ELEM
;
112 TraceElementType elementType
= forExperiments ? TraceElementType
.EXPERIMENT
: TraceElementType
.TRACE
;
114 List
<IContributionItem
> list
= new LinkedList
<>();
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
);
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
));
137 /* Get the icon from the tmftracetypeui extension, if it exists */
138 String traceIcon
= null;
139 IConfigurationElement uiCE
= TmfTraceTypeUIUtils
.getTraceUIAttributes(traceTypeId
, elementType
);
141 traceIcon
= uiCE
.getAttribute(TmfTraceTypeUIUtils
.ICON_ATTR
);
144 addContributionItem(list
, traceBundle
, traceTypeId
, traceIcon
, label
, selected
, subMenu
);
148 Comparator
<IContributionItem
> comparator
= new Comparator
<IContributionItem
>() {
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());
159 if (o2
instanceof MenuManager
) {
162 CommandContributionItem c1
= (CommandContributionItem
) o1
;
163 CommandContributionItem c2
= (CommandContributionItem
) o2
;
164 return c1
.getData().label
.compareTo(c2
.getData().label
);
168 if (forExperiments
) {
169 Collections
.sort(list
, comparator
);
170 return list
.toArray(new IContributionItem
[list
.size()]);
174 * Add the custom txt and xml trace type to the contribution items for
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
);
185 addContributionItem(list
, traceBundle
, traceTypeId
, traceIcon
, label
, selected
, subMenu
);
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
);
195 addContributionItem(list
, traceBundle
, traceTypeId
, traceIcon
, label
, selected
, subMenu
);
198 Collections
.sort(list
, comparator
);
199 return list
.toArray(new IContributionItem
[list
.size()]);
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
)) {
208 subMenu
.setImageDescriptor(SELECTED_ICON
);
213 ImageDescriptor icon
= selected ? SELECTED_ICON
: null;
214 MenuManager subMenu
= new MenuManager(categoryName
, icon
, null);
215 categoriesMap
.put(categoryName
, subMenu
);
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
;
226 params
= new HashMap
<>();
227 params
.put(BUNDLE_PARAMETER
, traceBundle
);
228 params
.put(TYPE_PARAMETER
, traceTypeId
);
229 params
.put(ICON_PARAMETER
, traceIcon
);
231 ImageDescriptor icon
= null;
233 icon
= SELECTED_ICON
;
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
242 param
.parameters
= params
;
244 param
.disabledIcon
= icon
;
245 param
.hoverIcon
= icon
;
247 param
.visibleEnabled
= true;
249 if (subMenu
!= null) {
250 subMenu
.add(new CommandContributionItem(param
));
252 list
.add(new CommandContributionItem(param
));
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
))) {