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