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