1 /*****************************************************************************
2 * Copyright (c) 2007, 2008 Intel Corporation, 2009, 2012 Ericsson.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchez-Leon - Udpated for TMF
12 * Patrick Tasse - Refactoring
14 *****************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
18 import java
.text
.NumberFormat
;
19 import java
.text
.SimpleDateFormat
;
20 import java
.util
.Date
;
21 import java
.util
.Iterator
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
25 import org
.eclipse
.swt
.graphics
.Color
;
26 import org
.eclipse
.swt
.graphics
.Device
;
27 import org
.eclipse
.swt
.graphics
.GC
;
28 import org
.eclipse
.swt
.graphics
.Point
;
29 import org
.eclipse
.swt
.graphics
.Rectangle
;
30 import org
.eclipse
.swt
.widgets
.Display
;
33 * General utilities and definitions used by the time graph widget
36 * @author Alvaro Sanchez-Leon
37 * @author Patrick Tasse
41 /** Time format for dates and timestamp */
42 public enum TimeFormat
{
43 /** Relative to the start of the trace */
45 /** Absolute timestamp (ie, relative to the Unix epoch) */
47 /** timestamp displayed as a simple number */
51 static public final int IMG_THREAD_RUNNING
= 0;
52 static public final int IMG_THREAD_SUSPENDED
= 1;
53 static public final int IMG_THREAD_STOPPED
= 2;
54 static public final int IMG_METHOD_RUNNING
= 3;
55 static public final int IMG_METHOD
= 4;
56 static public final int IMG_NUM
= 5;
58 static public final Object
[] _empty
= new Object
[0];
60 public static enum Resolution
{
61 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
64 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
65 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
67 static Rectangle
clone(Rectangle source
) {
68 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
72 * Initialize a Rectangle object to default values (all equal to 0)
75 * The Rectangle to initialize
77 static public void init(Rectangle rect
) {
85 * Initialize a Rectangle object with all the given values
88 * The Rectangle object to initialize
94 * The width of the rectangle
96 * The height of the rectangle
98 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
102 rect
.height
= height
;
106 * Initialize a Rectangle object to another existing Rectangle's values.
109 * The Rectangle to initialize
111 * The reference Rectangle to copy
113 static public void init(Rectangle rect
, Rectangle source
) {
116 rect
.width
= source
.width
;
117 rect
.height
= source
.height
;
121 * Reduce the size of a given rectangle by the given amounts.
124 * The rectangle to modify
126 * The reduction in width
128 * The reduction in height
130 static public void deflate(Rectangle rect
, int x
, int y
) {
134 rect
.height
-= y
+ y
;
138 * Increase the size of a given rectangle by the given amounts.
141 * The rectangle to modify
143 * The augmentation in width
145 * The augmentation in height
147 static public void inflate(Rectangle rect
, int x
, int y
) {
151 rect
.height
+= y
+ y
;
154 static void dispose(Color col
) {
161 * Get the resulting color from a mix of two existing ones for a given
165 * The display device (which might affect the color conversion)
171 * The gamma level for color 1
173 * The gamma level for color 2
174 * @return The resulting color
176 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
178 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
179 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
180 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
185 * Get the system color with the given ID.
189 * @return The resulting color
191 static public Color
getSysColor(int id
) {
192 Color col
= Display
.getCurrent().getSystemColor(id
);
193 return new Color(col
.getDevice(), col
.getRGB());
197 * Get the resulting color from a mix of two existing ones for the current
205 * The gamma level for color 1
207 * The gamma level for color 2
208 * @return The resulting color
210 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
211 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
215 * Draw text in a rectangle.
222 * The rectangle object which is being drawn
224 * Should we transpose the color
225 * @return The X coordinate where we have written
227 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
228 Point size
= gc
.stringExtent(text
);
229 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
234 * Draw text at a given location.
241 * The X coordinate of the starting point
243 * the Y coordinate of the starting point
245 * Should we transpose the color
246 * @return The X coordinate where we have written
248 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
249 Point size
= gc
.stringExtent(text
);
250 gc
.drawText(text
, x
, y
, transp
);
255 * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
260 * The string to be drawn
262 * The x coordinate of the top left corner of the rectangular area where the text is to be drawn
264 * The y coordinate of the top left corner of the rectangular area where the text is to be drawn
266 * The width of the area to be drawn
268 * If <code>true</code> the text will be centered in the available width if space permits
269 * @param isTransparent
270 * If <code>true</code> the background will be transparent, otherwise it will be opaque
271 * @return The number of characters written
275 static public int drawText(GC gc
, String text
, int x
, int y
, int width
, boolean isCentered
, boolean isTransparent
) {
276 int len
= text
.length();
279 textWidth
= gc
.stringExtent(text
.substring(0, len
)).x
;
280 if (textWidth
<= width
) {
288 x
+= (width
- textWidth
) / 2;
290 gc
.drawText(text
.substring(0, len
), x
, y
, isTransparent
);
296 * Formats time in format: MM:SS:NNN
299 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
300 * @param resolution the resolution
301 * @return the formatted time
303 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
304 // if format is absolute (Calendar)
305 if (format
== TimeFormat
.CALENDAR
) {
306 return formatTimeAbs(time
, resolution
);
307 } else if (format
== TimeFormat
.NUMBER
) {
308 return NumberFormat
.getInstance().format(time
);
311 StringBuffer str
= new StringBuffer();
312 boolean neg
= time
< 0;
318 long sec
= (long) (time
* 1E-9);
319 // TODO: Expand to make it possible to select the minute, second, nanosecond format
320 //printing minutes is suppressed just sec and ns
321 // if (sec / 60 < 10)
323 // str.append(sec / 60);
329 String ns
= formatNs(time
, resolution
);
330 if (!ns
.equals("")) { //$NON-NLS-1$
335 return str
.toString();
339 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
342 * The source time, in ns
343 * @return the formatted date
345 public static String
formatDate(long absTime
) {
346 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
351 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
354 * The source time, in ns
356 * The resolution to use
357 * @return the formatted time
359 static public String
formatTimeAbs(long time
, Resolution res
) {
360 StringBuffer str
= new StringBuffer();
362 // format time from nanoseconds to calendar time HH:MM:SS
363 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
366 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
368 str
.append(formatNs(time
, res
));
369 return str
.toString();
373 * Obtains the remainder fraction on unit Seconds of the entered value in
374 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
375 * seconds can be obtained by removing the last 9 digits: 1241207054 the
376 * fractional portion of seconds, expressed in ns is: 171080214
379 * The source time in ns
381 * The Resolution to use
382 * @return the formatted nanosec
384 public static String
formatNs(long time
, Resolution res
) {
385 StringBuffer str
= new StringBuffer();
386 boolean neg
= time
< 0;
391 // The following approach could be used although performance
392 // decreases in half.
393 // String strVal = String.format("%09d", time);
394 // String tmp = strVal.substring(strVal.length() - 9);
399 str
.append("00000000"); //$NON-NLS-1$
400 } else if (ns
< 100) {
401 str
.append("0000000"); //$NON-NLS-1$
402 } else if (ns
< 1000) {
403 str
.append("000000"); //$NON-NLS-1$
404 } else if (ns
< 10000) {
405 str
.append("00000"); //$NON-NLS-1$
406 } else if (ns
< 100000) {
407 str
.append("0000"); //$NON-NLS-1$
408 } else if (ns
< 1000000) {
409 str
.append("000"); //$NON-NLS-1$
410 } else if (ns
< 10000000) {
411 str
.append("00"); //$NON-NLS-1$
412 } else if (ns
< 100000000) {
413 str
.append("0"); //$NON-NLS-1$
417 if (res
== Resolution
.MILLISEC
) {
418 return str
.substring(0, 3);
419 } else if (res
== Resolution
.MICROSEC
) {
420 return str
.substring(0, 6);
421 } else if (res
== Resolution
.NANOSEC
) {
422 return str
.substring(0, 9);
424 return ""; //$NON-NLS-1$
428 * FIXME Currently does nothing.
435 * The minimal accepted value
437 * The maximal accepted value
438 * @return The value that was read
440 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
442 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
453 * FIXME currently does nothing
460 static public void saveIntOption(String opt
, int val
) {
461 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
464 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
465 if (null == entry
|| ! entry
.hasTimeEvents()) {
468 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
469 if (iterator
!= null && iterator
.hasNext()) {
470 return iterator
.next();
476 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
477 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
485 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
486 if (null == entry
|| ! entry
.hasTimeEvents()) {
489 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
490 if (iterator
== null) {
493 ITimeEvent nextEvent
= null;
494 ITimeEvent currEvent
= null;
495 ITimeEvent prevEvent
= null;
497 while (iterator
.hasNext()) {
498 nextEvent
= iterator
.next();
499 long nextStartTime
= nextEvent
.getTime();
501 if (nextStartTime
> time
) {
505 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
506 prevEvent
= currEvent
;
507 currEvent
= nextEvent
;
511 if (n
== -1) { //previous
512 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
516 } else if (n
== 0) { //current
517 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
521 } else if (n
== 1) { //next
522 if (nextEvent
!= null && nextEvent
.getTime() > time
) {
526 } else if (n
== 2) { //current or previous when in empty space
534 * Pretty-print a method signature.
537 * The original signature
538 * @return The pretty signature
540 static public String
fixMethodSignature(String sig
) {
541 int pos
= sig
.indexOf('(');
543 String ret
= sig
.substring(0, pos
);
544 sig
= sig
.substring(pos
);
545 sig
= sig
+ " " + ret
; //$NON-NLS-1$
551 * Restore an original method signature from a pretty-printed one.
554 * The pretty-printed signature
555 * @return The original method signature
557 static public String
restoreMethodSignature(String sig
) {
558 String ret
= ""; //$NON-NLS-1$
559 int pos
= sig
.indexOf('(');
561 ret
= sig
.substring(0, pos
);
562 sig
= sig
.substring(pos
+ 1);
564 pos
= sig
.indexOf(')');
566 sig
= sig
.substring(0, pos
);
568 String args
[] = sig
.split(","); //$NON-NLS-1$
569 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
570 for (int i
= 0; i
< args
.length
; i
++) {
571 String arg
= args
[i
].trim();
572 if (arg
.length() == 0 && args
.length
== 1) {
575 result
.append(getTypeSignature(arg
));
577 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
578 return result
.toString();
582 * Get the mangled type information from an array of types.
585 * The types to convert. See method implementation for what it
587 * @return The mangled string of types
589 static public String
getTypeSignature(String type
) {
591 for (int j
= 0; j
< type
.length(); j
++) {
592 if (type
.charAt(j
) == '[') {
596 int pos
= type
.indexOf('[');
598 type
= type
.substring(0, pos
);
600 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
601 for (int j
= 0; j
< dim
; j
++)
603 sig
.append("["); //$NON-NLS-1$
605 if (type
.equals("boolean")) { //$NON-NLS-1$
607 } else if (type
.equals("byte")) { //$NON-NLS-1$
609 } else if (type
.equals("char")) { //$NON-NLS-1$
611 } else if (type
.equals("short")) { //$NON-NLS-1$
613 } else if (type
.equals("int")) { //$NON-NLS-1$
615 } else if (type
.equals("long")) { //$NON-NLS-1$
617 } else if (type
.equals("float")) { //$NON-NLS-1$
619 } else if (type
.equals("double")) { //$NON-NLS-1$
621 } else if (type
.equals("void")) { //$NON-NLS-1$
625 sig
.append('L').append(type
.replace('.', '/')).append(';');
627 return sig
.toString();
631 * Compare two doubles together.
637 * @return 1 if they are different, and 0 if they are *exactly* the same.
638 * Because of the way doubles are stored, it's possible for the
639 * same number obtained in two different ways to actually look
642 static public int compare(double d1
, double d2
) {
653 * Compare two character strings alphabetically. This is simply a wrapper
654 * around String.compareToIgnoreCase but that will handle cases where
655 * strings can be null
661 * @return A number below, equal, or greater than zero if the first string
662 * is smaller, equal, or bigger (alphabetically) than the second
665 static public int compare(String s1
, String s2
) {
666 if (s1
!= null && s2
!= null) {
667 return s1
.compareToIgnoreCase(s2
);