ac8eb753c96eb383a75accfb543634d5435d60b0
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / ui / views / timegraph / XmlPresentationProvider.java
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.tracecompass.internal.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.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.core.module.XmlUtils;
28 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
29 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
30 import org.eclipse.tracecompass.internal.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;
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 // Draw state only if state is already known
67 Integer index = stateIndex.get(value);
68 if (index != null) {
69 return index;
70 }
71 }
72 }
73 return INVISIBLE;
74 }
75
76 @Override
77 public StateItem[] getStateTable() {
78 return stateValues.toArray(new StateItem[stateValues.size()]);
79 }
80
81 @Override
82 public String getEventName(ITimeEvent event) {
83 if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) {
84 TimeEvent tcEvent = (TimeEvent) event;
85
86 XmlEntry entry = (XmlEntry) event.getEntry();
87 int value = tcEvent.getValue();
88
89 if (entry.getType() == EntryDisplayType.DISPLAY) {
90 Integer index = stateIndex.get(value);
91 if (index != null) {
92 String rgb = stateValues.get(index.intValue()).getStateString();
93 return rgb;
94 }
95 }
96 return null;
97 }
98 return Messages.XmlPresentationProvider_MultipleStates;
99 }
100
101 @Override
102 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) {
103 /*
104 * TODO: Add the XML elements to support adding extra information in the
105 * tooltips and implement this
106 */
107 return Collections.EMPTY_MAP;
108 }
109
110 @Override
111 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
112 /*
113 * TODO Add the XML elements to support texts in intervals and implement
114 * this
115 */
116 }
117
118 @Override
119 public void postDrawEntry(ITimeGraphEntry entry, Rectangle bounds, GC gc) {
120 }
121
122 /**
123 * Loads the states from a {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} XML
124 * element
125 *
126 * @param viewElement
127 * The XML view element
128 */
129 public void loadNewStates(@NonNull Element viewElement) {
130 stateValues.clear();
131 stateIndex.clear();
132 List<Element> states = XmlUtils.getChildElements(viewElement, TmfXmlStrings.DEFINED_VALUE);
133
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);
138
139 // FIXME Allow this case
140 if (value < 0) {
141 return;
142 }
143
144 final RGB colorRGB = (color.startsWith(TmfXmlStrings.COLOR_PREFIX)) ? parseColor(color) : calcColor(value);
145
146 StateItem item = new StateItem(colorRGB, name);
147
148 Integer index = stateIndex.get(value);
149 if (index == null) {
150 /* Add the new state value */
151 stateIndex.put(value, stateValues.size());
152 stateValues.add(item);
153 } else {
154 /* Override a previous state value */
155 stateValues.set(index, item);
156 }
157 }
158 Display.getDefault().asyncExec(new Runnable() {
159 @Override
160 public void run() {
161 fireColorSettingsChanged();
162 }
163 });
164 }
165
166 private static RGB parseColor(String color) {
167 RGB colorRGB;
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);
173 return colorRGB;
174 }
175
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) {
180 r = 255;
181 g = x;
182 b = 0;
183 }
184 if (x >= 255 && x < 510) {
185 r = 510 - x;
186 g = 255;
187 b = 0;
188 }
189 if (x >= 510 && x < 765) {
190 r = 0;
191 g = 255;
192 b = x - 510;
193 }
194 if (x >= 765 && x < 1020) {
195 r = 0;
196 g = 1020 - x;
197 b = 255;
198 }
199 if (x >= 1020 && x < 1275) {
200 r = x - 1020;
201 g = 0;
202 b = 255;
203 }
204 if (x >= 1275 && x <= 1530) {
205 r = 255;
206 g = 0;
207 b = 1530 - x;
208 }
209 return new RGB(r, g, b);
210 }
211
212 }
This page took 0.034307 seconds and 4 git commands to generate.