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 * Formats time in format: MM:SS:NNN
255 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
256 * @param resolution the resolution
257 * @return the formatted time
259 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
260 // if format is absolute (Calendar)
261 if (format
== TimeFormat
.ABSOLUTE
) {
262 return formatTimeAbs(time
, resolution
);
265 StringBuffer str
= new StringBuffer();
266 boolean neg
= time
< 0;
272 long sec
= (long) (time
* 1E-9);
273 // TODO: Expand to make it possible to select the minute, second, nanosecond format
274 //printing minutes is suppressed just sec and ns
275 // if (sec / 60 < 10)
277 // str.append(sec / 60);
283 String ns
= formatNs(time
, resolution
);
284 if (!ns
.equals("")) { //$NON-NLS-1$
289 return str
.toString();
293 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
296 * The source time, in ns
297 * @return the formatted date
299 public static String
formatDate(long absTime
) {
300 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
305 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
308 * The source time, in ns
310 * The resolution to use
311 * @return the formatted time
313 static public String
formatTimeAbs(long time
, Resolution res
) {
314 StringBuffer str
= new StringBuffer();
316 // format time from nanoseconds to calendar time HH:MM:SS
317 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
320 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
322 str
.append(formatNs(time
, res
));
323 return str
.toString();
327 * Obtains the remainder fraction on unit Seconds of the entered value in
328 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
329 * seconds can be obtained by removing the last 9 digits: 1241207054 the
330 * fractional portion of seconds, expressed in ns is: 171080214
333 * The source time in ns
335 * The Resolution to use
336 * @return the formatted nanosec
338 public static String
formatNs(long time
, Resolution res
) {
339 StringBuffer str
= new StringBuffer();
340 boolean neg
= time
< 0;
345 // The following approach could be used although performance
346 // decreases in half.
347 // String strVal = String.format("%09d", time);
348 // String tmp = strVal.substring(strVal.length() - 9);
353 str
.append("00000000"); //$NON-NLS-1$
354 } else if (ns
< 100) {
355 str
.append("0000000"); //$NON-NLS-1$
356 } else if (ns
< 1000) {
357 str
.append("000000"); //$NON-NLS-1$
358 } else if (ns
< 10000) {
359 str
.append("00000"); //$NON-NLS-1$
360 } else if (ns
< 100000) {
361 str
.append("0000"); //$NON-NLS-1$
362 } else if (ns
< 1000000) {
363 str
.append("000"); //$NON-NLS-1$
364 } else if (ns
< 10000000) {
365 str
.append("00"); //$NON-NLS-1$
366 } else if (ns
< 100000000) {
367 str
.append("0"); //$NON-NLS-1$
371 if (res
== Resolution
.MILLISEC
) {
372 return str
.substring(0, 3);
373 } else if (res
== Resolution
.MICROSEC
) {
374 return str
.substring(0, 6);
375 } else if (res
== Resolution
.NANOSEC
) {
376 return str
.substring(0, 9);
378 return ""; //$NON-NLS-1$
382 * FIXME Currently does nothing.
389 * The minimal accepted value
391 * The maximal accepted value
392 * @return The value that was read
394 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
396 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
407 * FIXME currently does nothing
414 static public void saveIntOption(String opt
, int val
) {
415 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
418 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
419 if (null == entry
|| ! entry
.hasTimeEvents()) {
422 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
423 if (iterator
!= null && iterator
.hasNext()) {
424 return iterator
.next();
431 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
432 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
440 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
441 if (null == entry
|| ! entry
.hasTimeEvents()) {
444 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
445 if (iterator
== null) {
448 ITimeEvent nextEvent
= null;
449 ITimeEvent currEvent
= null;
450 ITimeEvent prevEvent
= null;
452 while (iterator
.hasNext()) {
453 nextEvent
= (ITimeEvent
) iterator
.next();
454 long nextStartTime
= nextEvent
.getTime();
456 if (nextStartTime
> time
) {
460 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
461 prevEvent
= currEvent
;
462 currEvent
= nextEvent
;
466 if (n
== -1) { //previous
467 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
472 } else if (n
== 0) { //current
473 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
478 } else if (n
== 1) { //next
480 } else if (n
== 2) { //current or previous when in empty space
488 * Pretty-print a method signature.
491 * The original signature
492 * @return The pretty signature
494 static public String
fixMethodSignature(String sig
) {
495 int pos
= sig
.indexOf('(');
497 String ret
= sig
.substring(0, pos
);
498 sig
= sig
.substring(pos
);
499 sig
= sig
+ " " + ret
; //$NON-NLS-1$
505 * Restore an original method signature from a pretty-printed one.
508 * The pretty-printed signature
509 * @return The original method signature
511 static public String
restoreMethodSignature(String sig
) {
512 String ret
= ""; //$NON-NLS-1$
513 int pos
= sig
.indexOf('(');
515 ret
= sig
.substring(0, pos
);
516 sig
= sig
.substring(pos
+ 1);
518 pos
= sig
.indexOf(')');
520 sig
= sig
.substring(0, pos
);
522 String args
[] = sig
.split(","); //$NON-NLS-1$
523 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
524 for (int i
= 0; i
< args
.length
; i
++) {
525 String arg
= args
[i
].trim();
526 if (arg
.length() == 0 && args
.length
== 1) {
529 result
.append(getTypeSignature(arg
));
531 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
532 return result
.toString();
536 * Get the mangled type information from an array of types.
539 * The types to convert. See method implementation for what it
541 * @return The mangled string of types
543 @SuppressWarnings("nls")
544 static public String
getTypeSignature(String type
) {
546 for (int j
= 0; j
< type
.length(); j
++) {
547 if (type
.charAt(j
) == '[') {
551 int pos
= type
.indexOf('[');
553 type
= type
.substring(0, pos
);
555 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
556 for (int j
= 0; j
< dim
; j
++)
558 sig
.append("["); //$NON-NLS-1$
560 if (type
.equals("boolean")) {
561 sig
.append("Z"); //$NON-NLS-1$
562 } else if (type
.equals("byte")) {
563 sig
.append("B"); //$NON-NLS-1$
564 } else if (type
.equals("char")) {
565 sig
.append("C"); //$NON-NLS-1$
566 } else if (type
.equals("short")) {
567 sig
.append("S"); //$NON-NLS-1$
568 } else if (type
.equals("int")) {
569 sig
.append("I"); //$NON-NLS-1$
570 } else if (type
.equals("long")) {
571 sig
.append("J"); //$NON-NLS-1$
572 } else if (type
.equals("float")) {
573 sig
.append("F"); //$NON-NLS-1$
574 } else if (type
.equals("double")) {
575 sig
.append("D"); //$NON-NLS-1$
576 } else if (type
.equals("void")) {
577 sig
.append("V"); //$NON-NLS-1$
580 sig
.append("L").append(type
.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
582 return sig
.toString();
586 * Compare two doubles together.
592 * @return 1 if they are different, and 0 if they are *exactly* the same.
593 * Because of the way doubles are stored, it's possible for the
594 * same number obtained in two different ways to actually look
597 static public int compare(double d1
, double d2
) {
608 * Compare two character strings alphabetically. This is simply a wrapper
609 * around String.compareToIgnoreCase but that will handle cases where
610 * strings can be null
616 * @return A number below, equal, or greater than zero if the first string
617 * is smaller, equal, or bigger (alphabetically) than the second
620 static public int compare(String s1
, String s2
) {
621 if (s1
!= null && s2
!= null) {
622 return s1
.compareToIgnoreCase(s2
);