Fix for bug289620
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / timeAnalysis / TmfTimeAnalysisProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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 * Alvaro Sanchez-Leon - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis;
14
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
20 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITimeEvent;
21 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;
22 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.TimeEvent;
23 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.widgets.TraceColorScheme;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.ui.plugin.AbstractUIPlugin;
29
30 public abstract class TmfTimeAnalysisProvider {
31
32 static public final int IMG_THREAD_RUNNING = 0;
33 static public final int IMG_THREAD_SUSPENDED = 1;
34 static public final int IMG_THREAD_STOPPED = 2;
35 static public final int IMG_METHOD_RUNNING = 3;
36 static public final int IMG_METHOD = 4;
37 static public final int IMG_NUM = 5;
38
39 public enum StateColor {
40 GREEN, DARK_BLUE, RED, GOLD, ORANGE, GRAY, BLACK, DARK_GREEN, DARK_YELLOW, MAGENTA3, PURPLE1, PINK1, AQUAMARINE, LIGHT_BLUE, CADET_BLUE, OLIVE;
41
42 private String stateName;
43
44 StateColor() {
45 String undef = new String("Undefined");
46 this.stateName = undef;
47 }
48
49 public String getStateName() {
50 return stateName;
51 }
52
53 public void setStateName(String stateName) {
54 this.stateName = stateName;
55 }
56 }
57
58 static private String _externalPath[] = {
59 "icons/full/obj16/thread_obj.gif", // running thread
60 "icons/full/obj16/threads_obj.gif", // suspended
61 "icons/full/obj16/threadt_obj.gif", // stopped
62 "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
63 "icons/full/obj16/stckframe_obj.gif", // stack frame
64 };
65
66 static private String _externalPlugin[] = { "org.eclipse.debug.ui",
67 "org.eclipse.debug.ui", "org.eclipse.debug.ui",
68 "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
69
70 static private Image getImage(int idx) {
71 if (idx < 0 || idx >= IMG_NUM)
72 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
73 String key = "trace.img." + idx;
74 Image img = TmfUiPlugin.getDefault().getImageRegistry().get(key);
75 if (null == img) {
76 ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
77 _externalPlugin[idx], _externalPath[idx]);
78 TmfUiPlugin.getDefault().getImageRegistry().put(key, desc);
79 img = TmfUiPlugin.getDefault().getImageRegistry().get(key);
80 }
81 return img;
82 }
83
84 public void drawState(TraceColorScheme colors, ITimeEvent event,
85 Rectangle rect, GC gc, boolean selected, boolean rectBound,
86 boolean timeSelected) {
87 int colorIdx = getEventColorVal(event);
88 drawState(colors, colorIdx, rect, gc, selected, rectBound, timeSelected);
89
90 }
91
92 public void drawState(TraceColorScheme colors, int colorIdx,
93 Rectangle rect, GC gc, boolean selected, boolean rectBound,
94 boolean timeSelected) {
95
96 boolean visible = rect.width == 0 ? false : true;
97 int colorIdx1 = colorIdx;
98
99 timeSelected = timeSelected && selected;
100 if (timeSelected) {
101 colorIdx1 = colorIdx + TraceColorScheme.STATES_SEL0
102 - TraceColorScheme.STATES0;
103 }
104
105 if (visible) {
106 // fill all rect area
107 if (rect.isEmpty())
108 return;
109
110 gc.setBackground(colors.getColor(colorIdx1));
111 gc.fillRectangle(rect);
112 colorIdx1 = colorIdx + TraceColorScheme.STATES_BORDER0
113 - TraceColorScheme.STATES0;
114 gc.setForeground(colors.getColor(colorIdx1));
115
116 // draw bounds
117 if (!timeSelected) {
118 if (rectBound && rect.width >= 3) {
119 gc.drawRectangle(rect.x, rect.y, rect.width - 1,
120 rect.height - 1);
121 } else {
122 // Draw the top and bottom borders i.e. no side borders
123 // top
124 gc
125 .drawLine(rect.x, rect.y, rect.x + rect.width - 1,
126 rect.y);
127 // bottom
128 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x
129 + rect.width - 1, rect.y + rect.height - 1);
130 }
131 }
132 // draw decoration middle line
133 // int mindy = rect.y + rect.height / 2;
134 // if (TraceColorScheme.GOLD_STATE == colorIdx
135 // || TraceColorScheme.ORANGE_STATE == colorIdx) {
136 // int s = gc.getLineStyle();
137 // int w = gc.getLineWidth();
138 // gc.setLineStyle(SWT.LINE_DOT);
139 // gc.setLineWidth(2);
140 // gc.drawLine(rect.x, mindy, rect.x + rect.width, mindy);
141 // gc.setLineStyle(s);
142 // gc.setLineWidth(w);
143 // } else if (TraceColorScheme.RED_STATE == colorIdx
144 // || TraceColorScheme.GRAY_STATE == colorIdx) {
145 // int w = gc.getLineWidth();
146 // gc.setLineWidth(2);
147 // gc.drawLine(rect.x, mindy, rect.x + rect.width, mindy);
148 // gc.setLineWidth(w);
149 // }
150 // // draw selection bounds
151 // if (timeSelected) {
152 // gc.setForeground(colors
153 // .getColor(TraceColorScheme.SELECTED_TIME));
154 // if (rect.width >= 3) {
155 // gc.drawRectangle(rect.x, rect.y, rect.width - 1,
156 // rect.height - 1);
157 // // gc.drawRectangle(rect.x + 1, rect.y + 1, rect.width - 3,
158 // // rect.height - 3);
159 // } else {
160 // gc
161 // .drawLine(rect.x, rect.y, rect.x + rect.width - 1,
162 // rect.y);
163 // gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x
164 // + rect.width - 1, rect.y + rect.height - 1);
165 // }
166 // gc.drawLine(rect.x, rect.y + 1, rect.x + rect.width - 1,
167 // rect.y + 1);
168 // gc.drawLine(rect.x, rect.y + rect.height - 2, rect.x
169 // + rect.width - 1, rect.y + rect.height - 2);
170 // }
171 } else {
172 // selected rectangle area is not visible but can be represented
173 // with a broken vertical line of specified width.
174 int width = 2;
175 rect.width = width;
176 // check if height is greater than zero.
177 if (rect.isEmpty())
178 return;
179 // colorIdx1 = TraceColorScheme.BLACK;
180 gc.setForeground(colors.getColor(colorIdx));
181 int s = gc.getLineStyle();
182 int w = gc.getLineWidth();
183 gc.setLineStyle(SWT.LINE_DOT);
184 gc.setLineWidth(width);
185 // Trace.debug("Reactangle not visible, drawing vertical line with: "
186 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
187 // + rect.height);
188 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
189 gc.setLineStyle(s);
190 gc.setLineWidth(w);
191 }
192 }
193
194 /**
195 * Uses the abstract method getEventcolor to obtain an enum value and
196 * convert it to an internal color index
197 *
198 * @param event
199 * @return
200 */
201 public int getEventColorVal(ITimeEvent event) {
202 StateColor colors = getEventColor(event);
203 if (colors == StateColor.GREEN) {
204 return TraceColorScheme.GREEN_STATE;
205 } else if (colors == StateColor.DARK_BLUE) {
206 return TraceColorScheme.DARK_BLUE_STATE;
207 } else if (colors == StateColor.RED) {
208 return TraceColorScheme.RED_STATE;
209 } else if (colors == StateColor.GOLD) {
210 return TraceColorScheme.GOLD_STATE;
211 } else if (colors == StateColor.ORANGE) {
212 return TraceColorScheme.ORANGE_STATE;
213 } else if (colors == StateColor.GRAY) {
214 return TraceColorScheme.GRAY_STATE;
215 } else if (colors == StateColor.DARK_GREEN) {
216 return TraceColorScheme.DARK_GREEN_STATE;
217 } else if (colors == StateColor.DARK_YELLOW) {
218 return TraceColorScheme.DARK_YELLOW_STATE;
219 } else if (colors == StateColor.MAGENTA3) {
220 return TraceColorScheme.MAGENTA3_STATE;
221 } else if (colors == StateColor.PURPLE1) {
222 return TraceColorScheme.PURPLE1_STATE;
223 } else if (colors == StateColor.PINK1) {
224 return TraceColorScheme.PINK1_STATE;
225 } else if (colors == StateColor.AQUAMARINE) {
226 return TraceColorScheme.AQUAMARINE_STATE;
227 } else if (colors == StateColor.LIGHT_BLUE) {
228 return TraceColorScheme.LIGHT_BLUE_STATE;
229 } else if (colors == StateColor.CADET_BLUE) {
230 return TraceColorScheme.CADET_BLUE_STATE_SEL;
231 } else if (colors == StateColor.OLIVE) {
232 return TraceColorScheme.OLIVE_STATE;
233 }
234
235 return TraceColorScheme.BLACK_STATE;
236 }
237
238 /**
239 * Select the color for the different internal variants of events.
240 *
241 * @param event
242 * @return
243 */
244 public abstract StateColor getEventColor(ITimeEvent event);
245
246 /**
247 * This values is appended between braces to the right of Trace Name e.g.
248 * Trace And Error Log [Board 17] or for a Thread trace e.g. State Server
249 * [java.lang.Thread]
250 *
251 * @param trace
252 * @return
253 */
254 public abstract String getTraceClassName(ITmfTimeAnalysisEntry trace);
255
256 public String getEventName(ITimeEvent event) {
257 return getEventName(event, true, false);
258 }
259
260 /**
261 * Specify a Name for the event depending on its type or state e.g. blocked,
262 * running, etc..
263 *
264 * @param event
265 * @param upper
266 * True return String value in Upper case
267 * @param extInfo
268 * Verbose, add additional information if applicable
269 * @return
270 */
271 public abstract String getEventName(ITimeEvent event, boolean upper,
272 boolean extInfo);
273
274 public String composeTraceName(ITmfTimeAnalysisEntry trace, boolean inclState) {
275 String name = trace.getName();
276 String threadClass = getTraceClassName(trace);
277 if (threadClass != null && threadClass.length() > 0) {
278 name += " [" + threadClass + "]";
279 }
280 if (inclState) {
281 List<TimeEvent> list = trace.getTraceEvents();
282 if (null != list && list.size() > 0) {
283 ITimeEvent event = (ITimeEvent) list.get(list.size() - 1);
284 name += " (" + getEventName(event, false, true) + ")";
285 }
286 }
287 return name;
288 }
289
290 public String composeEventName(ITimeEvent event) {
291 String name = event.getEntry().getName();
292 String threadClass = getTraceClassName(event.getEntry());
293 if (threadClass != null && threadClass.length() > 0) {
294 name += " [" + threadClass + "]";
295 }
296 name += " (" + getEventName(event, false, true) + ")";
297 return name;
298 }
299
300 public abstract Map<String, String> getEventHoverToolTipInfo(
301 ITimeEvent event);
302
303 /**
304 * Provides the image icon for a given Event or Trace e.g. customize to use
305 * different icons according to specific event /state combination
306 *
307 * @param obj
308 * @return
309 */
310 public Image getItemImage(Object obj) {
311 if (obj instanceof ITmfTimeAnalysisEntry) {
312 List<TimeEvent> list = ((ITmfTimeAnalysisEntry) obj).getTraceEvents();
313 if (null != list && list.size() > 0)
314 obj = list.get(list.size() - 1);
315 else if (((ITmfTimeAnalysisEntry) obj).getStopTime() > 0)
316 return getImage(IMG_THREAD_STOPPED);
317 else
318 return getImage(IMG_THREAD_RUNNING);
319 }
320 if (obj instanceof TimeEvent) {
321 return getImage(IMG_THREAD_RUNNING);
322 }
323 return null;
324 }
325
326 public abstract String getStateName(StateColor color);
327
328 }
This page took 0.037355 seconds and 5 git commands to generate.