tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / 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.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.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;
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 String rgb = stateValues.get(index).getStateString();
92 return rgb;
93 }
94 return null;
95 }
96 return Messages.XmlPresentationProvider_MultipleStates;
97 }
98
99 @Override
100 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) {
101 /*
102 * TODO: Add the XML elements to support adding extra information in the
103 * tooltips and implement this
104 */
105 return Collections.EMPTY_MAP;
106 }
107
108 @Override
109 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
110 /*
111 * TODO Add the XML elements to support texts in intervals and implement
112 * this
113 */
114 }
115
116 @Override
117 public void postDrawEntry(ITimeGraphEntry entry, Rectangle bounds, GC gc) {
118 }
119
120 /**
121 * Loads the states from a {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} XML
122 * element
123 *
124 * @param viewElement
125 * The XML view element
126 */
127 public void loadNewStates(@NonNull Element viewElement) {
128 stateValues.clear();
129 stateIndex.clear();
130 List<Element> states = XmlUtils.getChildElements(viewElement, TmfXmlStrings.DEFINED_VALUE);
131
132 for (Element state : states) {
133 int value = Integer.parseInt(state.getAttribute(TmfXmlStrings.VALUE));
134 String name = state.getAttribute(TmfXmlStrings.NAME);
135 String color = state.getAttribute(TmfXmlStrings.COLOR);
136
137 // FIXME Allow this case
138 if (value < 0) {
139 return;
140 }
141
142 RGB colorRGB = new RGB(255, 0, 0);
143 if (color.startsWith(TmfXmlStrings.COLOR_PREFIX)) {
144 Integer hex = Integer.parseInt(color.substring(1), 16);
145 int hex1 = hex.intValue() % 256;
146 int hex2 = (hex.intValue() / 256) % 256;
147 int hex3 = (hex.intValue() / (256 * 256)) % 256;
148 colorRGB = new RGB(hex3, hex2, hex1);
149 } else {
150 colorRGB = calcColor(value);
151 }
152
153 StateItem item = new StateItem(colorRGB, name);
154
155 Integer index = stateIndex.get(value);
156 if (index == null) {
157 /* Add the new state value */
158 stateIndex.put(value, stateValues.size());
159 stateValues.add(item);
160 } else {
161 /* Override a previous state value */
162 stateValues.set(index, item);
163 }
164 }
165 Display.getDefault().asyncExec(new Runnable() {
166 @Override
167 public void run() {
168 fireColorSettingsChanged();
169 }
170 });
171 }
172
173 private static RGB calcColor(int value) {
174 int x = (value * 97) % 1530;
175 int r = 0, g = 0, b = 0;
176 if (x >= 0 && x < 255) {
177 r = 255;
178 g = x;
179 b = 0;
180 }
181 if (x >= 255 && x < 510) {
182 r = 510 - x;
183 g = 255;
184 b = 0;
185 }
186 if (x >= 510 && x < 765) {
187 r = 0;
188 g = 255;
189 b = x - 510;
190 }
191 if (x >= 765 && x < 1020) {
192 r = 0;
193 g = 1020 - x;
194 b = 255;
195 }
196 if (x >= 1020 && x < 1275) {
197 r = x - 1020;
198 g = 0;
199 b = 255;
200 }
201 if (x >= 1275 && x <= 1530) {
202 r = 255;
203 g = 0;
204 b = 1530 - x;
205 }
206 return new RGB(r, g, b);
207 }
208
209 }
This page took 0.051201 seconds and 5 git commands to generate.