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
.SimpleDateFormat
;
19 import java
.util
.Date
;
20 import java
.util
.Iterator
;
22 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
24 import org
.eclipse
.swt
.graphics
.Color
;
25 import org
.eclipse
.swt
.graphics
.Device
;
26 import org
.eclipse
.swt
.graphics
.GC
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.graphics
.Rectangle
;
29 import org
.eclipse
.swt
.widgets
.Display
;
32 * General utilities and definitions used by the time graph widget
35 * @author Alvaro Sanchez-Leon
36 * @author Patrick Tasse
40 /** Time format for dates and timestamp */
41 public enum TimeFormat
{
42 /** Relative to the start of the trace */
44 /** Absolute timestamp (ie, relative to the Unix epoch) */
48 static public final int IMG_THREAD_RUNNING
= 0;
49 static public final int IMG_THREAD_SUSPENDED
= 1;
50 static public final int IMG_THREAD_STOPPED
= 2;
51 static public final int IMG_METHOD_RUNNING
= 3;
52 static public final int IMG_METHOD
= 4;
53 static public final int IMG_NUM
= 5;
55 static public final Object
[] _empty
= new Object
[0];
57 public static enum Resolution
{
58 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
61 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
62 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
64 static Rectangle
clone(Rectangle source
) {
65 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
69 * Initialize a Rectangle object to default values (all equal to 0)
72 * The Rectangle to initialize
74 static public void init(Rectangle rect
) {
82 * Initialize a Rectangle object with all the given values
85 * The Rectangle object to initialize
91 * The width of the rectangle
93 * The height of the rectangle
95 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
103 * Initialize a Rectangle object to another existing Rectangle's values.
106 * The Rectangle to initialize
108 * The reference Rectangle to copy
110 static public void init(Rectangle rect
, Rectangle source
) {
113 rect
.width
= source
.width
;
114 rect
.height
= source
.height
;
118 * Reduce the size of a given rectangle by the given amounts.
121 * The rectangle to modify
123 * The reduction in width
125 * The reduction in height
127 static public void deflate(Rectangle rect
, int x
, int y
) {
131 rect
.height
-= y
+ y
;
135 * Increase the size of a given rectangle by the given amounts.
138 * The rectangle to modify
140 * The augmentation in width
142 * The augmentation in height
144 static public void inflate(Rectangle rect
, int x
, int y
) {
148 rect
.height
+= y
+ y
;
151 static void dispose(Color col
) {
158 * Get the resulting color from a mix of two existing ones for a given
162 * The display device (which might affect the color conversion)
168 * The gamma level for color 1
170 * The gamma level for color 2
171 * @return The resulting color
173 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
175 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
176 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
177 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
182 * Get the system color with the given ID.
186 * @return The resulting color
188 static public Color
getSysColor(int id
) {
189 Color col
= Display
.getCurrent().getSystemColor(id
);
190 return new Color(col
.getDevice(), col
.getRGB());
194 * Get the resulting color from a mix of two existing ones for the current
202 * The gamma level for color 1
204 * The gamma level for color 2
205 * @return The resulting color
207 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
208 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
212 * Draw text in a rectangle.
219 * The rectangle object which is being drawn
221 * Should we transpose the color
222 * @return The X coordinate where we have written
224 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
225 Point size
= gc
.stringExtent(text
);
226 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
231 * Draw text at a given location.
238 * The X coordinate of the starting point
240 * the Y coordinate of the starting point
242 * Should we transpose the color
243 * @return The X coordinate where we have written
245 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
246 Point size
= gc
.stringExtent(text
);
247 gc
.drawText(text
, x
, y
, transp
);
252 * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
257 * The string to be drawn
259 * The x coordinate of the top left corner of the rectangular area where the text is to be drawn
261 * The y coordinate of the top left corner of the rectangular area where the text is to be drawn
263 * The width of the area to be drawn
265 * If <code>true</code> the text will be centered in the available width if space permits
266 * @param isTransparent
267 * If <code>true</code> the background will be transparent, otherwise it will be opaque
268 * @return The number of characters written
272 static public int drawText(GC gc
, String text
, int x
, int y
, int width
, boolean isCentered
, boolean isTransparent
) {
273 int len
= text
.length();
276 textWidth
= gc
.stringExtent(text
.substring(0, len
)).x
;
277 if (textWidth
<= width
) {
285 x
+= (width
- textWidth
) / 2;
287 gc
.drawText(text
.substring(0, len
), x
, y
, isTransparent
);
293 * Formats time in format: MM:SS:NNN
296 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
297 * @param resolution the resolution
298 * @return the formatted time
300 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
301 // if format is absolute (Calendar)
302 if (format
== TimeFormat
.ABSOLUTE
) {
303 return formatTimeAbs(time
, resolution
);
306 StringBuffer str
= new StringBuffer();
307 boolean neg
= time
< 0;
313 long sec
= (long) (time
* 1E-9);
314 // TODO: Expand to make it possible to select the minute, second, nanosecond format
315 //printing minutes is suppressed just sec and ns
316 // if (sec / 60 < 10)
318 // str.append(sec / 60);
324 String ns
= formatNs(time
, resolution
);
325 if (!ns
.equals("")) { //$NON-NLS-1$
330 return str
.toString();
334 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
337 * The source time, in ns
338 * @return the formatted date
340 public static String
formatDate(long absTime
) {
341 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
346 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
349 * The source time, in ns
351 * The resolution to use
352 * @return the formatted time
354 static public String
formatTimeAbs(long time
, Resolution res
) {
355 StringBuffer str
= new StringBuffer();
357 // format time from nanoseconds to calendar time HH:MM:SS
358 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
361 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
363 str
.append(formatNs(time
, res
));
364 return str
.toString();
368 * Obtains the remainder fraction on unit Seconds of the entered value in
369 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
370 * seconds can be obtained by removing the last 9 digits: 1241207054 the
371 * fractional portion of seconds, expressed in ns is: 171080214
374 * The source time in ns
376 * The Resolution to use
377 * @return the formatted nanosec
379 public static String
formatNs(long time
, Resolution res
) {
380 StringBuffer str
= new StringBuffer();
381 boolean neg
= time
< 0;
386 // The following approach could be used although performance
387 // decreases in half.
388 // String strVal = String.format("%09d", time);
389 // String tmp = strVal.substring(strVal.length() - 9);
394 str
.append("00000000"); //$NON-NLS-1$
395 } else if (ns
< 100) {
396 str
.append("0000000"); //$NON-NLS-1$
397 } else if (ns
< 1000) {
398 str
.append("000000"); //$NON-NLS-1$
399 } else if (ns
< 10000) {
400 str
.append("00000"); //$NON-NLS-1$
401 } else if (ns
< 100000) {
402 str
.append("0000"); //$NON-NLS-1$
403 } else if (ns
< 1000000) {
404 str
.append("000"); //$NON-NLS-1$
405 } else if (ns
< 10000000) {
406 str
.append("00"); //$NON-NLS-1$
407 } else if (ns
< 100000000) {
408 str
.append("0"); //$NON-NLS-1$
412 if (res
== Resolution
.MILLISEC
) {
413 return str
.substring(0, 3);
414 } else if (res
== Resolution
.MICROSEC
) {
415 return str
.substring(0, 6);
416 } else if (res
== Resolution
.NANOSEC
) {
417 return str
.substring(0, 9);
419 return ""; //$NON-NLS-1$
423 * FIXME Currently does nothing.
430 * The minimal accepted value
432 * The maximal accepted value
433 * @return The value that was read
435 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
437 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
448 * FIXME currently does nothing
455 static public void saveIntOption(String opt
, int val
) {
456 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
459 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
460 if (null == entry
|| ! entry
.hasTimeEvents()) {
463 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
464 if (iterator
!= null && iterator
.hasNext()) {
465 return iterator
.next();
471 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
472 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
480 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
481 if (null == entry
|| ! entry
.hasTimeEvents()) {
484 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
485 if (iterator
== null) {
488 ITimeEvent nextEvent
= null;
489 ITimeEvent currEvent
= null;
490 ITimeEvent prevEvent
= null;
492 while (iterator
.hasNext()) {
493 nextEvent
= iterator
.next();
494 long nextStartTime
= nextEvent
.getTime();
496 if (nextStartTime
> time
) {
500 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
501 prevEvent
= currEvent
;
502 currEvent
= nextEvent
;
506 if (n
== -1) { //previous
507 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
511 } else if (n
== 0) { //current
512 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
516 } else if (n
== 1) { //next
517 if (nextEvent
!= null && nextEvent
.getTime() > time
) {
521 } else if (n
== 2) { //current or previous when in empty space
529 * Pretty-print a method signature.
532 * The original signature
533 * @return The pretty signature
535 static public String
fixMethodSignature(String sig
) {
536 int pos
= sig
.indexOf('(');
538 String ret
= sig
.substring(0, pos
);
539 sig
= sig
.substring(pos
);
540 sig
= sig
+ " " + ret
; //$NON-NLS-1$
546 * Restore an original method signature from a pretty-printed one.
549 * The pretty-printed signature
550 * @return The original method signature
552 static public String
restoreMethodSignature(String sig
) {
553 String ret
= ""; //$NON-NLS-1$
554 int pos
= sig
.indexOf('(');
556 ret
= sig
.substring(0, pos
);
557 sig
= sig
.substring(pos
+ 1);
559 pos
= sig
.indexOf(')');
561 sig
= sig
.substring(0, pos
);
563 String args
[] = sig
.split(","); //$NON-NLS-1$
564 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
565 for (int i
= 0; i
< args
.length
; i
++) {
566 String arg
= args
[i
].trim();
567 if (arg
.length() == 0 && args
.length
== 1) {
570 result
.append(getTypeSignature(arg
));
572 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
573 return result
.toString();
577 * Get the mangled type information from an array of types.
580 * The types to convert. See method implementation for what it
582 * @return The mangled string of types
584 static public String
getTypeSignature(String type
) {
586 for (int j
= 0; j
< type
.length(); j
++) {
587 if (type
.charAt(j
) == '[') {
591 int pos
= type
.indexOf('[');
593 type
= type
.substring(0, pos
);
595 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
596 for (int j
= 0; j
< dim
; j
++)
598 sig
.append("["); //$NON-NLS-1$
600 if (type
.equals("boolean")) { //$NON-NLS-1$
602 } else if (type
.equals("byte")) { //$NON-NLS-1$
604 } else if (type
.equals("char")) { //$NON-NLS-1$
606 } else if (type
.equals("short")) { //$NON-NLS-1$
608 } else if (type
.equals("int")) { //$NON-NLS-1$
610 } else if (type
.equals("long")) { //$NON-NLS-1$
612 } else if (type
.equals("float")) { //$NON-NLS-1$
614 } else if (type
.equals("double")) { //$NON-NLS-1$
616 } else if (type
.equals("void")) { //$NON-NLS-1$
620 sig
.append('L').append(type
.replace('.', '/')).append(';');
622 return sig
.toString();
626 * Compare two doubles together.
632 * @return 1 if they are different, and 0 if they are *exactly* the same.
633 * Because of the way doubles are stored, it's possible for the
634 * same number obtained in two different ways to actually look
637 static public int compare(double d1
, double d2
) {
648 * Compare two character strings alphabetically. This is simply a wrapper
649 * around String.compareToIgnoreCase but that will handle cases where
650 * strings can be null
656 * @return A number below, equal, or greater than zero if the first string
657 * is smaller, equal, or bigger (alphabetically) than the second
660 static public int compare(String s1
, String s2
) {
661 if (s1
!= null && s2
!= null) {
662 return s1
.compareToIgnoreCase(s2
);