2010-11-05 Francois Chouinard <fchouinard@gmail.com> Fix for Bug329473
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / timeAnalysis / widgets / Utils.java
CommitLineData
b0d3496e
ASL
1/*****************************************************************************\r
2 * Copyright (c) 2007, 2008 Intel Corporation and others.\r
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
9 * Intel Corporation - Initial API and implementation\r
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation\r
11 * Alvaro Sanchex-Leon - Udpated for TMF\r
12 *\r
13 * $Id: Utils.java,v 1.11 2008/06/16 21:04:49 jkubasta Exp $ \r
14 *****************************************************************************/\r
15\r
16package org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.widgets;\r
17\r
18import java.text.SimpleDateFormat;\r
19import java.util.Date;\r
20import java.util.Iterator;\r
21import java.util.List;\r
a5823d5f 22import java.util.TimeZone;\r
b0d3496e
ASL
23import java.util.Vector;\r
24\r
25import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.ITimeAnalysisViewer.TimeFormat;\r
26import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITimeEvent;\r
27import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;\r
a5823d5f 28import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.TimeEvent;\r
b0d3496e
ASL
29import org.eclipse.swt.graphics.Color;\r
30import org.eclipse.swt.graphics.Device;\r
31import org.eclipse.swt.graphics.GC;\r
32import org.eclipse.swt.graphics.Point;\r
33import org.eclipse.swt.graphics.Rectangle;\r
34import org.eclipse.swt.widgets.Display;\r
35\r
36public class Utils {\r
37\r
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
44\r
45 static public final Object[] _empty = new Object[0];\r
46\r
47 static enum Resolution {\r
48 SECONDS, MILLISEC, MICROSEC, NANOSEC\r
49 };\r
50\r
a5823d5f
ASL
51 static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss");\r
52 static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd");\r
53 static {\r
54 stimeformat.setTimeZone(TimeZone.getTimeZone("GMT"));\r
55 sdateformat.setTimeZone(TimeZone.getTimeZone("GMT"));\r
56 }\r
b0d3496e
ASL
57\r
58// static private String _externalPlugin[] = { "org.eclipse.debug.ui",\r
59// "org.eclipse.debug.ui", "org.eclipse.debug.ui",\r
60// "org.eclipse.debug.ui", "org.eclipse.debug.ui", };\r
61//\r
62// static private String _externalPath[] = {\r
63// "icons/full/obj16/thread_obj.gif", // running thread\r
64// "icons/full/obj16/threads_obj.gif", // suspended\r
65// "icons/full/obj16/threadt_obj.gif", // stopped\r
66// "icons/full/obj16/stckframe_running_obj.gif", // running stack frame\r
67// "icons/full/obj16/stckframe_obj.gif", // stack frame\r
68// };\r
69\r
70// static public Image getImage(int idx) {\r
71// if (idx < 0 || idx >= IMG_NUM)\r
72// SWT.error(SWT.ERROR_INVALID_ARGUMENT);\r
73// String key = "trace.img." + idx;\r
74// Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);\r
75// if (null == img) {\r
76// ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(\r
77// _externalPlugin[idx], _externalPath[idx]);\r
78// TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);\r
79// img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);\r
80// }\r
81// return img;\r
82// }\r
83\r
84 static public void init(Rectangle rect) {\r
85 rect.x = 0;\r
86 rect.y = 0;\r
87 rect.width = 0;\r
88 rect.height = 0;\r
89 }\r
90\r
91 static public void init(Rectangle rect, int x, int y, int width, int height) {\r
92 rect.x = x;\r
93 rect.y = y;\r
94 rect.width = width;\r
95 rect.height = height;\r
96 }\r
97\r
98 static public void init(Rectangle rect, Rectangle source) {\r
99 rect.x = source.x;\r
100 rect.y = source.y;\r
101 rect.width = source.width;\r
102 rect.height = source.height;\r
103 }\r
104\r
105 static public void deflate(Rectangle rect, int x, int y) {\r
106 rect.x += x;\r
107 rect.y += y;\r
108 rect.width -= x + x;\r
109 rect.height -= y + y;\r
110 }\r
111\r
112 static public void inflate(Rectangle rect, int x, int y) {\r
113 rect.x -= x;\r
114 rect.y -= y;\r
115 rect.width += x + x;\r
116 rect.height += y + y;\r
117 }\r
118\r
119 static void dispose(Color col) {\r
120 if (null != col)\r
121 col.dispose();\r
122 }\r
123\r
124 static public Color mixColors(Device display, Color c1, Color c2, int w1,\r
125 int w2) {\r
126 return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())\r
127 / (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())\r
128 / (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())\r
129 / (w1 + w2));\r
130 }\r
131\r
132 static public Color getSysColor(int id) {\r
133 Color col = Display.getCurrent().getSystemColor(id);\r
134 return new Color(col.getDevice(), col.getRGB());\r
135 }\r
136\r
137 static public Color mixColors(Color col1, Color col2, int w1, int w2) {\r
138 return mixColors(Display.getCurrent(), col1, col2, w1, w2);\r
139 }\r
140\r
141 static public int drawText(GC gc, String text, Rectangle rect,\r
142 boolean transp) {\r
143 Point size = gc.stringExtent(text);\r
144 gc.drawText(text, rect.x, rect.y, transp);\r
145 return size.x;\r
146 }\r
147\r
148 static public int drawText(GC gc, String text, int x, int y, boolean transp) {\r
149 Point size = gc.stringExtent(text);\r
150 gc.drawText(text, x, y, transp);\r
151 return size.x;\r
152 }\r
153\r
154 /**\r
155 * Formats time in format: MM:SS:NNN\r
156 * \r
157 * @param v\r
158 * @param option\r
159 * 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn\r
160 * @return\r
161 */\r
162 static public String formatTime(long v, TimeFormat format, Resolution res) {\r
163 // if format is absolute (Calendar)\r
164 if (format == TimeFormat.ABSOLUTE) {\r
165 return formatTimeAbs(v, res);\r
166 }\r
167\r
168 StringBuffer str = new StringBuffer();\r
169 boolean neg = v < 0;\r
170 if (neg) {\r
171 v = -v;\r
172 str.append('-');\r
173 }\r
174\r
175 long sec = (long) (v * 1E-9);\r
176 // TODO: Expand to make it possible to select the minute, second, nanosecond format\r
177 //printing minutes is suppressed just sec and ns\r
178 // if (sec / 60 < 10)\r
179 // str.append('0');\r
180 // str.append(sec / 60);\r
181 // str.append(':');\r
182 // sec %= 60;\r
183 // if (sec < 10)\r
184 // str.append('0');\r
185 str.append(sec);\r
186 String ns = formatNs(v, res);\r
187 if (!ns.equals("")) {\r
188 str.append(':');\r
189 str.append(ns);\r
190 }\r
191\r
192 return str.toString();\r
193 }\r
194\r
195 /**\r
196 * From input time in nanoseconds, convert to Date format YYYY-MM-dd\r
197 * \r
198 * @param absTime\r
199 * @return\r
200 */\r
201 public static String formatDate(long absTime) {\r
202 String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));\r
203 return sdate;\r
204 }\r
205\r
206 /**\r
207 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn\r
208 * \r
209 * @param time\r
210 * @return\r
211 */\r
212 static public String formatTimeAbs(long time, Resolution res) {\r
213 StringBuffer str = new StringBuffer();\r
214\r
215 // format time from nanoseconds to calendar time HH:MM:SS\r
216 String stime = stimeformat.format(new Date((long) (time * 1E-6)));\r
217 str.append(stime + " ");\r
218 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in\r
219 // the Resolution\r
220 str.append(formatNs(time, res));\r
221 return str.toString();\r
222 }\r
223\r
224 /**\r
225 * Obtains the remainder fraction on unit Seconds of the entered value in\r
226 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction\r
227 * seconds can be obtained by removing the last 9 digits: 1241207054 the\r
228 * fractional portion of seconds, expressed in ns is: 171080214\r
229 * \r
230 * @param time\r
231 * @param res\r
232 * @return\r
233 */\r
234 public static String formatNs(long time, Resolution res) {\r
235 StringBuffer temp = new StringBuffer();\r
236 boolean neg = time < 0;\r
237 if (neg) {\r
238 time = -time;\r
239 }\r
240\r
241 // The following approach could be used although performance\r
242 // decreases in half.\r
243 // String strVal = String.format("%09d", time);\r
244 // String tmp = strVal.substring(strVal.length() - 9);\r
245\r
246 // number of segments to be included\r
247 int segments = 0;\r
248 switch (res) {\r
249 case MILLISEC:\r
250 segments = 1;\r
251 break;\r
252 case MICROSEC:\r
253 segments = 2;\r
254 break;\r
255 case NANOSEC:\r
256 segments = 3;\r
257 break;\r
258 default:\r
259 break;\r
260 }\r
261\r
262 long ns = time;\r
263 ns %= 1000000000;\r
264 if (ns < 10) {\r
265 temp.append("00000000");\r
266 } else if (ns < 100) {\r
267 temp.append("0000000");\r
268 } else if (ns < 1000) {\r
269 temp.append("000000");\r
270 } else if (ns < 10000) {\r
271 temp.append("00000");\r
272 } else if (ns < 100000) {\r
273 temp.append("0000");\r
274 } else if (ns < 1000000) {\r
275 temp.append("000");\r
276 } else if (ns < 10000000) {\r
277 temp.append("00");\r
278 } else if (ns < 100000000) {\r
279 temp.append("0");\r
280 }\r
281 temp.append(ns);\r
282\r
283 StringBuffer str = new StringBuffer();\r
284 if (segments > 0) {\r
285 // append ms\r
286 str.append(temp.substring(0, 3));\r
287 }\r
288 if (segments > 1) {\r
289 // append Micro secs\r
290 str.append(".");\r
291 str.append(temp.substring(3, 6));\r
292 }\r
293 if (segments > 2) {\r
294 // append Nano seconds\r
295 str.append(".");\r
296 str.append(temp.substring(6));\r
297 }\r
298\r
299 return str.toString();\r
300 }\r
301\r
302 static public int loadIntOption(String opt, int def, int min, int max) {\r
303 // int val =\r
304 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);\r
305 // if (0 == val)\r
306 // val = def;\r
307 // if (val < min)\r
308 // val = min;\r
309 // if (val > max)\r
310 // val = max;\r
311 return def;\r
312 }\r
313\r
314 // static public int loadIntOption(String opt) {\r
315 // int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);\r
316 // return val;\r
317 // }\r
318\r
319 static public void saveIntOption(String opt, int val) {\r
320 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);\r
321 }\r
322\r
323 static ITimeEvent getFirstEvent(ITmfTimeAnalysisEntry thread) {\r
324 if (null == thread)\r
325 return null;\r
326 Vector<TimeEvent> list = thread.getTraceEvents();\r
327 ITimeEvent event = null;\r
328 if (!list.isEmpty())\r
329 event = (ITimeEvent) list.get(0);\r
330 return event;\r
331 }\r
332\r
333 /**\r
334 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>\r
335 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area\r
336 * </list>\r
337 * \r
338 * @param thread\r
339 * @param time\r
340 * @param n\r
341 * @return\r
342 */\r
a5823d5f
ASL
343 static ITimeEvent findEvent(ITmfTimeAnalysisEntry thread, long time, int n) {\r
344 if (null == thread)\r
345 return null;\r
346 List<TimeEvent> list = thread.getTraceEvents();\r
347 Iterator<TimeEvent> it = list.iterator();\r
348 ITimeEvent nextEvent = null;\r
349 ITimeEvent currEvent = null;\r
350 ITimeEvent prevEvent = null;\r
351\r
352 while (it.hasNext()) {\r
353 nextEvent = (ITimeEvent) it.next();\r
354 long nextStartTime = nextEvent.getTime();\r
355 \r
356 if (nextStartTime > time) {\r
357 break;\r
358 }\r
359 \r
360 if (currEvent == null || currEvent.getTime() != nextStartTime) {\r
361 prevEvent = currEvent;\r
362 currEvent = nextEvent;\r
363 }\r
364 }\r
365 \r
366 if (n == -1) { //previous\r
367 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {\r
368 return prevEvent;\r
369 } else {\r
370 return currEvent;\r
371 }\r
372 } else if (n == 0) { //current\r
373 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {\r
374 return currEvent;\r
375 } else {\r
376 return null;\r
377 }\r
378 } else if (n == 1) { //next\r
379 return nextEvent;\r
380 } else if (n == 2) { //current or previous when in empty space\r
381 return currEvent;\r
382 }\r
383 \r
384 return null;\r
385 }\r
b0d3496e
ASL
386\r
387 // static public TRCPackage getPackage(Object element) {\r
388 // if (element instanceof TRCPackage)\r
389 // return (TRCPackage) element;\r
390 // if (element instanceof TRCClass)\r
391 // return ((TRCClass) element).getPackage();\r
392 // return null;\r
393 // }\r
394\r
395 // static public TRCObjectAllocationAnnotation getAllocationAnnotation(\r
396 // TRCClass cls) {\r
397 // TRCObjectAllocationAnnotation aa = null;\r
398 // EList list = cls.getAnnotations();\r
399 // int len = list.size();\r
400 // for (int i = 0; i < len; i++) {\r
401 // TRCAnnotation annotation = (TRCAnnotation) list.get(i);\r
402 // if (annotation instanceof TRCObjectAllocationAnnotation)\r
403 // aa = (TRCObjectAllocationAnnotation) annotation;\r
404 // }\r
405 // return aa;\r
406 // }\r
407\r
408 static public String fixMethodSignature(String sig) {\r
409 int pos = sig.indexOf('(');\r
410 if (pos >= 0) {\r
411 String ret = sig.substring(0, pos);\r
412 sig = sig.substring(pos);\r
413 sig = sig + " " + ret;\r
414 }\r
415 return sig;\r
416 }\r
417\r
418 static public String restoreMethodSignature(String sig) {\r
419 String ret = "";\r
420 int pos = sig.indexOf('(');\r
421 if (pos >= 0) {\r
422 ret = sig.substring(0, pos);\r
423 sig = sig.substring(pos + 1);\r
424 }\r
425 pos = sig.indexOf(')');\r
426 if (pos >= 0) {\r
427 sig = sig.substring(0, pos);\r
428 }\r
429 String args[] = sig.split(",");\r
430 sig = "(";\r
431 for (int i = 0; i < args.length; i++) {\r
432 String arg = args[i].trim();\r
433 if (arg.length() == 0 && args.length == 1)\r
434 break;\r
435 sig += getTypeSignature(arg);\r
436 }\r
437 sig += ")" + getTypeSignature(ret);\r
438 return sig;\r
439 }\r
440\r
441 static public String getTypeSignature(String type) {\r
442 int dim = 0;\r
443 for (int j = 0; j < type.length(); j++) {\r
444 if (type.charAt(j) == '[')\r
445 dim++;\r
446 }\r
447 int pos = type.indexOf('[');\r
448 if (pos >= 0)\r
449 type = type.substring(0, pos);\r
450 String sig = "";\r
451 for (int j = 0; j < dim; j++)\r
452 sig += "[";\r
453 if (type.equals("boolean"))\r
454 sig += "Z";\r
455 else if (type.equals("byte"))\r
456 sig += "B";\r
457 else if (type.equals("char"))\r
458 sig += "C";\r
459 else if (type.equals("short"))\r
460 sig += "S";\r
461 else if (type.equals("int"))\r
462 sig += "I";\r
463 else if (type.equals("long"))\r
464 sig += "J";\r
465 else if (type.equals("float"))\r
466 sig += "F";\r
467 else if (type.equals("double"))\r
468 sig += "D";\r
469 else if (type.equals("void"))\r
470 sig += "V";\r
471 else\r
472 sig += "L" + type.replace('.', '/') + ";";\r
473 return sig;\r
474 }\r
475\r
476 // static public boolean openSource(Object element) {\r
477 // if (element instanceof String) {\r
478 // final String pattern = (String) element;\r
479 // final int javaType = IJavaSearchConstants.METHOD;\r
480 // BusyIndicator.showWhile(Display.getDefault(), new Runnable() {\r
481 // public void run() {\r
482 // if (!OpenJavaSource.openSource(pattern, javaType,\r
483 // SearchEngine.createWorkspaceScope(), true)) {\r
484 // MessageDialog.openInformation(UIPlugin.getDefault()\r
485 // .getWorkbench().getActiveWorkbenchWindow()\r
486 // .getShell(), TraceMessages.TRC_MSGT, NLS.bind(\r
487 // TraceUIMessages._68, pattern));\r
488 // }\r
489 // }\r
490 // });\r
491 // }\r
492 // OpenSource.openSource(element);\r
493 // return true;\r
494 // }\r
495\r
496 // static public int getObjAge(TRCFullTraceObject obj, EList listGC) {\r
497 // int age = 0;\r
498 // double t0 = obj.getCreateTime();\r
499 // double t1 = obj.getCollectTime();\r
500 // int len = listGC.size();\r
501 // for (int j = 0; j < len; j++) {\r
502 // TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);\r
503 // if (gcEvent.getType().equals("finish")) {\r
504 // double time = gcEvent.getTime();\r
505 // if (time <= t0)\r
506 // continue;\r
507 // if (t1 > 0 && time >= t1)\r
508 // break;\r
509 // age++;\r
510 // }\r
511 // }\r
512 // return age;\r
513 // }\r
514\r
515 static public int compare(double d1, double d2) {\r
516 if (d1 > d2)\r
517 return 1;\r
518 if (d1 < d2)\r
519 return 1;\r
520 return 0;\r
521 }\r
522\r
523 static public int compare(String s1, String s2) {\r
524 if (s1 != null && s2 != null)\r
525 return s1.compareToIgnoreCase(s2);\r
526 if (s1 != null)\r
527 return 1;\r
528 if (s2 != null)\r
529 return -1;\r
530 return 0;\r
531 }\r
532\r
533 // static public String formatPercent(int val, int max) {\r
534 // String s = max > 0 && max >= val ? TString\r
535 // .formatAsPercentage((double) val / (double) max) : "";\r
536 // return s;\r
537 // }\r
538}\r
This page took 0.047751 seconds and 5 git commands to generate.