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 *****************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
17 import java
.text
.NumberFormat
;
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 */
46 * Absolute timestamp (ie, relative to the Unix epoch)
52 * Timestamp displayed as a simple number
59 * Timestamp resolution
61 public static enum Resolution
{
75 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
76 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
78 static Rectangle
clone(Rectangle source
) {
79 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
83 * Initialize a Rectangle object to default values (all equal to 0)
86 * The Rectangle to initialize
88 static public void init(Rectangle rect
) {
96 * Initialize a Rectangle object with all the given values
99 * The Rectangle object to initialize
105 * The width of the rectangle
107 * The height of the rectangle
109 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
113 rect
.height
= height
;
117 * Initialize a Rectangle object to another existing Rectangle's values.
120 * The Rectangle to initialize
122 * The reference Rectangle to copy
124 static public void init(Rectangle rect
, Rectangle source
) {
127 rect
.width
= source
.width
;
128 rect
.height
= source
.height
;
132 * Reduce the size of a given rectangle by the given amounts.
135 * The rectangle to modify
137 * The reduction in width
139 * The reduction in height
141 static public void deflate(Rectangle rect
, int x
, int y
) {
145 rect
.height
-= y
+ y
;
149 * Increase the size of a given rectangle by the given amounts.
152 * The rectangle to modify
154 * The augmentation in width
156 * The augmentation in height
158 static public void inflate(Rectangle rect
, int x
, int y
) {
162 rect
.height
+= y
+ y
;
165 static void dispose(Color col
) {
172 * Get the resulting color from a mix of two existing ones for a given
176 * The display device (which might affect the color conversion)
182 * The gamma level for color 1
184 * The gamma level for color 2
185 * @return The resulting color
187 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
189 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
190 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
191 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
196 * Get the system color with the given ID.
200 * @return The resulting color
202 static public Color
getSysColor(int id
) {
203 Color col
= Display
.getCurrent().getSystemColor(id
);
204 return new Color(col
.getDevice(), col
.getRGB());
208 * Get the resulting color from a mix of two existing ones for the current
216 * The gamma level for color 1
218 * The gamma level for color 2
219 * @return The resulting color
221 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
222 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
226 * Draw text in a rectangle.
233 * The rectangle object which is being drawn
235 * Should we transpose the color
236 * @return The X coordinate where we have written
238 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
239 Point size
= gc
.stringExtent(text
);
240 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
245 * Draw text at a given location.
252 * The X coordinate of the starting point
254 * the Y coordinate of the starting point
256 * Should we transpose the color
257 * @return The X coordinate where we have written
259 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
260 Point size
= gc
.stringExtent(text
);
261 gc
.drawText(text
, x
, y
, transp
);
266 * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
271 * The string to be drawn
273 * The x coordinate of the top left corner of the rectangular area where the text is to be drawn
275 * The y coordinate of the top left corner of the rectangular area where the text is to be drawn
277 * The width of the area to be drawn
279 * If <code>true</code> the text will be centered in the available width if space permits
280 * @param isTransparent
281 * If <code>true</code> the background will be transparent, otherwise it will be opaque
282 * @return The number of characters written
286 static public int drawText(GC gc
, String text
, int x
, int y
, int width
, boolean isCentered
, boolean isTransparent
) {
287 int len
= text
.length();
289 boolean isReallyCentered
= isCentered
;
293 textWidth
= gc
.stringExtent(text
.substring(0, len
)).x
;
294 if (textWidth
<= width
) {
297 isReallyCentered
= false;
301 if (isReallyCentered
) {
302 realX
+= (width
- textWidth
) / 2;
304 gc
.drawText(text
.substring(0, len
), realX
, y
, isTransparent
);
310 * Formats time in format: MM:SS:NNN
313 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
314 * @param resolution the resolution
315 * @return the formatted time
317 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
318 // if format is absolute (Calendar)
319 if (format
== TimeFormat
.CALENDAR
) {
320 return formatTimeAbs(time
, resolution
);
321 } else if (format
== TimeFormat
.NUMBER
) {
322 return NumberFormat
.getInstance().format(time
);
325 StringBuffer str
= new StringBuffer();
333 long sec
= (long) (t
* 1E-9);
334 // TODO: Expand to make it possible to select the minute, second, nanosecond format
335 //printing minutes is suppressed just sec and ns
336 // if (sec / 60 < 10)
338 // str.append(sec / 60);
344 String ns
= formatNs(t
, resolution
);
345 if (!ns
.equals("")) { //$NON-NLS-1$
350 return str
.toString();
354 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
357 * The source time, in ns
358 * @return the formatted date
360 public static String
formatDate(long absTime
) {
361 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
366 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
369 * The source time, in ns
371 * The resolution to use
372 * @return the formatted time
374 static public String
formatTimeAbs(long time
, Resolution res
) {
375 StringBuffer str
= new StringBuffer();
377 // format time from nanoseconds to calendar time HH:MM:SS
378 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
381 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
383 str
.append(formatNs(time
, res
));
384 return str
.toString();
388 * Obtains the remainder fraction on unit Seconds of the entered value in
389 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
390 * seconds can be obtained by removing the last 9 digits: 1241207054 the
391 * fractional portion of seconds, expressed in ns is: 171080214
394 * The source time in ns
396 * The Resolution to use
397 * @return the formatted nanosec
399 public static String
formatNs(long srcTime
, Resolution res
) {
400 StringBuffer str
= new StringBuffer();
402 boolean neg
= time
< 0;
407 // The following approach could be used although performance
408 // decreases in half.
409 // String strVal = String.format("%09d", time);
410 // String tmp = strVal.substring(strVal.length() - 9);
415 str
.append("00000000"); //$NON-NLS-1$
416 } else if (ns
< 100) {
417 str
.append("0000000"); //$NON-NLS-1$
418 } else if (ns
< 1000) {
419 str
.append("000000"); //$NON-NLS-1$
420 } else if (ns
< 10000) {
421 str
.append("00000"); //$NON-NLS-1$
422 } else if (ns
< 100000) {
423 str
.append("0000"); //$NON-NLS-1$
424 } else if (ns
< 1000000) {
425 str
.append("000"); //$NON-NLS-1$
426 } else if (ns
< 10000000) {
427 str
.append("00"); //$NON-NLS-1$
428 } else if (ns
< 100000000) {
429 str
.append("0"); //$NON-NLS-1$
433 if (res
== Resolution
.MILLISEC
) {
434 return str
.substring(0, 3);
435 } else if (res
== Resolution
.MICROSEC
) {
436 return str
.substring(0, 6);
437 } else if (res
== Resolution
.NANOSEC
) {
438 return str
.substring(0, 9);
440 return ""; //$NON-NLS-1$
444 * FIXME Currently does nothing.
451 * The minimal accepted value
453 * The maximal accepted value
454 * @return The value that was read
456 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
458 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
469 * FIXME currently does nothing
476 static public void saveIntOption(String opt
, int val
) {
477 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
480 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
481 if (null == entry
|| ! entry
.hasTimeEvents()) {
484 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
485 if (iterator
!= null && iterator
.hasNext()) {
486 return iterator
.next();
492 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
493 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
501 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
502 if (null == entry
|| ! entry
.hasTimeEvents()) {
505 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
506 if (iterator
== null) {
509 ITimeEvent nextEvent
= null;
510 ITimeEvent currEvent
= null;
511 ITimeEvent prevEvent
= null;
513 while (iterator
.hasNext()) {
514 nextEvent
= iterator
.next();
515 long nextStartTime
= nextEvent
.getTime();
517 if (nextStartTime
> time
) {
521 if (currEvent
== null || currEvent
.getTime() != nextStartTime
||
522 (nextStartTime
!= time
&& currEvent
.getDuration() != nextEvent
.getDuration())) {
523 prevEvent
= currEvent
;
524 currEvent
= nextEvent
;
528 if (n
== -1) { //previous
529 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
533 } else if (n
== 0) { //current
534 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
538 } else if (n
== 1) { //next
539 if (nextEvent
!= null && nextEvent
.getTime() > time
) {
543 } else if (n
== 2) { //current or previous when in empty space
551 * Pretty-print a method signature.
554 * The original signature
555 * @return The pretty signature
557 static public String
fixMethodSignature(String origSig
) {
558 String sig
= origSig
;
559 int pos
= sig
.indexOf('(');
561 String ret
= sig
.substring(0, pos
);
562 sig
= sig
.substring(pos
);
563 sig
= sig
+ " " + ret
; //$NON-NLS-1$
569 * Restore an original method signature from a pretty-printed one.
572 * The pretty-printed signature
573 * @return The original method signature
575 static public String
restoreMethodSignature(String ppSig
) {
576 String ret
= ""; //$NON-NLS-1$
579 int pos
= sig
.indexOf('(');
581 ret
= sig
.substring(0, pos
);
582 sig
= sig
.substring(pos
+ 1);
584 pos
= sig
.indexOf(')');
586 sig
= sig
.substring(0, pos
);
588 String args
[] = sig
.split(","); //$NON-NLS-1$
589 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
590 for (int i
= 0; i
< args
.length
; i
++) {
591 String arg
= args
[i
].trim();
592 if (arg
.length() == 0 && args
.length
== 1) {
595 result
.append(getTypeSignature(arg
));
597 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
598 return result
.toString();
602 * Get the mangled type information from an array of types.
605 * The types to convert. See method implementation for what it
607 * @return The mangled string of types
609 public static String
getTypeSignature(String typeStr
) {
611 String type
= typeStr
;
612 for (int j
= 0; j
< type
.length(); j
++) {
613 if (type
.charAt(j
) == '[') {
617 int pos
= type
.indexOf('[');
619 type
= type
.substring(0, pos
);
621 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
622 for (int j
= 0; j
< dim
; j
++)
624 sig
.append("["); //$NON-NLS-1$
626 if (type
.equals("boolean")) { //$NON-NLS-1$
628 } else if (type
.equals("byte")) { //$NON-NLS-1$
630 } else if (type
.equals("char")) { //$NON-NLS-1$
632 } else if (type
.equals("short")) { //$NON-NLS-1$
634 } else if (type
.equals("int")) { //$NON-NLS-1$
636 } else if (type
.equals("long")) { //$NON-NLS-1$
638 } else if (type
.equals("float")) { //$NON-NLS-1$
640 } else if (type
.equals("double")) { //$NON-NLS-1$
642 } else if (type
.equals("void")) { //$NON-NLS-1$
646 sig
.append('L').append(type
.replace('.', '/')).append(';');
648 return sig
.toString();
652 * Compare two doubles together.
658 * @return 1 if they are different, and 0 if they are *exactly* the same.
659 * Because of the way doubles are stored, it's possible for the
660 * same number obtained in two different ways to actually look
663 static public int compare(double d1
, double d2
) {
674 * Compare two character strings alphabetically. This is simply a wrapper
675 * around String.compareToIgnoreCase but that will handle cases where
676 * strings can be null
682 * @return A number below, equal, or greater than zero if the first string
683 * is smaller, equal, or bigger (alphabetically) than the second
686 static public int compare(String s1
, String s2
) {
687 if (s1
!= null && s2
!= null) {
688 return s1
.compareToIgnoreCase(s2
);