April 26th, 2010
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / timeAnalysis / widgets / Utils.java
1 /*****************************************************************************
2 * Copyright (c) 2007, 2008 Intel Corporation and others.
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
7 *
8 * Contributors:
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchex-Leon - Udpated for TMF
12 *
13 * $Id: Utils.java,v 1.11 2008/06/16 21:04:49 jkubasta Exp $
14 *****************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.widgets;
17
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.TimeZone;
23 import java.util.Vector;
24
25 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.ITimeAnalysisViewer.TimeFormat;
26 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITimeEvent;
27 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;
28 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.TimeEvent;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.Device;
31 import org.eclipse.swt.graphics.GC;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.widgets.Display;
35
36 public class Utils {
37
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;
44
45 static public final Object[] _empty = new Object[0];
46
47 static enum Resolution {
48 SECONDS, MILLISEC, MICROSEC, NANOSEC
49 };
50
51 static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss");
52 static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd");
53 static {
54 stimeformat.setTimeZone(TimeZone.getTimeZone("GMT"));
55 sdateformat.setTimeZone(TimeZone.getTimeZone("GMT"));
56 }
57
58 // static private String _externalPlugin[] = { "org.eclipse.debug.ui",
59 // "org.eclipse.debug.ui", "org.eclipse.debug.ui",
60 // "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
61 //
62 // static private String _externalPath[] = {
63 // "icons/full/obj16/thread_obj.gif", // running thread
64 // "icons/full/obj16/threads_obj.gif", // suspended
65 // "icons/full/obj16/threadt_obj.gif", // stopped
66 // "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
67 // "icons/full/obj16/stckframe_obj.gif", // stack frame
68 // };
69
70 // static public Image getImage(int idx) {
71 // if (idx < 0 || idx >= IMG_NUM)
72 // SWT.error(SWT.ERROR_INVALID_ARGUMENT);
73 // String key = "trace.img." + idx;
74 // Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
75 // if (null == img) {
76 // ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
77 // _externalPlugin[idx], _externalPath[idx]);
78 // TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);
79 // img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
80 // }
81 // return img;
82 // }
83
84 static public void init(Rectangle rect) {
85 rect.x = 0;
86 rect.y = 0;
87 rect.width = 0;
88 rect.height = 0;
89 }
90
91 static public void init(Rectangle rect, int x, int y, int width, int height) {
92 rect.x = x;
93 rect.y = y;
94 rect.width = width;
95 rect.height = height;
96 }
97
98 static public void init(Rectangle rect, Rectangle source) {
99 rect.x = source.x;
100 rect.y = source.y;
101 rect.width = source.width;
102 rect.height = source.height;
103 }
104
105 static public void deflate(Rectangle rect, int x, int y) {
106 rect.x += x;
107 rect.y += y;
108 rect.width -= x + x;
109 rect.height -= y + y;
110 }
111
112 static public void inflate(Rectangle rect, int x, int y) {
113 rect.x -= x;
114 rect.y -= y;
115 rect.width += x + x;
116 rect.height += y + y;
117 }
118
119 static void dispose(Color col) {
120 if (null != col)
121 col.dispose();
122 }
123
124 static public Color mixColors(Device display, Color c1, Color c2, int w1,
125 int w2) {
126 return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())
127 / (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())
128 / (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())
129 / (w1 + w2));
130 }
131
132 static public Color getSysColor(int id) {
133 Color col = Display.getCurrent().getSystemColor(id);
134 return new Color(col.getDevice(), col.getRGB());
135 }
136
137 static public Color mixColors(Color col1, Color col2, int w1, int w2) {
138 return mixColors(Display.getCurrent(), col1, col2, w1, w2);
139 }
140
141 static public int drawText(GC gc, String text, Rectangle rect,
142 boolean transp) {
143 Point size = gc.stringExtent(text);
144 gc.drawText(text, rect.x, rect.y, transp);
145 return size.x;
146 }
147
148 static public int drawText(GC gc, String text, int x, int y, boolean transp) {
149 Point size = gc.stringExtent(text);
150 gc.drawText(text, x, y, transp);
151 return size.x;
152 }
153
154 /**
155 * Formats time in format: MM:SS:NNN
156 *
157 * @param v
158 * @param option
159 * 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
160 * @return
161 */
162 static public String formatTime(long v, TimeFormat format, Resolution res) {
163 // if format is absolute (Calendar)
164 if (format == TimeFormat.ABSOLUTE) {
165 return formatTimeAbs(v, res);
166 }
167
168 StringBuffer str = new StringBuffer();
169 boolean neg = v < 0;
170 if (neg) {
171 v = -v;
172 str.append('-');
173 }
174
175 long sec = (long) (v * 1E-9);
176 // TODO: Expand to make it possible to select the minute, second, nanosecond format
177 //printing minutes is suppressed just sec and ns
178 // if (sec / 60 < 10)
179 // str.append('0');
180 // str.append(sec / 60);
181 // str.append(':');
182 // sec %= 60;
183 // if (sec < 10)
184 // str.append('0');
185 str.append(sec);
186 String ns = formatNs(v, res);
187 if (!ns.equals("")) {
188 str.append(':');
189 str.append(ns);
190 }
191
192 return str.toString();
193 }
194
195 /**
196 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
197 *
198 * @param absTime
199 * @return
200 */
201 public static String formatDate(long absTime) {
202 String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));
203 return sdate;
204 }
205
206 /**
207 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
208 *
209 * @param time
210 * @return
211 */
212 static public String formatTimeAbs(long time, Resolution res) {
213 StringBuffer str = new StringBuffer();
214
215 // format time from nanoseconds to calendar time HH:MM:SS
216 String stime = stimeformat.format(new Date((long) (time * 1E-6)));
217 str.append(stime + " ");
218 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
219 // the Resolution
220 str.append(formatNs(time, res));
221 return str.toString();
222 }
223
224 /**
225 * Obtains the remainder fraction on unit Seconds of the entered value in
226 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
227 * seconds can be obtained by removing the last 9 digits: 1241207054 the
228 * fractional portion of seconds, expressed in ns is: 171080214
229 *
230 * @param time
231 * @param res
232 * @return
233 */
234 public static String formatNs(long time, Resolution res) {
235 StringBuffer temp = new StringBuffer();
236 boolean neg = time < 0;
237 if (neg) {
238 time = -time;
239 }
240
241 // The following approach could be used although performance
242 // decreases in half.
243 // String strVal = String.format("%09d", time);
244 // String tmp = strVal.substring(strVal.length() - 9);
245
246 // number of segments to be included
247 int segments = 0;
248 switch (res) {
249 case MILLISEC:
250 segments = 1;
251 break;
252 case MICROSEC:
253 segments = 2;
254 break;
255 case NANOSEC:
256 segments = 3;
257 break;
258 default:
259 break;
260 }
261
262 long ns = time;
263 ns %= 1000000000;
264 if (ns < 10) {
265 temp.append("00000000");
266 } else if (ns < 100) {
267 temp.append("0000000");
268 } else if (ns < 1000) {
269 temp.append("000000");
270 } else if (ns < 10000) {
271 temp.append("00000");
272 } else if (ns < 100000) {
273 temp.append("0000");
274 } else if (ns < 1000000) {
275 temp.append("000");
276 } else if (ns < 10000000) {
277 temp.append("00");
278 } else if (ns < 100000000) {
279 temp.append("0");
280 }
281 temp.append(ns);
282
283 StringBuffer str = new StringBuffer();
284 if (segments > 0) {
285 // append ms
286 str.append(temp.substring(0, 3));
287 }
288 if (segments > 1) {
289 // append Micro secs
290 str.append(".");
291 str.append(temp.substring(3, 6));
292 }
293 if (segments > 2) {
294 // append Nano seconds
295 str.append(".");
296 str.append(temp.substring(6));
297 }
298
299 return str.toString();
300 }
301
302 static public int loadIntOption(String opt, int def, int min, int max) {
303 // int val =
304 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
305 // if (0 == val)
306 // val = def;
307 // if (val < min)
308 // val = min;
309 // if (val > max)
310 // val = max;
311 return def;
312 }
313
314 // static public int loadIntOption(String opt) {
315 // int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
316 // return val;
317 // }
318
319 static public void saveIntOption(String opt, int val) {
320 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
321 }
322
323 static ITimeEvent getFirstEvent(ITmfTimeAnalysisEntry thread) {
324 if (null == thread)
325 return null;
326 Vector<TimeEvent> list = thread.getTraceEvents();
327 ITimeEvent event = null;
328 if (!list.isEmpty())
329 event = (ITimeEvent) list.get(0);
330 return event;
331 }
332
333 /**
334 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
335 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
336 * </list>
337 *
338 * @param thread
339 * @param time
340 * @param n
341 * @return
342 */
343 static ITimeEvent findEvent(ITmfTimeAnalysisEntry thread, long time, int n) {
344 if (null == thread)
345 return null;
346 List<TimeEvent> list = thread.getTraceEvents();
347 Iterator<TimeEvent> it = list.iterator();
348 ITimeEvent nextEvent = null;
349 ITimeEvent currEvent = null;
350 ITimeEvent prevEvent = null;
351
352 while (it.hasNext()) {
353 nextEvent = (ITimeEvent) it.next();
354 long nextStartTime = nextEvent.getTime();
355
356 if (nextStartTime > time) {
357 break;
358 }
359
360 if (currEvent == null || currEvent.getTime() != nextStartTime) {
361 prevEvent = currEvent;
362 currEvent = nextEvent;
363 }
364 }
365
366 if (n == -1) { //previous
367 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
368 return prevEvent;
369 } else {
370 return currEvent;
371 }
372 } else if (n == 0) { //current
373 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
374 return currEvent;
375 } else {
376 return null;
377 }
378 } else if (n == 1) { //next
379 return nextEvent;
380 } else if (n == 2) { //current or previous when in empty space
381 return currEvent;
382 }
383
384 return null;
385 }
386
387 // static public TRCPackage getPackage(Object element) {
388 // if (element instanceof TRCPackage)
389 // return (TRCPackage) element;
390 // if (element instanceof TRCClass)
391 // return ((TRCClass) element).getPackage();
392 // return null;
393 // }
394
395 // static public TRCObjectAllocationAnnotation getAllocationAnnotation(
396 // TRCClass cls) {
397 // TRCObjectAllocationAnnotation aa = null;
398 // EList list = cls.getAnnotations();
399 // int len = list.size();
400 // for (int i = 0; i < len; i++) {
401 // TRCAnnotation annotation = (TRCAnnotation) list.get(i);
402 // if (annotation instanceof TRCObjectAllocationAnnotation)
403 // aa = (TRCObjectAllocationAnnotation) annotation;
404 // }
405 // return aa;
406 // }
407
408 static public String fixMethodSignature(String sig) {
409 int pos = sig.indexOf('(');
410 if (pos >= 0) {
411 String ret = sig.substring(0, pos);
412 sig = sig.substring(pos);
413 sig = sig + " " + ret;
414 }
415 return sig;
416 }
417
418 static public String restoreMethodSignature(String sig) {
419 String ret = "";
420 int pos = sig.indexOf('(');
421 if (pos >= 0) {
422 ret = sig.substring(0, pos);
423 sig = sig.substring(pos + 1);
424 }
425 pos = sig.indexOf(')');
426 if (pos >= 0) {
427 sig = sig.substring(0, pos);
428 }
429 String args[] = sig.split(",");
430 sig = "(";
431 for (int i = 0; i < args.length; i++) {
432 String arg = args[i].trim();
433 if (arg.length() == 0 && args.length == 1)
434 break;
435 sig += getTypeSignature(arg);
436 }
437 sig += ")" + getTypeSignature(ret);
438 return sig;
439 }
440
441 static public String getTypeSignature(String type) {
442 int dim = 0;
443 for (int j = 0; j < type.length(); j++) {
444 if (type.charAt(j) == '[')
445 dim++;
446 }
447 int pos = type.indexOf('[');
448 if (pos >= 0)
449 type = type.substring(0, pos);
450 String sig = "";
451 for (int j = 0; j < dim; j++)
452 sig += "[";
453 if (type.equals("boolean"))
454 sig += "Z";
455 else if (type.equals("byte"))
456 sig += "B";
457 else if (type.equals("char"))
458 sig += "C";
459 else if (type.equals("short"))
460 sig += "S";
461 else if (type.equals("int"))
462 sig += "I";
463 else if (type.equals("long"))
464 sig += "J";
465 else if (type.equals("float"))
466 sig += "F";
467 else if (type.equals("double"))
468 sig += "D";
469 else if (type.equals("void"))
470 sig += "V";
471 else
472 sig += "L" + type.replace('.', '/') + ";";
473 return sig;
474 }
475
476 // static public boolean openSource(Object element) {
477 // if (element instanceof String) {
478 // final String pattern = (String) element;
479 // final int javaType = IJavaSearchConstants.METHOD;
480 // BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
481 // public void run() {
482 // if (!OpenJavaSource.openSource(pattern, javaType,
483 // SearchEngine.createWorkspaceScope(), true)) {
484 // MessageDialog.openInformation(UIPlugin.getDefault()
485 // .getWorkbench().getActiveWorkbenchWindow()
486 // .getShell(), TraceMessages.TRC_MSGT, NLS.bind(
487 // TraceUIMessages._68, pattern));
488 // }
489 // }
490 // });
491 // }
492 // OpenSource.openSource(element);
493 // return true;
494 // }
495
496 // static public int getObjAge(TRCFullTraceObject obj, EList listGC) {
497 // int age = 0;
498 // double t0 = obj.getCreateTime();
499 // double t1 = obj.getCollectTime();
500 // int len = listGC.size();
501 // for (int j = 0; j < len; j++) {
502 // TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);
503 // if (gcEvent.getType().equals("finish")) {
504 // double time = gcEvent.getTime();
505 // if (time <= t0)
506 // continue;
507 // if (t1 > 0 && time >= t1)
508 // break;
509 // age++;
510 // }
511 // }
512 // return age;
513 // }
514
515 static public int compare(double d1, double d2) {
516 if (d1 > d2)
517 return 1;
518 if (d1 < d2)
519 return 1;
520 return 0;
521 }
522
523 static public int compare(String s1, String s2) {
524 if (s1 != null && s2 != null)
525 return s1.compareToIgnoreCase(s2);
526 if (s1 != null)
527 return 1;
528 if (s2 != null)
529 return -1;
530 return 0;
531 }
532
533 // static public String formatPercent(int val, int max) {
534 // String s = max > 0 && max >= val ? TString
535 // .formatAsPercentage((double) val / (double) max) : "";
536 // return s;
537 // }
538 }
This page took 0.042223 seconds and 5 git commands to generate.