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
.linuxtools
.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
.linuxtools
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
24 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
25 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
26 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphPresentationProvider
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
32 import org
.eclipse
.swt
.graphics
.GC
;
33 import org
.eclipse
.swt
.graphics
.RGB
;
34 import org
.eclipse
.swt
.graphics
.Rectangle
;
35 import org
.eclipse
.swt
.widgets
.Display
;
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 Integer index
= stateIndex
.get(value
);
68 /* Colors won't be refreshed yet, return something known */
70 stateIndex
.put(value
, stateValues
.size());
71 StateItem item
= new StateItem(calcColor(stateValues
.size()), String
.valueOf(value
));
72 stateValues
.add(item
);
73 Display
.getDefault().asyncExec(new Runnable() {
76 fireColorSettingsChanged();
88 public StateItem
[] getStateTable() {
89 return stateValues
.toArray(new StateItem
[stateValues
.size()]);
93 public String
getEventName(ITimeEvent event
) {
94 if (event
instanceof TimeEvent
&& ((TimeEvent
) event
).hasValue()) {
95 TimeEvent tcEvent
= (TimeEvent
) event
;
97 XmlEntry entry
= (XmlEntry
) event
.getEntry();
98 int value
= tcEvent
.getValue();
100 if (entry
.getType() == EntryDisplayType
.DISPLAY
) {
101 Integer index
= stateIndex
.get(value
);
102 String rgb
= stateValues
.get(index
).getStateString();
107 return Messages
.XmlPresentationProvider_MultipleStates
;
111 public Map
<String
, String
> getEventHoverToolTipInfo(ITimeEvent event
, long hoverTime
) {
113 * TODO: Add the XML elements to support adding extra information in the
114 * tooltips and implement this
116 return Collections
.EMPTY_MAP
;
120 public void postDrawEvent(ITimeEvent event
, Rectangle bounds
, GC gc
) {
122 * TODO Add the XML elements to support texts in intervals and implement
128 public void postDrawEntry(ITimeGraphEntry entry
, Rectangle bounds
, GC gc
) {
132 * Loads the states from a {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} XML
136 * The XML view element
138 public void loadNewStates(@NonNull Element viewElement
) {
141 List
<Element
> states
= XmlUtils
.getChildElements(viewElement
, TmfXmlStrings
.DEFINED_VALUE
);
143 for (Element state
: states
) {
144 int value
= Integer
.parseInt(state
.getAttribute(TmfXmlStrings
.VALUE
));
145 String name
= state
.getAttribute(TmfXmlStrings
.NAME
);
146 String color
= state
.getAttribute(TmfXmlStrings
.COLOR
);
148 // FIXME Allow this case
153 RGB colorRGB
= new RGB(255, 0, 0);
154 if (color
.startsWith(TmfXmlStrings
.COLOR_PREFIX
)) {
155 Integer hex
= Integer
.parseInt(color
.substring(1), 16);
156 int hex1
= hex
.intValue() % 256;
157 int hex2
= (hex
.intValue() / 256) % 256;
158 int hex3
= (hex
.intValue() / (256 * 256)) % 256;
159 colorRGB
= new RGB(hex3
, hex2
, hex1
);
161 colorRGB
= calcColor(value
);
164 StateItem item
= new StateItem(colorRGB
, name
);
166 Integer index
= stateIndex
.get(value
);
168 /* Add the new state value */
169 stateIndex
.put(value
, stateValues
.size());
170 stateValues
.add(item
);
172 /* Override a previous state value */
173 stateValues
.set(index
, item
);
176 Display
.getDefault().asyncExec(new Runnable() {
179 fireColorSettingsChanged();
184 private static RGB
calcColor(int value
) {
185 int x
= (value
* 97) % 1530;
186 int r
= 0, g
= 0, b
= 0;
187 if (x
>= 0 && x
< 255) {
192 if (x
>= 255 && x
< 510) {
197 if (x
>= 510 && x
< 765) {
202 if (x
>= 765 && x
< 1020) {
207 if (x
>= 1020 && x
< 1275) {
212 if (x
>= 1275 && x
<= 1530) {
217 return new RGB(r
, g
, b
);