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 */
47 * Absolute timestamp (ie, relative to the Unix epoch)
53 * Timestamp displayed as a simple number
60 * Timestamp resolution
62 public static enum Resolution
{
76 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
77 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
79 static Rectangle
clone(Rectangle source
) {
80 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
84 * Initialize a Rectangle object to default values (all equal to 0)
87 * The Rectangle to initialize
89 static public void init(Rectangle rect
) {
97 * Initialize a Rectangle object with all the given values
100 * The Rectangle object to initialize
106 * The width of the rectangle
108 * The height of the rectangle
110 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
114 rect
.height
= height
;
118 * Initialize a Rectangle object to another existing Rectangle's values.
121 * The Rectangle to initialize
123 * The reference Rectangle to copy
125 static public void init(Rectangle rect
, Rectangle source
) {
128 rect
.width
= source
.width
;
129 rect
.height
= source
.height
;
133 * Reduce the size of a given rectangle by the given amounts.
136 * The rectangle to modify
138 * The reduction in width
140 * The reduction in height
142 static public void deflate(Rectangle rect
, int x
, int y
) {
146 rect
.height
-= y
+ y
;
150 * Increase the size of a given rectangle by the given amounts.
153 * The rectangle to modify
155 * The augmentation in width
157 * The augmentation in height
159 static public void inflate(Rectangle rect
, int x
, int y
) {
163 rect
.height
+= y
+ y
;
166 static void dispose(Color col
) {
173 * Get the resulting color from a mix of two existing ones for a given
177 * The display device (which might affect the color conversion)
183 * The gamma level for color 1
185 * The gamma level for color 2
186 * @return The resulting color
188 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
190 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
191 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
192 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
197 * Get the system color with the given ID.
201 * @return The resulting color
203 static public Color
getSysColor(int id
) {
204 Color col
= Display
.getCurrent().getSystemColor(id
);
205 return new Color(col
.getDevice(), col
.getRGB());
209 * Get the resulting color from a mix of two existing ones for the current
217 * The gamma level for color 1
219 * The gamma level for color 2
220 * @return The resulting color
222 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
223 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
227 * Draw text in a rectangle.
234 * The rectangle object which is being drawn
236 * Should we transpose the color
237 * @return The X coordinate where we have written
239 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
240 Point size
= gc
.stringExtent(text
);
241 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
246 * Draw text at a given location.
253 * The X coordinate of the starting point
255 * the Y coordinate of the starting point
257 * Should we transpose the color
258 * @return The X coordinate where we have written
260 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
261 Point size
= gc
.stringExtent(text
);
262 gc
.drawText(text
, x
, y
, transp
);
267 * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
272 * The string to be drawn
274 * The x coordinate of the top left corner of the rectangular area where the text is to be drawn
276 * The y coordinate of the top left corner of the rectangular area where the text is to be drawn
278 * The width of the area to be drawn
280 * If <code>true</code> the text will be centered in the available width if space permits
281 * @param isTransparent
282 * If <code>true</code> the background will be transparent, otherwise it will be opaque
283 * @return The number of characters written
287 static public int drawText(GC gc
, String text
, int x
, int y
, int width
, boolean isCentered
, boolean isTransparent
) {
288 int len
= text
.length();
290 boolean isReallyCentered
= isCentered
;
294 textWidth
= gc
.stringExtent(text
.substring(0, len
)).x
;
295 if (textWidth
<= width
) {
298 isReallyCentered
= false;
302 if (isReallyCentered
) {
303 realX
+= (width
- textWidth
) / 2;
305 gc
.drawText(text
.substring(0, len
), realX
, y
, isTransparent
);
311 * Formats time in format: MM:SS:NNN
314 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
315 * @param resolution the resolution
316 * @return the formatted time
318 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
319 // if format is absolute (Calendar)
320 if (format
== TimeFormat
.CALENDAR
) {
321 return formatTimeAbs(time
, resolution
);
322 } else if (format
== TimeFormat
.NUMBER
) {
323 return NumberFormat
.getInstance().format(time
);
326 StringBuffer str
= new StringBuffer();
334 long sec
= (long) (t
* 1E-9);
335 // TODO: Expand to make it possible to select the minute, second, nanosecond format
336 //printing minutes is suppressed just sec and ns
337 // if (sec / 60 < 10)
339 // str.append(sec / 60);
345 String ns
= formatNs(t
, resolution
);
346 if (!ns
.equals("")) { //$NON-NLS-1$
351 return str
.toString();
355 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
358 * The source time, in ns
359 * @return the formatted date
361 public static String
formatDate(long absTime
) {
362 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
367 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
370 * The source time, in ns
372 * The resolution to use
373 * @return the formatted time
375 static public String
formatTimeAbs(long time
, Resolution res
) {
376 StringBuffer str
= new StringBuffer();
378 // format time from nanoseconds to calendar time HH:MM:SS
379 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
382 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
384 str
.append(formatNs(time
, res
));
385 return str
.toString();
389 * Obtains the remainder fraction on unit Seconds of the entered value in
390 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
391 * seconds can be obtained by removing the last 9 digits: 1241207054 the
392 * fractional portion of seconds, expressed in ns is: 171080214
395 * The source time in ns
397 * The Resolution to use
398 * @return the formatted nanosec
400 public static String
formatNs(long srcTime
, Resolution res
) {
401 StringBuffer str
= new StringBuffer();
403 boolean neg
= time
< 0;
408 // The following approach could be used although performance
409 // decreases in half.
410 // String strVal = String.format("%09d", time);
411 // String tmp = strVal.substring(strVal.length() - 9);
416 str
.append("00000000"); //$NON-NLS-1$
417 } else if (ns
< 100) {
418 str
.append("0000000"); //$NON-NLS-1$
419 } else if (ns
< 1000) {
420 str
.append("000000"); //$NON-NLS-1$
421 } else if (ns
< 10000) {
422 str
.append("00000"); //$NON-NLS-1$
423 } else if (ns
< 100000) {
424 str
.append("0000"); //$NON-NLS-1$
425 } else if (ns
< 1000000) {
426 str
.append("000"); //$NON-NLS-1$
427 } else if (ns
< 10000000) {
428 str
.append("00"); //$NON-NLS-1$
429 } else if (ns
< 100000000) {
430 str
.append("0"); //$NON-NLS-1$
434 if (res
== Resolution
.MILLISEC
) {
435 return str
.substring(0, 3);
436 } else if (res
== Resolution
.MICROSEC
) {
437 return str
.substring(0, 6);
438 } else if (res
== Resolution
.NANOSEC
) {
439 return str
.substring(0, 9);
441 return ""; //$NON-NLS-1$
445 * FIXME Currently does nothing.
452 * The minimal accepted value
454 * The maximal accepted value
455 * @return The value that was read
457 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
459 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
470 * FIXME currently does nothing
477 static public void saveIntOption(String opt
, int val
) {
478 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
481 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
482 if (null == entry
|| ! entry
.hasTimeEvents()) {
485 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
486 if (iterator
!= null && iterator
.hasNext()) {
487 return iterator
.next();
493 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
494 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
502 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
503 if (null == entry
|| ! entry
.hasTimeEvents()) {
506 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
507 if (iterator
== null) {
510 ITimeEvent nextEvent
= null;
511 ITimeEvent currEvent
= null;
512 ITimeEvent prevEvent
= null;
514 while (iterator
.hasNext()) {
515 nextEvent
= iterator
.next();
516 long nextStartTime
= nextEvent
.getTime();
518 if (nextStartTime
> time
) {
522 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
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
);