1 /*******************************************************************************
2 * Copyright (c) 2014 É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 * Florian Wininger - Initial API and implementation
11 * Geneviève Bastien - Review of the initial implementation
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.views
.timegraph
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.swt
.graphics
.GC
;
24 import org
.eclipse
.swt
.graphics
.RGB
;
25 import org
.eclipse
.swt
.graphics
.Rectangle
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
28 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
29 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
30 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphPresentationProvider
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
36 import org
.w3c
.dom
.Element
;
39 * Presentation provider for the XML view, based on the generic TMF presentation
42 * TODO: This should support colors/states defined for each entry element in the
43 * XML element. Also, event values may not be integers only (for instance, this
44 * wouldn't support yet the callstack view)
46 * @author Florian Wininger
48 public class XmlPresentationProvider
extends TimeGraphPresentationProvider
{
50 private List
<StateItem
> stateValues
= new ArrayList
<>();
52 * Maps the value of an event with the corresponding index in the
55 private Map
<Integer
, Integer
> stateIndex
= new HashMap
<>();
58 public int getStateTableIndex(ITimeEvent event
) {
59 if (event
instanceof TimeEvent
&& ((TimeEvent
) event
).hasValue()) {
60 TimeEvent tcEvent
= (TimeEvent
) event
;
62 XmlEntry entry
= (XmlEntry
) event
.getEntry();
63 int value
= tcEvent
.getValue();
65 if (entry
.getType() == EntryDisplayType
.DISPLAY
) {
66 // Draw state only if state is already known
67 Integer index
= stateIndex
.get(value
);
77 public StateItem
[] getStateTable() {
78 return stateValues
.toArray(new StateItem
[stateValues
.size()]);
82 public String
getEventName(ITimeEvent event
) {
83 if (event
instanceof TimeEvent
&& ((TimeEvent
) event
).hasValue()) {
84 TimeEvent tcEvent
= (TimeEvent
) event
;
86 XmlEntry entry
= (XmlEntry
) event
.getEntry();
87 int value
= tcEvent
.getValue();
89 if (entry
.getType() == EntryDisplayType
.DISPLAY
) {
90 Integer index
= stateIndex
.get(value
);
92 String rgb
= stateValues
.get(index
.intValue()).getStateString();
98 return Messages
.XmlPresentationProvider_MultipleStates
;
102 public Map
<String
, String
> getEventHoverToolTipInfo(ITimeEvent event
, long hoverTime
) {
104 * TODO: Add the XML elements to support adding extra information in the
105 * tooltips and implement this
107 return Collections
.EMPTY_MAP
;
111 public void postDrawEvent(ITimeEvent event
, Rectangle bounds
, GC gc
) {
113 * TODO Add the XML elements to support texts in intervals and implement
119 public void postDrawEntry(ITimeGraphEntry entry
, Rectangle bounds
, GC gc
) {
123 * Loads the states from a {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} XML
127 * The XML view element
129 public void loadNewStates(@NonNull Element viewElement
) {
132 List
<Element
> states
= XmlUtils
.getChildElements(viewElement
, TmfXmlStrings
.DEFINED_VALUE
);
134 for (Element state
: states
) {
135 int value
= Integer
.parseInt(state
.getAttribute(TmfXmlStrings
.VALUE
));
136 String name
= state
.getAttribute(TmfXmlStrings
.NAME
);
137 String color
= state
.getAttribute(TmfXmlStrings
.COLOR
);
139 // FIXME Allow this case
144 final RGB colorRGB
= (color
.startsWith(TmfXmlStrings
.COLOR_PREFIX
)) ?
parseColor(color
) : calcColor(value
);
146 StateItem item
= new StateItem(colorRGB
, name
);
148 Integer index
= stateIndex
.get(value
);
150 /* Add the new state value */
151 stateIndex
.put(value
, stateValues
.size());
152 stateValues
.add(item
);
154 /* Override a previous state value */
155 stateValues
.set(index
, item
);
158 Display
.getDefault().asyncExec(new Runnable() {
161 fireColorSettingsChanged();
166 private static RGB
parseColor(String color
) {
168 Integer hex
= Integer
.parseInt(color
.substring(1), 16);
169 int hex1
= hex
.intValue() % 256;
170 int hex2
= (hex
.intValue() / 256) % 256;
171 int hex3
= (hex
.intValue() / (256 * 256)) % 256;
172 colorRGB
= new RGB(hex3
, hex2
, hex1
);
176 private static RGB
calcColor(int value
) {
177 int x
= (value
* 97) % 1530;
178 int r
= 0, g
= 0, b
= 0;
179 if (x
>= 0 && x
< 255) {
184 if (x
>= 255 && x
< 510) {
189 if (x
>= 510 && x
< 765) {
194 if (x
>= 765 && x
< 1020) {
199 if (x
>= 1020 && x
< 1275) {
204 if (x
>= 1275 && x
<= 1530) {
209 return new RGB(r
, g
, b
);