lttng/tmf: Enable and fix parameter assignment warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / Utils.java
CommitLineData
6151d86c
PT
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
7 *
8 * Contributors:
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
13 *
14 *****************************************************************************/
15
16package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets;
17
026664b7 18import java.text.NumberFormat;
6151d86c
PT
19import java.text.SimpleDateFormat;
20import java.util.Date;
21import java.util.Iterator;
22
23import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
24import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
25import org.eclipse.swt.graphics.Color;
26import org.eclipse.swt.graphics.Device;
27import org.eclipse.swt.graphics.GC;
28import org.eclipse.swt.graphics.Point;
29import org.eclipse.swt.graphics.Rectangle;
30import org.eclipse.swt.widgets.Display;
31
32/**
33 * General utilities and definitions used by the time graph widget
34 *
35 * @version 1.0
36 * @author Alvaro Sanchez-Leon
37 * @author Patrick Tasse
38 */
39public class Utils {
40
41 /** Time format for dates and timestamp */
42 public enum TimeFormat {
43 /** Relative to the start of the trace */
44 RELATIVE,
386cf45c
AM
45
46 /**
47 * Absolute timestamp (ie, relative to the Unix epoch)
48 * @since 2.0
49 */
026664b7 50 CALENDAR,
386cf45c
AM
51
52 /**
53 * Timestamp displayed as a simple number
54 * @since 2.0
55 */
026664b7 56 NUMBER,
6151d86c
PT
57 }
58
a0a88f65
AM
59 /**
60 * Timestamp resolution
61 */
62 public static enum Resolution {
63 /** seconds */
64 SECONDS,
6151d86c 65
a0a88f65
AM
66 /** milliseconds */
67 MILLISEC,
6151d86c 68
a0a88f65
AM
69 /** microseconds */
70 MICROSEC,
71
72 /** nanoseconds */
73 NANOSEC
6151d86c
PT
74 }
75
76 static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
77 static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
78
79 static Rectangle clone(Rectangle source) {
80 return new Rectangle(source.x, source.y, source.width, source.height);
81 }
82
83 /**
84 * Initialize a Rectangle object to default values (all equal to 0)
85 *
86 * @param rect
87 * The Rectangle to initialize
88 */
89 static public void init(Rectangle rect) {
90 rect.x = 0;
91 rect.y = 0;
92 rect.width = 0;
93 rect.height = 0;
94 }
95
96 /**
97 * Initialize a Rectangle object with all the given values
98 *
99 * @param rect
100 * The Rectangle object to initialize
101 * @param x
102 * The X coordinate
103 * @param y
104 * The Y coordinate
105 * @param width
106 * The width of the rectangle
107 * @param height
108 * The height of the rectangle
109 */
110 static public void init(Rectangle rect, int x, int y, int width, int height) {
111 rect.x = x;
112 rect.y = y;
113 rect.width = width;
114 rect.height = height;
115 }
116
117 /**
118 * Initialize a Rectangle object to another existing Rectangle's values.
119 *
120 * @param rect
121 * The Rectangle to initialize
122 * @param source
123 * The reference Rectangle to copy
124 */
125 static public void init(Rectangle rect, Rectangle source) {
126 rect.x = source.x;
127 rect.y = source.y;
128 rect.width = source.width;
129 rect.height = source.height;
130 }
131
132 /**
133 * Reduce the size of a given rectangle by the given amounts.
134 *
135 * @param rect
136 * The rectangle to modify
137 * @param x
138 * The reduction in width
139 * @param y
140 * The reduction in height
141 */
142 static public void deflate(Rectangle rect, int x, int y) {
143 rect.x += x;
144 rect.y += y;
145 rect.width -= x + x;
146 rect.height -= y + y;
147 }
148
149 /**
150 * Increase the size of a given rectangle by the given amounts.
151 *
152 * @param rect
153 * The rectangle to modify
154 * @param x
155 * The augmentation in width
156 * @param y
157 * The augmentation in height
158 */
159 static public void inflate(Rectangle rect, int x, int y) {
160 rect.x -= x;
161 rect.y -= y;
162 rect.width += x + x;
163 rect.height += y + y;
164 }
165
166 static void dispose(Color col) {
167 if (null != col) {
168 col.dispose();
169 }
170 }
171
172 /**
173 * Get the resulting color from a mix of two existing ones for a given
174 * display.
175 *
176 * @param display
177 * The display device (which might affect the color conversion)
178 * @param c1
179 * The first color
180 * @param c2
181 * The second color
182 * @param w1
183 * The gamma level for color 1
184 * @param w2
185 * The gamma level for color 2
186 * @return The resulting color
187 */
188 static public Color mixColors(Device display, Color c1, Color c2, int w1,
189 int w2) {
190 return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())
191 / (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())
192 / (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())
193 / (w1 + w2));
194 }
195
196 /**
197 * Get the system color with the given ID.
198 *
199 * @param id
200 * The color ID
201 * @return The resulting color
202 */
203 static public Color getSysColor(int id) {
204 Color col = Display.getCurrent().getSystemColor(id);
205 return new Color(col.getDevice(), col.getRGB());
206 }
207
208 /**
209 * Get the resulting color from a mix of two existing ones for the current
210 * display.
211 *
212 * @param col1
213 * The first color
214 * @param col2
215 * The second color
216 * @param w1
217 * The gamma level for color 1
218 * @param w2
219 * The gamma level for color 2
220 * @return The resulting color
221 */
222 static public Color mixColors(Color col1, Color col2, int w1, int w2) {
223 return mixColors(Display.getCurrent(), col1, col2, w1, w2);
224 }
225
226 /**
227 * Draw text in a rectangle.
228 *
229 * @param gc
230 * The SWT GC object
231 * @param text
232 * The text to draw
233 * @param rect
234 * The rectangle object which is being drawn
235 * @param transp
236 * Should we transpose the color
237 * @return The X coordinate where we have written
238 */
239 static public int drawText(GC gc, String text, Rectangle rect, boolean transp) {
240 Point size = gc.stringExtent(text);
241 gc.drawText(text, rect.x, rect.y, transp);
242 return size.x;
243 }
244
245 /**
246 * Draw text at a given location.
247 *
248 * @param gc
249 * The SWT GC object
250 * @param text
251 * The text to draw
252 * @param x
253 * The X coordinate of the starting point
254 * @param y
255 * the Y coordinate of the starting point
256 * @param transp
257 * Should we transpose the color
258 * @return The X coordinate where we have written
259 */
260 static public int drawText(GC gc, String text, int x, int y, boolean transp) {
261 Point size = gc.stringExtent(text);
262 gc.drawText(text, x, y, transp);
263 return size.x;
264 }
265
713a70ae
PT
266 /**
267 * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
268 *
269 * @param gc
270 * The SWT GC object
271 * @param text
272 * The string to be drawn
273 * @param x
274 * The x coordinate of the top left corner of the rectangular area where the text is to be drawn
275 * @param y
276 * The y coordinate of the top left corner of the rectangular area where the text is to be drawn
277 * @param width
278 * The width of the area to be drawn
279 * @param isCentered
280 * If <code>true</code> the text will be centered in the available width if space permits
281 * @param isTransparent
282 * If <code>true</code> the background will be transparent, otherwise it will be opaque
283 * @return The number of characters written
284 *
285 * @since 2.0
286 */
287 static public int drawText(GC gc, String text, int x, int y, int width, boolean isCentered, boolean isTransparent) {
288 int len = text.length();
289 int textWidth = 0;
41b5c37f
AM
290 boolean isReallyCentered = isCentered;
291 int realX = x;
292
713a70ae
PT
293 while (len > 0) {
294 textWidth = gc.stringExtent(text.substring(0, len)).x;
295 if (textWidth <= width) {
296 break;
297 }
41b5c37f 298 isReallyCentered = false;
713a70ae
PT
299 len--;
300 }
301 if (len > 0) {
41b5c37f
AM
302 if (isReallyCentered) {
303 realX += (width - textWidth) / 2;
713a70ae 304 }
41b5c37f 305 gc.drawText(text.substring(0, len), realX, y, isTransparent);
713a70ae
PT
306 }
307 return len;
308 }
309
6151d86c
PT
310 /**
311 * Formats time in format: MM:SS:NNN
312 *
313 * @param time time
314 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
315 * @param resolution the resolution
316 * @return the formatted time
317 */
318 static public String formatTime(long time, TimeFormat format, Resolution resolution) {
319 // if format is absolute (Calendar)
026664b7 320 if (format == TimeFormat.CALENDAR) {
6151d86c 321 return formatTimeAbs(time, resolution);
026664b7
XR
322 } else if (format == TimeFormat.NUMBER) {
323 return NumberFormat.getInstance().format(time);
6151d86c
PT
324 }
325
326 StringBuffer str = new StringBuffer();
41b5c37f
AM
327 long t = time;
328 boolean neg = t < 0;
6151d86c 329 if (neg) {
41b5c37f 330 t = -t;
6151d86c
PT
331 str.append('-');
332 }
333
41b5c37f 334 long sec = (long) (t * 1E-9);
6151d86c
PT
335 // TODO: Expand to make it possible to select the minute, second, nanosecond format
336 //printing minutes is suppressed just sec and ns
337 // if (sec / 60 < 10)
338 // str.append('0');
339 // str.append(sec / 60);
340 // str.append(':');
341 // sec %= 60;
342 // if (sec < 10)
343 // str.append('0');
344 str.append(sec);
41b5c37f 345 String ns = formatNs(t, resolution);
6151d86c
PT
346 if (!ns.equals("")) { //$NON-NLS-1$
347 str.append('.');
348 str.append(ns);
349 }
350
351 return str.toString();
352 }
353
354 /**
355 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
356 *
357 * @param absTime
358 * The source time, in ns
359 * @return the formatted date
360 */
361 public static String formatDate(long absTime) {
362 String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));
363 return sdate;
364 }
365
366 /**
367 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
368 *
369 * @param time
370 * The source time, in ns
371 * @param res
372 * The resolution to use
373 * @return the formatted time
374 */
375 static public String formatTimeAbs(long time, Resolution res) {
376 StringBuffer str = new StringBuffer();
377
378 // format time from nanoseconds to calendar time HH:MM:SS
379 String stime = stimeformat.format(new Date((long) (time * 1E-6)));
380 str.append(stime);
381 str.append('.');
382 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
383 // the Resolution
384 str.append(formatNs(time, res));
385 return str.toString();
386 }
387
388 /**
389 * Obtains the remainder fraction on unit Seconds of the entered value in
390 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
391 * seconds can be obtained by removing the last 9 digits: 1241207054 the
392 * fractional portion of seconds, expressed in ns is: 171080214
393 *
41b5c37f 394 * @param srcTime
6151d86c
PT
395 * The source time in ns
396 * @param res
397 * The Resolution to use
398 * @return the formatted nanosec
399 */
41b5c37f 400 public static String formatNs(long srcTime, Resolution res) {
6151d86c 401 StringBuffer str = new StringBuffer();
41b5c37f 402 long time = srcTime;
6151d86c
PT
403 boolean neg = time < 0;
404 if (neg) {
405 time = -time;
406 }
407
408 // The following approach could be used although performance
409 // decreases in half.
410 // String strVal = String.format("%09d", time);
411 // String tmp = strVal.substring(strVal.length() - 9);
412
413 long ns = time;
414 ns %= 1000000000;
415 if (ns < 10) {
416 str.append("00000000"); //$NON-NLS-1$
417 } else if (ns < 100) {
418 str.append("0000000"); //$NON-NLS-1$
419 } else if (ns < 1000) {
420 str.append("000000"); //$NON-NLS-1$
421 } else if (ns < 10000) {
422 str.append("00000"); //$NON-NLS-1$
423 } else if (ns < 100000) {
424 str.append("0000"); //$NON-NLS-1$
425 } else if (ns < 1000000) {
426 str.append("000"); //$NON-NLS-1$
427 } else if (ns < 10000000) {
428 str.append("00"); //$NON-NLS-1$
429 } else if (ns < 100000000) {
430 str.append("0"); //$NON-NLS-1$
431 }
432 str.append(ns);
433
434 if (res == Resolution.MILLISEC) {
435 return str.substring(0, 3);
436 } else if (res == Resolution.MICROSEC) {
437 return str.substring(0, 6);
438 } else if (res == Resolution.NANOSEC) {
439 return str.substring(0, 9);
440 }
441 return ""; //$NON-NLS-1$
442 }
443
444 /**
445 * FIXME Currently does nothing.
446 *
447 * @param opt
448 * The option name
449 * @param def
450 * The option value
451 * @param min
452 * The minimal accepted value
453 * @param max
454 * The maximal accepted value
455 * @return The value that was read
456 */
457 static public int loadIntOption(String opt, int def, int min, int max) {
458 // int val =
459 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
460 // if (0 == val)
461 // val = def;
462 // if (val < min)
463 // val = min;
464 // if (val > max)
465 // val = max;
466 return def;
467 }
468
469 /**
470 * FIXME currently does nothing
471 *
472 * @param opt
473 * The option name
474 * @param val
475 * The option value
476 */
477 static public void saveIntOption(String opt, int val) {
478 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
479 }
480
481 static ITimeEvent getFirstEvent(ITimeGraphEntry entry) {
482 if (null == entry || ! entry.hasTimeEvents()) {
483 return null;
484 }
485 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator();
486 if (iterator != null && iterator.hasNext()) {
487 return iterator.next();
488 }
489 return null;
490 }
491
492 /**
493 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
494 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
495 * </list>
496 *
497 * @param entry
498 * @param time
499 * @param n
500 * @return
501 */
502 static ITimeEvent findEvent(ITimeGraphEntry entry, long time, int n) {
503 if (null == entry || ! entry.hasTimeEvents()) {
504 return null;
505 }
506 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator();
507 if (iterator == null) {
508 return null;
509 }
510 ITimeEvent nextEvent = null;
511 ITimeEvent currEvent = null;
512 ITimeEvent prevEvent = null;
513
514 while (iterator.hasNext()) {
515 nextEvent = iterator.next();
516 long nextStartTime = nextEvent.getTime();
517
518 if (nextStartTime > time) {
519 break;
520 }
521
522 if (currEvent == null || currEvent.getTime() != nextStartTime) {
523 prevEvent = currEvent;
524 currEvent = nextEvent;
525 }
526 }
527
528 if (n == -1) { //previous
529 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
530 return prevEvent;
531 }
532 return currEvent;
533 } else if (n == 0) { //current
534 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
535 return currEvent;
536 }
537 return null;
538 } else if (n == 1) { //next
539 if (nextEvent != null && nextEvent.getTime() > time) {
540 return nextEvent;
541 }
542 return null;
543 } else if (n == 2) { //current or previous when in empty space
544 return currEvent;
545 }
546
547 return null;
548 }
549
550 /**
551 * Pretty-print a method signature.
552 *
41b5c37f 553 * @param origSig
6151d86c
PT
554 * The original signature
555 * @return The pretty signature
556 */
41b5c37f
AM
557 static public String fixMethodSignature(String origSig) {
558 String sig = origSig;
6151d86c
PT
559 int pos = sig.indexOf('(');
560 if (pos >= 0) {
561 String ret = sig.substring(0, pos);
562 sig = sig.substring(pos);
563 sig = sig + " " + ret; //$NON-NLS-1$
564 }
565 return sig;
566 }
567
568 /**
569 * Restore an original method signature from a pretty-printed one.
570 *
41b5c37f 571 * @param ppSig
6151d86c
PT
572 * The pretty-printed signature
573 * @return The original method signature
574 */
41b5c37f 575 static public String restoreMethodSignature(String ppSig) {
6151d86c 576 String ret = ""; //$NON-NLS-1$
41b5c37f
AM
577 String sig = ppSig;
578
6151d86c
PT
579 int pos = sig.indexOf('(');
580 if (pos >= 0) {
581 ret = sig.substring(0, pos);
582 sig = sig.substring(pos + 1);
583 }
584 pos = sig.indexOf(')');
585 if (pos >= 0) {
586 sig = sig.substring(0, pos);
587 }
588 String args[] = sig.split(","); //$NON-NLS-1$
589 StringBuffer result = new StringBuffer("("); //$NON-NLS-1$
590 for (int i = 0; i < args.length; i++) {
591 String arg = args[i].trim();
592 if (arg.length() == 0 && args.length == 1) {
593 break;
594 }
595 result.append(getTypeSignature(arg));
596 }
597 result.append(")").append(getTypeSignature(ret)); //$NON-NLS-1$
598 return result.toString();
599 }
600
601 /**
602 * Get the mangled type information from an array of types.
603 *
41b5c37f 604 * @param typeStr
6151d86c
PT
605 * The types to convert. See method implementation for what it
606 * expects.
607 * @return The mangled string of types
608 */
41b5c37f 609 public static String getTypeSignature(String typeStr) {
6151d86c 610 int dim = 0;
41b5c37f 611 String type = typeStr;
6151d86c
PT
612 for (int j = 0; j < type.length(); j++) {
613 if (type.charAt(j) == '[') {
614 dim++;
615 }
616 }
617 int pos = type.indexOf('[');
618 if (pos >= 0) {
619 type = type.substring(0, pos);
620 }
621 StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$
622 for (int j = 0; j < dim; j++)
623 {
41b5c37f 624 sig.append("["); //$NON-NLS-1$
6151d86c
PT
625 }
626 if (type.equals("boolean")) { //$NON-NLS-1$
627 sig.append('Z');
628 } else if (type.equals("byte")) { //$NON-NLS-1$
629 sig.append('B');
630 } else if (type.equals("char")) { //$NON-NLS-1$
631 sig.append('C');
632 } else if (type.equals("short")) { //$NON-NLS-1$
633 sig.append('S');
634 } else if (type.equals("int")) { //$NON-NLS-1$
635 sig.append('I');
636 } else if (type.equals("long")) { //$NON-NLS-1$
637 sig.append('J');
638 } else if (type.equals("float")) { //$NON-NLS-1$
639 sig.append('F');
640 } else if (type.equals("double")) { //$NON-NLS-1$
641 sig.append('D');
642 } else if (type.equals("void")) { //$NON-NLS-1$
643 sig.append('V');
644 }
645 else {
646 sig.append('L').append(type.replace('.', '/')).append(';');
647 }
648 return sig.toString();
649 }
650
651 /**
652 * Compare two doubles together.
653 *
654 * @param d1
655 * First double
656 * @param d2
657 * Second double
658 * @return 1 if they are different, and 0 if they are *exactly* the same.
659 * Because of the way doubles are stored, it's possible for the
660 * same number obtained in two different ways to actually look
661 * different.
662 */
663 static public int compare(double d1, double d2) {
664 if (d1 > d2) {
665 return 1;
666 }
667 if (d1 < d2) {
668 return 1;
669 }
670 return 0;
671 }
672
673 /**
674 * Compare two character strings alphabetically. This is simply a wrapper
675 * around String.compareToIgnoreCase but that will handle cases where
676 * strings can be null
677 *
678 * @param s1
679 * The first string
680 * @param s2
681 * The second string
682 * @return A number below, equal, or greater than zero if the first string
683 * is smaller, equal, or bigger (alphabetically) than the second
684 * one.
685 */
686 static public int compare(String s1, String s2) {
687 if (s1 != null && s2 != null) {
688 return s1.compareToIgnoreCase(s2);
689 }
690 if (s1 != null) {
691 return 1;
692 }
693 if (s2 != null) {
694 return -1;
695 }
696 return 0;
697 }
698}
This page took 0.063846 seconds and 5 git commands to generate.