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 public enum TimeFormat
{
44 static public final int IMG_THREAD_RUNNING
= 0;
45 static public final int IMG_THREAD_SUSPENDED
= 1;
46 static public final int IMG_THREAD_STOPPED
= 2;
47 static public final int IMG_METHOD_RUNNING
= 3;
48 static public final int IMG_METHOD
= 4;
49 static public final int IMG_NUM
= 5;
51 static public final Object
[] _empty
= new Object
[0];
53 public static enum Resolution
{
54 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
57 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
58 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
60 static Rectangle
clone(Rectangle source
) {
61 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
64 static public void init(Rectangle rect
) {
71 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
78 static public void init(Rectangle rect
, Rectangle source
) {
81 rect
.width
= source
.width
;
82 rect
.height
= source
.height
;
85 static public void deflate(Rectangle rect
, int x
, int y
) {
92 static public void inflate(Rectangle rect
, int x
, int y
) {
99 static void dispose(Color col
) {
105 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
107 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
108 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
109 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
113 static public Color
getSysColor(int id
) {
114 Color col
= Display
.getCurrent().getSystemColor(id
);
115 return new Color(col
.getDevice(), col
.getRGB());
118 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
119 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
122 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
123 Point size
= gc
.stringExtent(text
);
124 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
128 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
129 Point size
= gc
.stringExtent(text
);
130 gc
.drawText(text
, x
, y
, transp
);
135 * Formats time in format: MM:SS:NNN
138 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
139 * @param resolution the resolution
140 * @return the formatted time
142 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
143 // if format is absolute (Calendar)
144 if (format
== TimeFormat
.ABSOLUTE
) {
145 return formatTimeAbs(time
, resolution
);
148 StringBuffer str
= new StringBuffer();
149 boolean neg
= time
< 0;
155 long sec
= (long) (time
* 1E-9);
156 // TODO: Expand to make it possible to select the minute, second, nanosecond format
157 //printing minutes is suppressed just sec and ns
158 // if (sec / 60 < 10)
160 // str.append(sec / 60);
166 String ns
= formatNs(time
, resolution
);
167 if (!ns
.equals("")) { //$NON-NLS-1$
172 return str
.toString();
176 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
179 * @return the formatted date
181 public static String
formatDate(long absTime
) {
182 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
187 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
190 * @return the formatted time
192 static public String
formatTimeAbs(long time
, Resolution res
) {
193 StringBuffer str
= new StringBuffer();
195 // format time from nanoseconds to calendar time HH:MM:SS
196 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
199 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
201 str
.append(formatNs(time
, res
));
202 return str
.toString();
206 * Obtains the remainder fraction on unit Seconds of the entered value in
207 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
208 * seconds can be obtained by removing the last 9 digits: 1241207054 the
209 * fractional portion of seconds, expressed in ns is: 171080214
213 * @return the formatted nanosec
215 public static String
formatNs(long time
, Resolution res
) {
216 StringBuffer str
= new StringBuffer();
217 boolean neg
= time
< 0;
222 // The following approach could be used although performance
223 // decreases in half.
224 // String strVal = String.format("%09d", time);
225 // String tmp = strVal.substring(strVal.length() - 9);
230 str
.append("00000000"); //$NON-NLS-1$
231 } else if (ns
< 100) {
232 str
.append("0000000"); //$NON-NLS-1$
233 } else if (ns
< 1000) {
234 str
.append("000000"); //$NON-NLS-1$
235 } else if (ns
< 10000) {
236 str
.append("00000"); //$NON-NLS-1$
237 } else if (ns
< 100000) {
238 str
.append("0000"); //$NON-NLS-1$
239 } else if (ns
< 1000000) {
240 str
.append("000"); //$NON-NLS-1$
241 } else if (ns
< 10000000) {
242 str
.append("00"); //$NON-NLS-1$
243 } else if (ns
< 100000000) {
244 str
.append("0"); //$NON-NLS-1$
248 if (res
== Resolution
.MILLISEC
) {
249 return str
.substring(0, 3);
250 } else if (res
== Resolution
.MICROSEC
) {
251 return str
.substring(0, 6);
252 } else if (res
== Resolution
.NANOSEC
) {
253 return str
.substring(0, 9);
255 return ""; //$NON-NLS-1$
258 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
260 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
270 static public void saveIntOption(String opt
, int val
) {
271 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
274 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
275 if (null == entry
|| ! entry
.hasTimeEvents()) {
278 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
279 if (iterator
!= null && iterator
.hasNext()) {
280 return iterator
.next();
287 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
288 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
296 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
297 if (null == entry
|| ! entry
.hasTimeEvents()) {
300 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
301 if (iterator
== null) {
304 ITimeEvent nextEvent
= null;
305 ITimeEvent currEvent
= null;
306 ITimeEvent prevEvent
= null;
308 while (iterator
.hasNext()) {
309 nextEvent
= (ITimeEvent
) iterator
.next();
310 long nextStartTime
= nextEvent
.getTime();
312 if (nextStartTime
> time
) {
316 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
317 prevEvent
= currEvent
;
318 currEvent
= nextEvent
;
322 if (n
== -1) { //previous
323 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
328 } else if (n
== 0) { //current
329 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
334 } else if (n
== 1) { //next
336 } else if (n
== 2) { //current or previous when in empty space
343 static public String
fixMethodSignature(String sig
) {
344 int pos
= sig
.indexOf('(');
346 String ret
= sig
.substring(0, pos
);
347 sig
= sig
.substring(pos
);
348 sig
= sig
+ " " + ret
; //$NON-NLS-1$
353 static public String
restoreMethodSignature(String sig
) {
354 String ret
= ""; //$NON-NLS-1$
355 int pos
= sig
.indexOf('(');
357 ret
= sig
.substring(0, pos
);
358 sig
= sig
.substring(pos
+ 1);
360 pos
= sig
.indexOf(')');
362 sig
= sig
.substring(0, pos
);
364 String args
[] = sig
.split(","); //$NON-NLS-1$
365 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
366 for (int i
= 0; i
< args
.length
; i
++) {
367 String arg
= args
[i
].trim();
368 if (arg
.length() == 0 && args
.length
== 1) {
371 result
.append(getTypeSignature(arg
));
373 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
374 return result
.toString();
377 static public String
getTypeSignature(String type
) {
379 for (int j
= 0; j
< type
.length(); j
++) {
380 if (type
.charAt(j
) == '[') {
384 int pos
= type
.indexOf('[');
386 type
= type
.substring(0, pos
);
388 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
389 for (int j
= 0; j
< dim
; j
++)
391 sig
.append("["); //$NON-NLS-1$
393 if (type
.equals("boolean")) {
394 sig
.append("Z"); //$NON-NLS-1$
395 } else if (type
.equals("byte")) {
396 sig
.append("B"); //$NON-NLS-1$
397 } else if (type
.equals("char")) {
398 sig
.append("C"); //$NON-NLS-1$
399 } else if (type
.equals("short")) {
400 sig
.append("S"); //$NON-NLS-1$
401 } else if (type
.equals("int")) {
402 sig
.append("I"); //$NON-NLS-1$
403 } else if (type
.equals("long")) {
404 sig
.append("J"); //$NON-NLS-1$
405 } else if (type
.equals("float")) {
406 sig
.append("F"); //$NON-NLS-1$
407 } else if (type
.equals("double")) {
408 sig
.append("D"); //$NON-NLS-1$
409 } else if (type
.equals("void")) {
410 sig
.append("V"); //$NON-NLS-1$
413 sig
.append("L").append(type
.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
415 return sig
.toString();
418 static public int compare(double d1
, double d2
) {
428 static public int compare(String s1
, String s2
) {
429 if (s1
!= null && s2
!= null) {
430 return s1
.compareToIgnoreCase(s2
);