Bug 378401: Implementation of time graph widget.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / Utils.java
CommitLineData
b0d3496e 1/*****************************************************************************\r
fb5cad3d 2 * Copyright (c) 2007, 2008 Intel Corporation, 2009, 2012 Ericsson.\r
b0d3496e
ASL
3 * All rights reserved. This program and the accompanying materials\r
4 * are made available under the terms of the Eclipse Public License v1.0\r
5 * which accompanies this distribution, and is available at\r
6 * http://www.eclipse.org/legal/epl-v10.html\r
7 *\r
8 * Contributors:\r
fb5cad3d
PT
9 * Intel Corporation - Initial API and implementation\r
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation\r
11 * Alvaro Sanchez-Leon - Udpated for TMF\r
12 * Patrick Tasse - Refactoring\r
b0d3496e 13 *\r
b0d3496e
ASL
14 *****************************************************************************/\r
15\r
2fa130b8 16package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets;\r
b0d3496e
ASL
17\r
18import java.text.SimpleDateFormat;\r
19import java.util.Date;\r
20import java.util.Iterator;\r
b0d3496e 21\r
2fa130b8 22import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;\r
fb5cad3d 23import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;\r
b0d3496e
ASL
24import org.eclipse.swt.graphics.Color;\r
25import org.eclipse.swt.graphics.Device;\r
26import org.eclipse.swt.graphics.GC;\r
27import org.eclipse.swt.graphics.Point;\r
28import org.eclipse.swt.graphics.Rectangle;\r
29import org.eclipse.swt.widgets.Display;\r
30\r
31public class Utils {\r
32\r
fb5cad3d
PT
33 public enum TimeFormat {\r
34 RELATIVE, ABSOLUTE\r
35 };\r
b0d3496e 36\r
fb5cad3d
PT
37 static public final int IMG_THREAD_RUNNING = 0;\r
38 static public final int IMG_THREAD_SUSPENDED = 1;\r
39 static public final int IMG_THREAD_STOPPED = 2;\r
40 static public final int IMG_METHOD_RUNNING = 3;\r
41 static public final int IMG_METHOD = 4;\r
42 static public final int IMG_NUM = 5;\r
b0d3496e 43\r
fb5cad3d 44 static public final Object[] _empty = new Object[0];\r
b0d3496e 45\r
4a56c4e6 46 public static enum Resolution {\r
fb5cad3d
PT
47 SECONDS, MILLISEC, MICROSEC, NANOSEC\r
48 };\r
49\r
50 static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$\r
51 static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$\r
fb5cad3d
PT
52\r
53 static Rectangle clone(Rectangle source) {\r
54 return new Rectangle(source.x, source.y, source.width, source.height);\r
55 }\r
56\r
57 static public void init(Rectangle rect) {\r
58 rect.x = 0;\r
59 rect.y = 0;\r
60 rect.width = 0;\r
61 rect.height = 0;\r
62 }\r
63\r
64 static public void init(Rectangle rect, int x, int y, int width, int height) {\r
65 rect.x = x;\r
66 rect.y = y;\r
67 rect.width = width;\r
68 rect.height = height;\r
69 }\r
70\r
71 static public void init(Rectangle rect, Rectangle source) {\r
72 rect.x = source.x;\r
73 rect.y = source.y;\r
74 rect.width = source.width;\r
75 rect.height = source.height;\r
76 }\r
77\r
78 static public void deflate(Rectangle rect, int x, int y) {\r
79 rect.x += x;\r
80 rect.y += y;\r
81 rect.width -= x + x;\r
82 rect.height -= y + y;\r
83 }\r
84\r
85 static public void inflate(Rectangle rect, int x, int y) {\r
86 rect.x -= x;\r
87 rect.y -= y;\r
88 rect.width += x + x;\r
89 rect.height += y + y;\r
90 }\r
91\r
92 static void dispose(Color col) {\r
93 if (null != col)\r
94 col.dispose();\r
95 }\r
96\r
97 static public Color mixColors(Device display, Color c1, Color c2, int w1,\r
98 int w2) {\r
99 return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())\r
100 / (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())\r
101 / (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())\r
102 / (w1 + w2));\r
103 }\r
104\r
105 static public Color getSysColor(int id) {\r
106 Color col = Display.getCurrent().getSystemColor(id);\r
107 return new Color(col.getDevice(), col.getRGB());\r
108 }\r
109\r
110 static public Color mixColors(Color col1, Color col2, int w1, int w2) {\r
111 return mixColors(Display.getCurrent(), col1, col2, w1, w2);\r
112 }\r
113\r
114 static public int drawText(GC gc, String text, Rectangle rect, boolean transp) {\r
115 Point size = gc.stringExtent(text);\r
116 gc.drawText(text, rect.x, rect.y, transp);\r
117 return size.x;\r
118 }\r
119\r
120 static public int drawText(GC gc, String text, int x, int y, boolean transp) {\r
121 Point size = gc.stringExtent(text);\r
122 gc.drawText(text, x, y, transp);\r
123 return size.x;\r
124 }\r
125\r
126 /**\r
0d9a6d76
FC
127 * Formats time in format: MM:SS:NNN\r
128 * \r
fb5cad3d
PT
129 * @param time time\r
130 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn\r
131 * @param resolution the resolution\r
132 * @return the formatted time\r
133 */\r
134 static public String formatTime(long time, TimeFormat format, Resolution resolution) {\r
135 // if format is absolute (Calendar)\r
136 if (format == TimeFormat.ABSOLUTE) {\r
137 return formatTimeAbs(time, resolution);\r
138 }\r
139\r
140 StringBuffer str = new StringBuffer();\r
141 boolean neg = time < 0;\r
142 if (neg) {\r
143 time = -time;\r
144 str.append('-');\r
145 }\r
146\r
147 long sec = (long) (time * 1E-9);\r
148 // TODO: Expand to make it possible to select the minute, second, nanosecond format\r
149 //printing minutes is suppressed just sec and ns\r
150 // if (sec / 60 < 10)\r
151 // str.append('0');\r
152 // str.append(sec / 60);\r
153 // str.append(':');\r
154 // sec %= 60;\r
155 // if (sec < 10)\r
156 // str.append('0');\r
157 str.append(sec);\r
158 String ns = formatNs(time, resolution);\r
159 if (!ns.equals("")) { //$NON-NLS-1$\r
eee04af0 160 str.append('.');\r
fb5cad3d
PT
161 str.append(ns);\r
162 }\r
163\r
164 return str.toString();\r
165 }\r
166\r
167 /**\r
168 * From input time in nanoseconds, convert to Date format YYYY-MM-dd\r
169 * \r
170 * @param absTime\r
171 * @return the formatted date\r
172 */\r
173 public static String formatDate(long absTime) {\r
174 String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));\r
175 return sdate;\r
176 }\r
177\r
178 /**\r
179 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn\r
180 * \r
181 * @param time\r
182 * @return the formatted time\r
183 */\r
184 static public String formatTimeAbs(long time, Resolution res) {\r
185 StringBuffer str = new StringBuffer();\r
186\r
187 // format time from nanoseconds to calendar time HH:MM:SS\r
188 String stime = stimeformat.format(new Date((long) (time * 1E-6)));\r
eee04af0
PT
189 str.append(stime);\r
190 str.append('.');\r
fb5cad3d
PT
191 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in\r
192 // the Resolution\r
193 str.append(formatNs(time, res));\r
194 return str.toString();\r
195 }\r
196\r
197 /**\r
198 * Obtains the remainder fraction on unit Seconds of the entered value in\r
199 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction\r
200 * seconds can be obtained by removing the last 9 digits: 1241207054 the\r
201 * fractional portion of seconds, expressed in ns is: 171080214\r
202 * \r
203 * @param time\r
204 * @param res\r
205 * @return the formatted nanosec\r
206 */\r
207 public static String formatNs(long time, Resolution res) {\r
eee04af0 208 StringBuffer str = new StringBuffer();\r
fb5cad3d
PT
209 boolean neg = time < 0;\r
210 if (neg) {\r
211 time = -time;\r
212 }\r
213\r
214 // The following approach could be used although performance\r
215 // decreases in half.\r
216 // String strVal = String.format("%09d", time);\r
217 // String tmp = strVal.substring(strVal.length() - 9);\r
218\r
fb5cad3d
PT
219 long ns = time;\r
220 ns %= 1000000000;\r
221 if (ns < 10) {\r
eee04af0 222 str.append("00000000"); //$NON-NLS-1$\r
fb5cad3d 223 } else if (ns < 100) {\r
eee04af0 224 str.append("0000000"); //$NON-NLS-1$\r
fb5cad3d 225 } else if (ns < 1000) {\r
eee04af0 226 str.append("000000"); //$NON-NLS-1$\r
fb5cad3d 227 } else if (ns < 10000) {\r
eee04af0 228 str.append("00000"); //$NON-NLS-1$\r
fb5cad3d 229 } else if (ns < 100000) {\r
eee04af0 230 str.append("0000"); //$NON-NLS-1$\r
fb5cad3d 231 } else if (ns < 1000000) {\r
eee04af0 232 str.append("000"); //$NON-NLS-1$\r
fb5cad3d 233 } else if (ns < 10000000) {\r
eee04af0 234 str.append("00"); //$NON-NLS-1$\r
fb5cad3d 235 } else if (ns < 100000000) {\r
eee04af0 236 str.append("0"); //$NON-NLS-1$\r
fb5cad3d 237 }\r
eee04af0
PT
238 str.append(ns);\r
239\r
240 if (res == Resolution.MILLISEC) {\r
241 return str.substring(0, 3);\r
242 } else if (res == Resolution.MICROSEC) {\r
243 return str.substring(0, 6);\r
244 } else if (res == Resolution.NANOSEC) {\r
245 return str.substring(0, 9);\r
fb5cad3d 246 }\r
eee04af0 247 return ""; //$NON-NLS-1$\r
fb5cad3d
PT
248 }\r
249\r
250 static public int loadIntOption(String opt, int def, int min, int max) {\r
251 // int val =\r
252 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);\r
253 // if (0 == val)\r
254 // val = def;\r
255 // if (val < min)\r
256 // val = min;\r
257 // if (val > max)\r
258 // val = max;\r
259 return def;\r
260 }\r
261\r
262 static public void saveIntOption(String opt, int val) {\r
263 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);\r
264 }\r
265\r
266 static ITimeEvent getFirstEvent(ITimeGraphEntry thread) {\r
267 if (null == thread)\r
268 return null;\r
269 Iterator<ITimeEvent> iterator = thread.getTimeEventsIterator();\r
270 if (iterator != null && iterator.hasNext()) {\r
271 return iterator.next();\r
272 } else {\r
273 return null;\r
274 }\r
275 }\r
276\r
277 /**\r
278 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>\r
279 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area\r
280 * </list>\r
281 * \r
282 * @param thread\r
283 * @param time\r
284 * @param n\r
285 * @return\r
286 */\r
287 static ITimeEvent findEvent(ITimeGraphEntry thread, long time, int n) {\r
a5823d5f
ASL
288 if (null == thread)\r
289 return null;\r
fb5cad3d
PT
290 Iterator<ITimeEvent> iterator = thread.getTimeEventsIterator();\r
291 if (iterator == null) {\r
292 return null;\r
293 }\r
a5823d5f
ASL
294 ITimeEvent nextEvent = null;\r
295 ITimeEvent currEvent = null;\r
296 ITimeEvent prevEvent = null;\r
297\r
ce62370f
FC
298 while (iterator.hasNext()) {\r
299 nextEvent = (ITimeEvent) iterator.next();\r
a5823d5f 300 long nextStartTime = nextEvent.getTime();\r
fb5cad3d 301\r
a5823d5f
ASL
302 if (nextStartTime > time) {\r
303 break;\r
304 }\r
fb5cad3d 305\r
a5823d5f
ASL
306 if (currEvent == null || currEvent.getTime() != nextStartTime) {\r
307 prevEvent = currEvent;\r
308 currEvent = nextEvent;\r
309 }\r
310 }\r
fb5cad3d 311\r
a5823d5f
ASL
312 if (n == -1) { //previous\r
313 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {\r
314 return prevEvent;\r
315 } else {\r
316 return currEvent;\r
317 }\r
318 } else if (n == 0) { //current\r
319 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {\r
320 return currEvent;\r
321 } else {\r
322 return null;\r
323 }\r
324 } else if (n == 1) { //next\r
325 return nextEvent;\r
326 } else if (n == 2) { //current or previous when in empty space\r
327 return currEvent;\r
328 }\r
fb5cad3d 329\r
a5823d5f
ASL
330 return null;\r
331 }\r
b0d3496e 332\r
fb5cad3d
PT
333 static public String fixMethodSignature(String sig) {\r
334 int pos = sig.indexOf('(');\r
335 if (pos >= 0) {\r
336 String ret = sig.substring(0, pos);\r
337 sig = sig.substring(pos);\r
338 sig = sig + " " + ret; //$NON-NLS-1$\r
339 }\r
340 return sig;\r
341 }\r
342\r
343 static public String restoreMethodSignature(String sig) {\r
344 String ret = ""; //$NON-NLS-1$\r
345 int pos = sig.indexOf('(');\r
346 if (pos >= 0) {\r
347 ret = sig.substring(0, pos);\r
348 sig = sig.substring(pos + 1);\r
349 }\r
350 pos = sig.indexOf(')');\r
351 if (pos >= 0) {\r
352 sig = sig.substring(0, pos);\r
353 }\r
354 String args[] = sig.split(","); //$NON-NLS-1$\r
5a5c2fc7 355 StringBuffer result = new StringBuffer("("); //$NON-NLS-1$\r
fb5cad3d
PT
356 for (int i = 0; i < args.length; i++) {\r
357 String arg = args[i].trim();\r
358 if (arg.length() == 0 && args.length == 1)\r
359 break;\r
360 result.append(getTypeSignature(arg));\r
361 }\r
362 result.append(")").append(getTypeSignature(ret)); //$NON-NLS-1$\r
363 return result.toString();\r
364 }\r
365\r
366 static public String getTypeSignature(String type) {\r
367 int dim = 0;\r
368 for (int j = 0; j < type.length(); j++) {\r
369 if (type.charAt(j) == '[')\r
370 dim++;\r
371 }\r
372 int pos = type.indexOf('[');\r
373 if (pos >= 0)\r
374 type = type.substring(0, pos);\r
375 StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$\r
376 for (int j = 0; j < dim; j++)\r
377 sig.append("["); //$NON-NLS-1$\r
378 if (type.equals("boolean")) //$NON-NLS-1$\r
379 sig.append("Z"); //$NON-NLS-1$\r
380 else if (type.equals("byte")) //$NON-NLS-1$\r
381 sig.append("B"); //$NON-NLS-1$\r
382 else if (type.equals("char")) //$NON-NLS-1$\r
383 sig.append("C"); //$NON-NLS-1$\r
384 else if (type.equals("short")) //$NON-NLS-1$\r
385 sig.append("S"); //$NON-NLS-1$\r
386 else if (type.equals("int")) //$NON-NLS-1$\r
387 sig.append("I"); //$NON-NLS-1$\r
388 else if (type.equals("long")) //$NON-NLS-1$\r
389 sig.append("J"); //$NON-NLS-1$\r
390 else if (type.equals("float")) //$NON-NLS-1$\r
391 sig.append("F"); //$NON-NLS-1$\r
392 else if (type.equals("double")) //$NON-NLS-1$\r
393 sig.append("D"); //$NON-NLS-1$\r
394 else if (type.equals("void")) //$NON-NLS-1$\r
395 sig.append("V"); //$NON-NLS-1$\r
396 else\r
397 sig.append("L").append(type.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$\r
398 return sig.toString();\r
399 }\r
400\r
401 static public int compare(double d1, double d2) {\r
402 if (d1 > d2)\r
403 return 1;\r
404 if (d1 < d2)\r
405 return 1;\r
406 return 0;\r
407 }\r
408\r
409 static public int compare(String s1, String s2) {\r
410 if (s1 != null && s2 != null)\r
411 return s1.compareToIgnoreCase(s2);\r
412 if (s1 != null)\r
413 return 1;\r
414 if (s2 != null)\r
415 return -1;\r
416 return 0;\r
417 }\r
b0d3496e 418}\r
This page took 0.057548 seconds and 5 git commands to generate.