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