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
;
33 public enum TimeFormat
{
37 static public final int IMG_THREAD_RUNNING
= 0;
38 static public final int IMG_THREAD_SUSPENDED
= 1;
39 static public final int IMG_THREAD_STOPPED
= 2;
40 static public final int IMG_METHOD_RUNNING
= 3;
41 static public final int IMG_METHOD
= 4;
42 static public final int IMG_NUM
= 5;
44 static public final Object
[] _empty
= new Object
[0];
46 public static enum Resolution
{
47 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
50 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
51 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
53 static Rectangle
clone(Rectangle source
) {
54 return new Rectangle(source
.x
, source
.y
, source
.width
, source
.height
);
57 static public void init(Rectangle rect
) {
64 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
71 static public void init(Rectangle rect
, Rectangle source
) {
74 rect
.width
= source
.width
;
75 rect
.height
= source
.height
;
78 static public void deflate(Rectangle rect
, int x
, int y
) {
85 static public void inflate(Rectangle rect
, int x
, int y
) {
92 static void dispose(Color col
) {
97 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
99 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
100 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
101 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
105 static public Color
getSysColor(int id
) {
106 Color col
= Display
.getCurrent().getSystemColor(id
);
107 return new Color(col
.getDevice(), col
.getRGB());
110 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
111 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
114 static public int drawText(GC gc
, String text
, Rectangle rect
, boolean transp
) {
115 Point size
= gc
.stringExtent(text
);
116 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
120 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
121 Point size
= gc
.stringExtent(text
);
122 gc
.drawText(text
, x
, y
, transp
);
127 * Formats time in format: MM:SS:NNN
130 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
131 * @param resolution the resolution
132 * @return the formatted time
134 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
135 // if format is absolute (Calendar)
136 if (format
== TimeFormat
.ABSOLUTE
) {
137 return formatTimeAbs(time
, resolution
);
140 StringBuffer str
= new StringBuffer();
141 boolean neg
= time
< 0;
147 long sec
= (long) (time
* 1E-9);
148 // TODO: Expand to make it possible to select the minute, second, nanosecond format
149 //printing minutes is suppressed just sec and ns
150 // if (sec / 60 < 10)
152 // str.append(sec / 60);
158 String ns
= formatNs(time
, resolution
);
159 if (!ns
.equals("")) { //$NON-NLS-1$
164 return str
.toString();
168 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
171 * @return the formatted date
173 public static String
formatDate(long absTime
) {
174 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
179 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
182 * @return the formatted time
184 static public String
formatTimeAbs(long time
, Resolution res
) {
185 StringBuffer str
= new StringBuffer();
187 // format time from nanoseconds to calendar time HH:MM:SS
188 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
191 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
193 str
.append(formatNs(time
, res
));
194 return str
.toString();
198 * Obtains the remainder fraction on unit Seconds of the entered value in
199 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
200 * seconds can be obtained by removing the last 9 digits: 1241207054 the
201 * fractional portion of seconds, expressed in ns is: 171080214
205 * @return the formatted nanosec
207 public static String
formatNs(long time
, Resolution res
) {
208 StringBuffer str
= new StringBuffer();
209 boolean neg
= time
< 0;
214 // The following approach could be used although performance
215 // decreases in half.
216 // String strVal = String.format("%09d", time);
217 // String tmp = strVal.substring(strVal.length() - 9);
222 str
.append("00000000"); //$NON-NLS-1$
223 } else if (ns
< 100) {
224 str
.append("0000000"); //$NON-NLS-1$
225 } else if (ns
< 1000) {
226 str
.append("000000"); //$NON-NLS-1$
227 } else if (ns
< 10000) {
228 str
.append("00000"); //$NON-NLS-1$
229 } else if (ns
< 100000) {
230 str
.append("0000"); //$NON-NLS-1$
231 } else if (ns
< 1000000) {
232 str
.append("000"); //$NON-NLS-1$
233 } else if (ns
< 10000000) {
234 str
.append("00"); //$NON-NLS-1$
235 } else if (ns
< 100000000) {
236 str
.append("0"); //$NON-NLS-1$
240 if (res
== Resolution
.MILLISEC
) {
241 return str
.substring(0, 3);
242 } else if (res
== Resolution
.MICROSEC
) {
243 return str
.substring(0, 6);
244 } else if (res
== Resolution
.NANOSEC
) {
245 return str
.substring(0, 9);
247 return ""; //$NON-NLS-1$
250 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
252 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
262 static public void saveIntOption(String opt
, int val
) {
263 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
266 static ITimeEvent
getFirstEvent(ITimeGraphEntry entry
) {
267 if (null == entry
|| ! entry
.hasTimeEvents()) {
270 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
271 if (iterator
!= null && iterator
.hasNext()) {
272 return iterator
.next();
279 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
280 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
288 static ITimeEvent
findEvent(ITimeGraphEntry entry
, long time
, int n
) {
289 if (null == entry
|| ! entry
.hasTimeEvents()) {
292 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator();
293 if (iterator
== null) {
296 ITimeEvent nextEvent
= null;
297 ITimeEvent currEvent
= null;
298 ITimeEvent prevEvent
= null;
300 while (iterator
.hasNext()) {
301 nextEvent
= (ITimeEvent
) iterator
.next();
302 long nextStartTime
= nextEvent
.getTime();
304 if (nextStartTime
> time
) {
308 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
309 prevEvent
= currEvent
;
310 currEvent
= nextEvent
;
314 if (n
== -1) { //previous
315 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
320 } else if (n
== 0) { //current
321 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
326 } else if (n
== 1) { //next
328 } else if (n
== 2) { //current or previous when in empty space
335 static public String
fixMethodSignature(String sig
) {
336 int pos
= sig
.indexOf('(');
338 String ret
= sig
.substring(0, pos
);
339 sig
= sig
.substring(pos
);
340 sig
= sig
+ " " + ret
; //$NON-NLS-1$
345 static public String
restoreMethodSignature(String sig
) {
346 String ret
= ""; //$NON-NLS-1$
347 int pos
= sig
.indexOf('(');
349 ret
= sig
.substring(0, pos
);
350 sig
= sig
.substring(pos
+ 1);
352 pos
= sig
.indexOf(')');
354 sig
= sig
.substring(0, pos
);
356 String args
[] = sig
.split(","); //$NON-NLS-1$
357 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
358 for (int i
= 0; i
< args
.length
; i
++) {
359 String arg
= args
[i
].trim();
360 if (arg
.length() == 0 && args
.length
== 1)
362 result
.append(getTypeSignature(arg
));
364 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
365 return result
.toString();
368 static public String
getTypeSignature(String type
) {
370 for (int j
= 0; j
< type
.length(); j
++) {
371 if (type
.charAt(j
) == '[')
374 int pos
= type
.indexOf('[');
376 type
= type
.substring(0, pos
);
377 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
378 for (int j
= 0; j
< dim
; j
++)
379 sig
.append("["); //$NON-NLS-1$
380 if (type
.equals("boolean")) //$NON-NLS-1$
381 sig
.append("Z"); //$NON-NLS-1$
382 else if (type
.equals("byte")) //$NON-NLS-1$
383 sig
.append("B"); //$NON-NLS-1$
384 else if (type
.equals("char")) //$NON-NLS-1$
385 sig
.append("C"); //$NON-NLS-1$
386 else if (type
.equals("short")) //$NON-NLS-1$
387 sig
.append("S"); //$NON-NLS-1$
388 else if (type
.equals("int")) //$NON-NLS-1$
389 sig
.append("I"); //$NON-NLS-1$
390 else if (type
.equals("long")) //$NON-NLS-1$
391 sig
.append("J"); //$NON-NLS-1$
392 else if (type
.equals("float")) //$NON-NLS-1$
393 sig
.append("F"); //$NON-NLS-1$
394 else if (type
.equals("double")) //$NON-NLS-1$
395 sig
.append("D"); //$NON-NLS-1$
396 else if (type
.equals("void")) //$NON-NLS-1$
397 sig
.append("V"); //$NON-NLS-1$
399 sig
.append("L").append(type
.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
400 return sig
.toString();
403 static public int compare(double d1
, double d2
) {
411 static public int compare(String s1
, String s2
) {
412 if (s1
!= null && s2
!= null)
413 return s1
.compareToIgnoreCase(s2
);