Commit | Line | Data |
---|---|---|
1a23419e FW |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 É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 | * Florian Wininger - Initial API and implementation | |
11 | * Geneviève Bastien - Review of the initial implementation | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.Collections; | |
18 | import java.util.HashMap; | |
19 | import java.util.List; | |
20 | import java.util.Map; | |
21 | ||
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; | |
37 | ||
38 | /** | |
39 | * Presentation provider for the XML view, based on the generic TMF presentation | |
40 | * provider. | |
41 | * | |
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) | |
45 | * | |
46 | * @author Florian Wininger | |
47 | */ | |
48 | public class XmlPresentationProvider extends TimeGraphPresentationProvider { | |
49 | ||
50 | private List<StateItem> stateValues = new ArrayList<>(); | |
51 | /* | |
52 | * Maps the value of an event with the corresponding index in the | |
53 | * stateValues list | |
54 | */ | |
55 | private Map<Integer, Integer> stateIndex = new HashMap<>(); | |
56 | ||
57 | @Override | |
58 | public int getStateTableIndex(ITimeEvent event) { | |
59 | if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) { | |
60 | TimeEvent tcEvent = (TimeEvent) event; | |
61 | ||
62 | XmlEntry entry = (XmlEntry) event.getEntry(); | |
63 | int value = tcEvent.getValue(); | |
64 | ||
65 | if (entry.getType() == EntryDisplayType.DISPLAY) { | |
66 | Integer index = stateIndex.get(value); | |
67 | if (index == null) { | |
68 | /* Colors won't be refreshed yet, return something known */ | |
69 | index = TRANSPARENT; | |
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() { | |
74 | @Override | |
75 | public void run() { | |
76 | fireColorSettingsChanged(); | |
77 | } | |
78 | }); | |
79 | } | |
80 | return index; | |
81 | } | |
82 | } | |
83 | ||
84 | return INVISIBLE; | |
85 | } | |
86 | ||
87 | @Override | |
88 | public StateItem[] getStateTable() { | |
89 | return stateValues.toArray(new StateItem[stateValues.size()]); | |
90 | } | |
91 | ||
92 | @Override | |
93 | public String getEventName(ITimeEvent event) { | |
94 | if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) { | |
95 | TimeEvent tcEvent = (TimeEvent) event; | |
96 | ||
97 | XmlEntry entry = (XmlEntry) event.getEntry(); | |
98 | int value = tcEvent.getValue(); | |
99 | ||
100 | if (entry.getType() == EntryDisplayType.DISPLAY) { | |
101 | Integer index = stateIndex.get(value); | |
102 | String rgb = stateValues.get(index).getStateString(); | |
103 | return rgb; | |
104 | } | |
105 | return null; | |
106 | } | |
107 | return Messages.XmlPresentationProvider_MultipleStates; | |
108 | } | |
109 | ||
110 | @Override | |
111 | public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) { | |
112 | /* | |
113 | * TODO: Add the XML elements to support adding extra information in the | |
114 | * tooltips and implement this | |
115 | */ | |
116 | return Collections.EMPTY_MAP; | |
117 | } | |
118 | ||
119 | @Override | |
120 | public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) { | |
121 | /* | |
122 | * TODO Add the XML elements to support texts in intervals and implement | |
123 | * this | |
124 | */ | |
125 | } | |
126 | ||
127 | @Override | |
128 | public void postDrawEntry(ITimeGraphEntry entry, Rectangle bounds, GC gc) { | |
129 | } | |
130 | ||
131 | /** | |
132 | * Loads the states from a {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} XML | |
133 | * element | |
134 | * | |
135 | * @param viewElement | |
136 | * The XML view element | |
137 | */ | |
138 | public void loadNewStates(@NonNull Element viewElement) { | |
139 | stateValues.clear(); | |
140 | stateIndex.clear(); | |
141 | List<Element> states = XmlUtils.getChildElements(viewElement, TmfXmlStrings.DEFINED_VALUE); | |
142 | ||
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); | |
147 | ||
148 | // FIXME Allow this case | |
149 | if (value < 0) { | |
150 | return; | |
151 | } | |
152 | ||
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); | |
160 | } else { | |
161 | colorRGB = calcColor(value); | |
162 | } | |
163 | ||
164 | StateItem item = new StateItem(colorRGB, name); | |
165 | ||
166 | Integer index = stateIndex.get(value); | |
167 | if (index == null) { | |
168 | /* Add the new state value */ | |
169 | stateIndex.put(value, stateValues.size()); | |
170 | stateValues.add(item); | |
171 | } else { | |
172 | /* Override a previous state value */ | |
173 | stateValues.set(index, item); | |
174 | } | |
175 | } | |
176 | Display.getDefault().asyncExec(new Runnable() { | |
177 | @Override | |
178 | public void run() { | |
179 | fireColorSettingsChanged(); | |
180 | } | |
181 | }); | |
182 | } | |
183 | ||
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) { | |
188 | r = 255; | |
189 | g = x; | |
190 | b = 0; | |
191 | } | |
192 | if (x >= 255 && x < 510) { | |
193 | r = 510 - x; | |
194 | g = 255; | |
195 | b = 0; | |
196 | } | |
197 | if (x >= 510 && x < 765) { | |
198 | r = 0; | |
199 | g = 255; | |
200 | b = x - 510; | |
201 | } | |
202 | if (x >= 765 && x < 1020) { | |
203 | r = 0; | |
204 | g = 1020 - x; | |
205 | b = 255; | |
206 | } | |
207 | if (x >= 1020 && x < 1275) { | |
208 | r = x - 1020; | |
209 | g = 0; | |
210 | b = 255; | |
211 | } | |
212 | if (x >= 1275 && x <= 1530) { | |
213 | r = 255; | |
214 | g = 0; | |
215 | b = 1530 - x; | |
216 | } | |
217 | return new RGB(r, g, b); | |
218 | } | |
219 | ||
220 | } |