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
;
21 import java
.util
.TimeZone
;
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
;
34 public enum TimeFormat
{
38 static public final int IMG_THREAD_RUNNING
= 0;
39 static public final int IMG_THREAD_SUSPENDED
= 1;
40 static public final int IMG_THREAD_STOPPED
= 2;
41 static public final int IMG_METHOD_RUNNING
= 3;
42 static public final int IMG_METHOD
= 4;
43 static public final int IMG_NUM
= 5;
45 static public final Object
[] _empty
= new Object
[0];
47 public static enum Resolution
{
48 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
51 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
52 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
54 stimeformat
.setTimeZone(TimeZone
.getTimeZone("GMT")); //$NON-NLS-1$
55 sdateformat
.setTimeZone(TimeZone
.getTimeZone("GMT")); //$NON-NLS-1$
58 static Rectangle
clone(Rectangle source
) {
59 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
62 static public void init(Rectangle rect
) {
69 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
76 static public void init(Rectangle rect
, Rectangle source
) {
79 rect
.width
= source
.width
;
80 rect
.height
= source
.height
;
83 static public void deflate(Rectangle rect
, int x
, int y
) {
90 static public void inflate(Rectangle rect
, int x
, int y
) {
97 static void dispose(Color col
) {
102 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
104 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
105 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
106 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
110 static public Color
getSysColor(int id
) {
111 Color col
= Display
.getCurrent().getSystemColor(id
);
112 return new Color(col
.getDevice(), col
.getRGB());
115 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
116 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
119 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
120 Point size
= gc
.stringExtent(text
);
121 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
125 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
126 Point size
= gc
.stringExtent(text
);
127 gc
.drawText(text
, x
, y
, transp
);
132 * Formats time in format: MM:SS:NNN
135 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
136 * @param resolution the resolution
137 * @return the formatted time
139 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
140 // if format is absolute (Calendar)
141 if (format
== TimeFormat
.ABSOLUTE
) {
142 return formatTimeAbs(time
, resolution
);
145 StringBuffer str
= new StringBuffer();
146 boolean neg
= time
< 0;
152 long sec
= (long) (time
* 1E-9);
153 // TODO: Expand to make it possible to select the minute, second, nanosecond format
154 //printing minutes is suppressed just sec and ns
155 // if (sec / 60 < 10)
157 // str.append(sec / 60);
163 String ns
= formatNs(time
, resolution
);
164 if (!ns
.equals("")) { //$NON-NLS-1$
169 return str
.toString();
173 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
176 * @return the formatted date
178 public static String
formatDate(long absTime
) {
179 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
184 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
187 * @return the formatted time
189 static public String
formatTimeAbs(long time
, Resolution res
) {
190 StringBuffer str
= new StringBuffer();
192 // format time from nanoseconds to calendar time HH:MM:SS
193 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
194 str
.append(stime
+ " "); //$NON-NLS-1$
195 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
197 str
.append(formatNs(time
, res
));
198 return str
.toString();
202 * Obtains the remainder fraction on unit Seconds of the entered value in
203 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
204 * seconds can be obtained by removing the last 9 digits: 1241207054 the
205 * fractional portion of seconds, expressed in ns is: 171080214
209 * @return the formatted nanosec
211 public static String
formatNs(long time
, Resolution res
) {
212 StringBuffer temp
= new StringBuffer();
213 boolean neg
= time
< 0;
218 // The following approach could be used although performance
219 // decreases in half.
220 // String strVal = String.format("%09d", time);
221 // String tmp = strVal.substring(strVal.length() - 9);
223 // number of segments to be included
242 temp
.append("00000000"); //$NON-NLS-1$
243 } else if (ns
< 100) {
244 temp
.append("0000000"); //$NON-NLS-1$
245 } else if (ns
< 1000) {
246 temp
.append("000000"); //$NON-NLS-1$
247 } else if (ns
< 10000) {
248 temp
.append("00000"); //$NON-NLS-1$
249 } else if (ns
< 100000) {
250 temp
.append("0000"); //$NON-NLS-1$
251 } else if (ns
< 1000000) {
252 temp
.append("000"); //$NON-NLS-1$
253 } else if (ns
< 10000000) {
254 temp
.append("00"); //$NON-NLS-1$
255 } else if (ns
< 100000000) {
256 temp
.append("0"); //$NON-NLS-1$
260 StringBuffer str
= new StringBuffer();
263 str
.append(temp
.substring(0, 3));
267 str
.append("."); //$NON-NLS-1$
268 str
.append(temp
.substring(3, 6));
271 // append Nano seconds
272 str
.append("."); //$NON-NLS-1$
273 str
.append(temp
.substring(6));
276 return str
.toString();
279 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
281 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
291 static public void saveIntOption(String opt
, int val
) {
292 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
295 static ITimeEvent
getFirstEvent(ITimeGraphEntry thread
) {
298 Iterator
<ITimeEvent
> iterator
= thread
.getTimeEventsIterator();
299 if (iterator
!= null && iterator
.hasNext()) {
300 return iterator
.next();
307 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
308 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
316 static ITimeEvent
findEvent(ITimeGraphEntry thread
, long time
, int n
) {
319 Iterator
<ITimeEvent
> iterator
= thread
.getTimeEventsIterator();
320 if (iterator
== null) {
323 ITimeEvent nextEvent
= null;
324 ITimeEvent currEvent
= null;
325 ITimeEvent prevEvent
= null;
327 while (iterator
.hasNext()) {
328 nextEvent
= (ITimeEvent
) iterator
.next();
329 long nextStartTime
= nextEvent
.getTime();
331 if (nextStartTime
> time
) {
335 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
336 prevEvent
= currEvent
;
337 currEvent
= nextEvent
;
341 if (n
== -1) { //previous
342 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
347 } else if (n
== 0) { //current
348 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
353 } else if (n
== 1) { //next
355 } else if (n
== 2) { //current or previous when in empty space
362 static public String
fixMethodSignature(String sig
) {
363 int pos
= sig
.indexOf('(');
365 String ret
= sig
.substring(0, pos
);
366 sig
= sig
.substring(pos
);
367 sig
= sig
+ " " + ret
; //$NON-NLS-1$
372 static public String
restoreMethodSignature(String sig
) {
373 String ret
= ""; //$NON-NLS-1$
374 int pos
= sig
.indexOf('(');
376 ret
= sig
.substring(0, pos
);
377 sig
= sig
.substring(pos
+ 1);
379 pos
= sig
.indexOf(')');
381 sig
= sig
.substring(0, pos
);
383 String args
[] = sig
.split(","); //$NON-NLS-1$
384 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
385 for (int i
= 0; i
< args
.length
; i
++) {
386 String arg
= args
[i
].trim();
387 if (arg
.length() == 0 && args
.length
== 1)
389 result
.append(getTypeSignature(arg
));
391 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
392 return result
.toString();
395 static public String
getTypeSignature(String type
) {
397 for (int j
= 0; j
< type
.length(); j
++) {
398 if (type
.charAt(j
) == '[')
401 int pos
= type
.indexOf('[');
403 type
= type
.substring(0, pos
);
404 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
405 for (int j
= 0; j
< dim
; j
++)
406 sig
.append("["); //$NON-NLS-1$
407 if (type
.equals("boolean")) //$NON-NLS-1$
408 sig
.append("Z"); //$NON-NLS-1$
409 else if (type
.equals("byte")) //$NON-NLS-1$
410 sig
.append("B"); //$NON-NLS-1$
411 else if (type
.equals("char")) //$NON-NLS-1$
412 sig
.append("C"); //$NON-NLS-1$
413 else if (type
.equals("short")) //$NON-NLS-1$
414 sig
.append("S"); //$NON-NLS-1$
415 else if (type
.equals("int")) //$NON-NLS-1$
416 sig
.append("I"); //$NON-NLS-1$
417 else if (type
.equals("long")) //$NON-NLS-1$
418 sig
.append("J"); //$NON-NLS-1$
419 else if (type
.equals("float")) //$NON-NLS-1$
420 sig
.append("F"); //$NON-NLS-1$
421 else if (type
.equals("double")) //$NON-NLS-1$
422 sig
.append("D"); //$NON-NLS-1$
423 else if (type
.equals("void")) //$NON-NLS-1$
424 sig
.append("V"); //$NON-NLS-1$
426 sig
.append("L").append(type
.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
427 return sig
.toString();
430 static public int compare(double d1
, double d2
) {
438 static public int compare(String s1
, String s2
) {
439 if (s1
!= null && s2
!= null)
440 return s1
.compareToIgnoreCase(s2
);