1 /*****************************************************************************
2 * Copyright (c) 2007, 2013 Intel Corporation, 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
13 * Marc-Andre Laperle - Add time zone preference
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
;
22 import java
.util
.TimeZone
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimePreferences
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
27 import org
.eclipse
.swt
.graphics
.Color
;
28 import org
.eclipse
.swt
.graphics
.Device
;
29 import org
.eclipse
.swt
.graphics
.GC
;
30 import org
.eclipse
.swt
.graphics
.Point
;
31 import org
.eclipse
.swt
.graphics
.Rectangle
;
32 import org
.eclipse
.swt
.widgets
.Display
;
35 * General utilities and definitions used by the time graph widget
38 * @author Alvaro Sanchez-Leon
39 * @author Patrick Tasse
46 /** Time format for dates and timestamp */
47 public enum TimeFormat
{
48 /** Relative to the start of the trace */
52 * Absolute timestamp (ie, relative to the Unix epoch)
58 * Timestamp displayed as a simple number
65 * Timestamp resolution
67 public static enum Resolution
{
81 private static final SimpleDateFormat TIME_FORMAT
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
82 private static final SimpleDateFormat DATE_FORMAT
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
83 private static final long SEC_IN_NS
= 1000000000;
84 private static final long MILLISEC_IN_NS
= 1000000;
87 * Update the time and date formats to use the current time zone
91 public static void updateTimeZone() {
92 TimeZone timeZone
= TmfTimePreferences
.getInstance().getTimeZone();
93 TIME_FORMAT
.setTimeZone(timeZone
);
94 DATE_FORMAT
.setTimeZone(timeZone
);
97 static Rectangle
clone(Rectangle source
) {
98 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
102 * Initialize a Rectangle object to default values (all equal to 0)
105 * The Rectangle to initialize
107 public static void init(Rectangle rect
) {
115 * Initialize a Rectangle object with all the given values
118 * The Rectangle object to initialize
124 * The width of the rectangle
126 * The height of the rectangle
128 public static void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
132 rect
.height
= height
;
136 * Initialize a Rectangle object to another existing Rectangle's values.
139 * The Rectangle to initialize
141 * The reference Rectangle to copy
143 public static void init(Rectangle rect
, Rectangle source
) {
146 rect
.width
= source
.width
;
147 rect
.height
= source
.height
;
151 * Reduce the size of a given rectangle by the given amounts.
154 * The rectangle to modify
156 * The reduction in width
158 * The reduction in height
160 public static void deflate(Rectangle rect
, int x
, int y
) {
164 rect
.height
-= y
+ y
;
168 * Increase the size of a given rectangle by the given amounts.
171 * The rectangle to modify
173 * The augmentation in width
175 * The augmentation in height
177 public static void inflate(Rectangle rect
, int x
, int y
) {
181 rect
.height
+= y
+ y
;
184 static void dispose(Color col
) {
191 * Get the resulting color from a mix of two existing ones for a given
195 * The display device (which might affect the color conversion)
201 * The gamma level for color 1
203 * The gamma level for color 2
204 * @return The resulting color
206 public static Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
208 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
209 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
210 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
215 * Get the system color with the given ID.
219 * @return The resulting color
221 public static Color
getSysColor(int id
) {
222 Color col
= Display
.getCurrent().getSystemColor(id
);
223 return new Color(col
.getDevice(), col
.getRGB());
227 * Get the resulting color from a mix of two existing ones for the current
235 * The gamma level for color 1
237 * The gamma level for color 2
238 * @return The resulting color
240 public static Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
241 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
245 * Draw text in a rectangle.
252 * The rectangle object which is being drawn
254 * Should we transpose the color
255 * @return The X coordinate where we have written
257 public static int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
258 Point size
= gc
.stringExtent(text
);
259 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
264 * Draw text at a given location.
271 * The X coordinate of the starting point
273 * the Y coordinate of the starting point
275 * Should we transpose the color
276 * @return The X coordinate where we have written
278 public static int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
279 Point size
= gc
.stringExtent(text
);
280 gc
.drawText(text
, x
, y
, transp
);
285 * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
290 * The string to be drawn
292 * The x coordinate of the top left corner of the rectangular area where the text is to be drawn
294 * The y coordinate of the top left corner of the rectangular area where the text is to be drawn
296 * The width of the area to be drawn
298 * If <code>true</code> the text will be centered in the available width if space permits
299 * @param isTransparent
300 * If <code>true</code> the background will be transparent, otherwise it will be opaque
301 * @return The number of characters written
305 public static int drawText(GC gc
, String text
, int x
, int y
, int width
, boolean isCentered
, boolean isTransparent
) {
306 int len
= text
.length();
308 boolean isReallyCentered
= isCentered
;
312 textWidth
= gc
.stringExtent(text
.substring(0, len
)).x
;
313 if (textWidth
<= width
) {
316 isReallyCentered
= false;
320 if (isReallyCentered
) {
321 realX
+= (width
- textWidth
) / 2;
323 gc
.drawText(text
.substring(0, len
), realX
, y
, isTransparent
);
329 * Formats time in format: MM:SS:NNN
332 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
333 * @param resolution the resolution
334 * @return the formatted time
336 public static String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
337 // if format is absolute (Calendar)
338 if (format
== TimeFormat
.CALENDAR
) {
339 return formatTimeAbs(time
, resolution
);
340 } else if (format
== TimeFormat
.NUMBER
) {
341 return NumberFormat
.getInstance().format(time
);
344 StringBuffer str
= new StringBuffer();
352 long sec
= t
/ SEC_IN_NS
;
354 String ns
= formatNs(t
, resolution
);
355 if (!ns
.equals("")) { //$NON-NLS-1$
360 return str
.toString();
364 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
367 * The source time, in ns
368 * @return the formatted date
370 public static String
formatDate(long absTime
) {
371 String sdate
= DATE_FORMAT
.format(new Date(absTime
/ MILLISEC_IN_NS
));
376 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
379 * The source time, in ns
381 * The resolution to use
382 * @return the formatted time
384 public static String
formatTimeAbs(long time
, Resolution res
) {
385 StringBuffer str
= new StringBuffer();
387 // format time from nanoseconds to calendar time HH:MM:SS
388 String stime
= TIME_FORMAT
.format(new Date(time
/ MILLISEC_IN_NS
));
391 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
393 str
.append(formatNs(time
, res
));
394 return str
.toString();
398 * Obtains the remainder fraction on unit Seconds of the entered value in
399 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
400 * seconds can be obtained by removing the last 9 digits: 1241207054 the
401 * fractional portion of seconds, expressed in ns is: 171080214
404 * The source time in ns
406 * The Resolution to use
407 * @return the formatted nanosec
409 public static String
formatNs(long srcTime
, Resolution res
) {
410 StringBuffer str
= new StringBuffer();
418 String nanos
= Long
.toString(ns
);
419 str
.append("000000000".substring(nanos
.length())); //$NON-NLS-1$
422 if (res
== Resolution
.MILLISEC
) {
423 return str
.substring(0, 3);
424 } else if (res
== Resolution
.MICROSEC
) {
425 return str
.substring(0, 6);
426 } else if (res
== Resolution
.NANOSEC
) {
427 return str
.substring(0, 9);
429 return ""; //$NON-NLS-1$
433 * FIXME Currently does nothing.
440 * The minimal accepted value
442 * The maximal accepted value
443 * @return The value that was read
445 public static int loadIntOption(String opt
, int def
, int min
, int max
) {
450 * FIXME currently does nothing
457 public static void saveIntOption(String opt
, int val
) {
460 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
461 if (null == entry
|| ! entry
.hasTimeEvents()) {
464 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
465 if (iterator
!= null && iterator
.hasNext()) {
466 return iterator
.next();
472 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
473 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
481 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
482 if (null == entry
|| ! entry
.hasTimeEvents()) {
485 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
486 if (iterator
== null) {
489 ITimeEvent nextEvent
= null;
490 ITimeEvent currEvent
= null;
491 ITimeEvent prevEvent
= null;
493 while (iterator
.hasNext()) {
494 nextEvent
= iterator
.next();
495 long nextStartTime
= nextEvent
.getTime();
497 if (nextStartTime
> time
) {
501 if (currEvent
== null || currEvent
.getTime() != nextStartTime
||
502 (nextStartTime
!= time
&& currEvent
.getDuration() != nextEvent
.getDuration())) {
503 prevEvent
= currEvent
;
504 currEvent
= nextEvent
;
508 if (n
== -1) { //previous
509 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
513 } else if (n
== 0) { //current
514 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
518 } else if (n
== 1) { //next
519 if (nextEvent
!= null && nextEvent
.getTime() > time
) {
523 } else if (n
== 2) { //current or previous when in empty space
531 * Pretty-print a method signature.
534 * The original signature
535 * @return The pretty signature
537 public static String
fixMethodSignature(String origSig
) {
538 String sig
= origSig
;
539 int pos
= sig
.indexOf('(');
541 String ret
= sig
.substring(0, pos
);
542 sig
= sig
.substring(pos
);
543 sig
= sig
+ " " + ret
; //$NON-NLS-1$
549 * Restore an original method signature from a pretty-printed one.
552 * The pretty-printed signature
553 * @return The original method signature
555 public static String
restoreMethodSignature(String ppSig
) {
556 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 public static String
getTypeSignature(String typeStr
) {
591 String type
= typeStr
;
592 for (int j
= 0; j
< type
.length(); j
++) {
593 if (type
.charAt(j
) == '[') {
597 int pos
= type
.indexOf('[');
599 type
= type
.substring(0, pos
);
601 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
602 for (int j
= 0; j
< dim
; j
++)
604 sig
.append("["); //$NON-NLS-1$
606 if (type
.equals("boolean")) { //$NON-NLS-1$
608 } else if (type
.equals("byte")) { //$NON-NLS-1$
610 } else if (type
.equals("char")) { //$NON-NLS-1$
612 } else if (type
.equals("short")) { //$NON-NLS-1$
614 } else if (type
.equals("int")) { //$NON-NLS-1$
616 } else if (type
.equals("long")) { //$NON-NLS-1$
618 } else if (type
.equals("float")) { //$NON-NLS-1$
620 } else if (type
.equals("double")) { //$NON-NLS-1$
622 } else if (type
.equals("void")) { //$NON-NLS-1$
626 sig
.append('L').append(type
.replace('.', '/')).append(';');
628 return sig
.toString();
632 * Compare two doubles together.
638 * @return 1 if they are different, and 0 if they are *exactly* the same.
639 * Because of the way doubles are stored, it's possible for the
640 * same number obtained in two different ways to actually look
643 public static int compare(double d1
, double d2
) {
654 * Compare two character strings alphabetically. This is simply a wrapper
655 * around String.compareToIgnoreCase but that will handle cases where
656 * strings can be null
662 * @return A number below, equal, or greater than zero if the first string
663 * is smaller, equal, or bigger (alphabetically) than the second
666 public static int compare(String s1
, String s2
) {
667 if (s1
!= null && s2
!= null) {
668 return s1
.compareToIgnoreCase(s2
);