tmf: Add missing Javadoc to tmf.ui
[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;
290 while (len > 0) {
291 textWidth = gc.stringExtent(text.substring(0, len)).x;
292 if (textWidth <= width) {
293 break;
294 }
295 isCentered = false;
296 len--;
297 }
298 if (len > 0) {
299 if (isCentered) {
300 x += (width - textWidth) / 2;
301 }
302 gc.drawText(text.substring(0, len), x, y, isTransparent);
303 }
304 return len;
305 }
306
6151d86c
PT
307 /**
308 * Formats time in format: MM:SS:NNN
309 *
310 * @param time time
311 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
312 * @param resolution the resolution
313 * @return the formatted time
314 */
315 static public String formatTime(long time, TimeFormat format, Resolution resolution) {
316 // if format is absolute (Calendar)
026664b7 317 if (format == TimeFormat.CALENDAR) {
6151d86c 318 return formatTimeAbs(time, resolution);
026664b7
XR
319 } else if (format == TimeFormat.NUMBER) {
320 return NumberFormat.getInstance().format(time);
6151d86c
PT
321 }
322
323 StringBuffer str = new StringBuffer();
324 boolean neg = time < 0;
325 if (neg) {
326 time = -time;
327 str.append('-');
328 }
329
330 long sec = (long) (time * 1E-9);
331 // TODO: Expand to make it possible to select the minute, second, nanosecond format
332 //printing minutes is suppressed just sec and ns
333 // if (sec / 60 < 10)
334 // str.append('0');
335 // str.append(sec / 60);
336 // str.append(':');
337 // sec %= 60;
338 // if (sec < 10)
339 // str.append('0');
340 str.append(sec);
341 String ns = formatNs(time, resolution);
342 if (!ns.equals("")) { //$NON-NLS-1$
343 str.append('.');
344 str.append(ns);
345 }
346
347 return str.toString();
348 }
349
350 /**
351 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
352 *
353 * @param absTime
354 * The source time, in ns
355 * @return the formatted date
356 */
357 public static String formatDate(long absTime) {
358 String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));
359 return sdate;
360 }
361
362 /**
363 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
364 *
365 * @param time
366 * The source time, in ns
367 * @param res
368 * The resolution to use
369 * @return the formatted time
370 */
371 static public String formatTimeAbs(long time, Resolution res) {
372 StringBuffer str = new StringBuffer();
373
374 // format time from nanoseconds to calendar time HH:MM:SS
375 String stime = stimeformat.format(new Date((long) (time * 1E-6)));
376 str.append(stime);
377 str.append('.');
378 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
379 // the Resolution
380 str.append(formatNs(time, res));
381 return str.toString();
382 }
383
384 /**
385 * Obtains the remainder fraction on unit Seconds of the entered value in
386 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
387 * seconds can be obtained by removing the last 9 digits: 1241207054 the
388 * fractional portion of seconds, expressed in ns is: 171080214
389 *
390 * @param time
391 * The source time in ns
392 * @param res
393 * The Resolution to use
394 * @return the formatted nanosec
395 */
396 public static String formatNs(long time, Resolution res) {
397 StringBuffer str = new StringBuffer();
398 boolean neg = time < 0;
399 if (neg) {
400 time = -time;
401 }
402
403 // The following approach could be used although performance
404 // decreases in half.
405 // String strVal = String.format("%09d", time);
406 // String tmp = strVal.substring(strVal.length() - 9);
407
408 long ns = time;
409 ns %= 1000000000;
410 if (ns < 10) {
411 str.append("00000000"); //$NON-NLS-1$
412 } else if (ns < 100) {
413 str.append("0000000"); //$NON-NLS-1$
414 } else if (ns < 1000) {
415 str.append("000000"); //$NON-NLS-1$
416 } else if (ns < 10000) {
417 str.append("00000"); //$NON-NLS-1$
418 } else if (ns < 100000) {
419 str.append("0000"); //$NON-NLS-1$
420 } else if (ns < 1000000) {
421 str.append("000"); //$NON-NLS-1$
422 } else if (ns < 10000000) {
423 str.append("00"); //$NON-NLS-1$
424 } else if (ns < 100000000) {
425 str.append("0"); //$NON-NLS-1$
426 }
427 str.append(ns);
428
429 if (res == Resolution.MILLISEC) {
430 return str.substring(0, 3);
431 } else if (res == Resolution.MICROSEC) {
432 return str.substring(0, 6);
433 } else if (res == Resolution.NANOSEC) {
434 return str.substring(0, 9);
435 }
436 return ""; //$NON-NLS-1$
437 }
438
439 /**
440 * FIXME Currently does nothing.
441 *
442 * @param opt
443 * The option name
444 * @param def
445 * The option value
446 * @param min
447 * The minimal accepted value
448 * @param max
449 * The maximal accepted value
450 * @return The value that was read
451 */
452 static public int loadIntOption(String opt, int def, int min, int max) {
453 // int val =
454 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
455 // if (0 == val)
456 // val = def;
457 // if (val < min)
458 // val = min;
459 // if (val > max)
460 // val = max;
461 return def;
462 }
463
464 /**
465 * FIXME currently does nothing
466 *
467 * @param opt
468 * The option name
469 * @param val
470 * The option value
471 */
472 static public void saveIntOption(String opt, int val) {
473 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
474 }
475
476 static ITimeEvent getFirstEvent(ITimeGraphEntry entry) {
477 if (null == entry || ! entry.hasTimeEvents()) {
478 return null;
479 }
480 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator();
481 if (iterator != null && iterator.hasNext()) {
482 return iterator.next();
483 }
484 return null;
485 }
486
487 /**
488 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
489 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
490 * </list>
491 *
492 * @param entry
493 * @param time
494 * @param n
495 * @return
496 */
497 static ITimeEvent findEvent(ITimeGraphEntry entry, long time, int n) {
498 if (null == entry || ! entry.hasTimeEvents()) {
499 return null;
500 }
501 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator();
502 if (iterator == null) {
503 return null;
504 }
505 ITimeEvent nextEvent = null;
506 ITimeEvent currEvent = null;
507 ITimeEvent prevEvent = null;
508
509 while (iterator.hasNext()) {
510 nextEvent = iterator.next();
511 long nextStartTime = nextEvent.getTime();
512
513 if (nextStartTime > time) {
514 break;
515 }
516
517 if (currEvent == null || currEvent.getTime() != nextStartTime) {
518 prevEvent = currEvent;
519 currEvent = nextEvent;
520 }
521 }
522
523 if (n == -1) { //previous
524 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
525 return prevEvent;
526 }
527 return currEvent;
528 } else if (n == 0) { //current
529 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
530 return currEvent;
531 }
532 return null;
533 } else if (n == 1) { //next
534 if (nextEvent != null && nextEvent.getTime() > time) {
535 return nextEvent;
536 }
537 return null;
538 } else if (n == 2) { //current or previous when in empty space
539 return currEvent;
540 }
541
542 return null;
543 }
544
545 /**
546 * Pretty-print a method signature.
547 *
548 * @param sig
549 * The original signature
550 * @return The pretty signature
551 */
552 static public String fixMethodSignature(String sig) {
553 int pos = sig.indexOf('(');
554 if (pos >= 0) {
555 String ret = sig.substring(0, pos);
556 sig = sig.substring(pos);
557 sig = sig + " " + ret; //$NON-NLS-1$
558 }
559 return sig;
560 }
561
562 /**
563 * Restore an original method signature from a pretty-printed one.
564 *
565 * @param sig
566 * The pretty-printed signature
567 * @return The original method signature
568 */
569 static public String restoreMethodSignature(String sig) {
570 String ret = ""; //$NON-NLS-1$
571 int pos = sig.indexOf('(');
572 if (pos >= 0) {
573 ret = sig.substring(0, pos);
574 sig = sig.substring(pos + 1);
575 }
576 pos = sig.indexOf(')');
577 if (pos >= 0) {
578 sig = sig.substring(0, pos);
579 }
580 String args[] = sig.split(","); //$NON-NLS-1$
581 StringBuffer result = new StringBuffer("("); //$NON-NLS-1$
582 for (int i = 0; i < args.length; i++) {
583 String arg = args[i].trim();
584 if (arg.length() == 0 && args.length == 1) {
585 break;
586 }
587 result.append(getTypeSignature(arg));
588 }
589 result.append(")").append(getTypeSignature(ret)); //$NON-NLS-1$
590 return result.toString();
591 }
592
593 /**
594 * Get the mangled type information from an array of types.
595 *
596 * @param type
597 * The types to convert. See method implementation for what it
598 * expects.
599 * @return The mangled string of types
600 */
601 static public String getTypeSignature(String type) {
602 int dim = 0;
603 for (int j = 0; j < type.length(); j++) {
604 if (type.charAt(j) == '[') {
605 dim++;
606 }
607 }
608 int pos = type.indexOf('[');
609 if (pos >= 0) {
610 type = type.substring(0, pos);
611 }
612 StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$
613 for (int j = 0; j < dim; j++)
614 {
615 sig.append("["); //$NON-NLS-1$
616 }
617 if (type.equals("boolean")) { //$NON-NLS-1$
618 sig.append('Z');
619 } else if (type.equals("byte")) { //$NON-NLS-1$
620 sig.append('B');
621 } else if (type.equals("char")) { //$NON-NLS-1$
622 sig.append('C');
623 } else if (type.equals("short")) { //$NON-NLS-1$
624 sig.append('S');
625 } else if (type.equals("int")) { //$NON-NLS-1$
626 sig.append('I');
627 } else if (type.equals("long")) { //$NON-NLS-1$
628 sig.append('J');
629 } else if (type.equals("float")) { //$NON-NLS-1$
630 sig.append('F');
631 } else if (type.equals("double")) { //$NON-NLS-1$
632 sig.append('D');
633 } else if (type.equals("void")) { //$NON-NLS-1$
634 sig.append('V');
635 }
636 else {
637 sig.append('L').append(type.replace('.', '/')).append(';');
638 }
639 return sig.toString();
640 }
641
642 /**
643 * Compare two doubles together.
644 *
645 * @param d1
646 * First double
647 * @param d2
648 * Second double
649 * @return 1 if they are different, and 0 if they are *exactly* the same.
650 * Because of the way doubles are stored, it's possible for the
651 * same number obtained in two different ways to actually look
652 * different.
653 */
654 static public int compare(double d1, double d2) {
655 if (d1 > d2) {
656 return 1;
657 }
658 if (d1 < d2) {
659 return 1;
660 }
661 return 0;
662 }
663
664 /**
665 * Compare two character strings alphabetically. This is simply a wrapper
666 * around String.compareToIgnoreCase but that will handle cases where
667 * strings can be null
668 *
669 * @param s1
670 * The first string
671 * @param s2
672 * The second string
673 * @return A number below, equal, or greater than zero if the first string
674 * is smaller, equal, or bigger (alphabetically) than the second
675 * one.
676 */
677 static public int compare(String s1, String s2) {
678 if (s1 != null && s2 != null) {
679 return s1.compareToIgnoreCase(s2);
680 }
681 if (s1 != null) {
682 return 1;
683 }
684 if (s2 != null) {
685 return -1;
686 }
687 return 0;
688 }
689}
This page took 0.05259 seconds and 5 git commands to generate.