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