1 /*****************************************************************************
2 * Copyright (c) 2007, 2015 Intel Corporation, 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
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchez-Leon - Updated for TMF
12 * Patrick Tasse - Refactoring
13 * Marc-Andre Laperle - Add time zone preference
14 *****************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
18 import java
.text
.NumberFormat
;
19 import java
.text
.SimpleDateFormat
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Calendar
;
22 import java
.util
.Date
;
23 import java
.util
.List
;
24 import java
.util
.TimeZone
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.MouseEvent
;
28 import org
.eclipse
.swt
.events
.MouseListener
;
29 import org
.eclipse
.swt
.events
.MouseMoveListener
;
30 import org
.eclipse
.swt
.events
.PaintEvent
;
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
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Control
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimePreferences
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
45 import com
.google
.common
.collect
.ImmutableList
;
48 * Implementation of the scale for the time graph view.
50 * This goes above the "gantt chart" area.
53 * @author Alvaro Sanchez-Leon
54 * @author Patrick Tasse
56 public class TimeGraphScale
extends TimeGraphBaseControl
implements
57 MouseListener
, MouseMoveListener
{
59 private static final int BASE_10
= 10;
60 private static final int X_OFFSET
= 4;
61 private static final int Y_OFFSET
= 4;
63 private static final int MIN_SECOND_FACTOR
= 20;
64 private static final int SECOND_FACTOR
= 30;
65 private static final int MAX_SECOND_FACTOR
= 30;
67 private static final int MIN_MINUTE_FACTOR
= 10;
68 private static final int MINUTE_FACTOR
= 15;
69 private static final int MAX_MINUTE_FACTOR
= 30;
71 private static final int MIN_HOUR_FACTOR
= 3;
72 private static final int HOUR_FACTOR
= 6;
73 private static final int MAX_HOUR_FACTOR
= 12;
75 private static final int MAX_DAY_FACTOR
= 10;
77 private static final int MAX_MONTH_FACTOR
= 6;
78 private static final int MONTH_FACTOR
= 6;
79 private static final int MIN_MONTH_FACTOR
= 3;
81 private static final long MICROSEC_IN_NS
= 1000;
82 private static final long MILLISEC_IN_NS
= 1000000;
83 private static final long SEC_IN_NS
= 1000000000;
84 private static final long MIN_IN_NS
= 60 * SEC_IN_NS
;
85 private static final long HOUR_IN_NS
= 60 * MIN_IN_NS
;
86 private static final long DAY_IN_NS
= 24 * HOUR_IN_NS
;
87 private static final long MONTH_IN_NS
= 31 * DAY_IN_NS
; // upper limit
88 private static final long YEAR_IN_NS
= 366 * DAY_IN_NS
; // upper limit
90 private static final double LOG10_1
= Math
.log10(1);
91 private static final double LOG10_2
= Math
.log10(2);
92 private static final double LOG10_3
= Math
.log10(3);
93 private static final double LOG10_5
= Math
.log10(5);
95 private static final Calendar GREGORIAN_CALENDAR
= Calendar
.getInstance();
97 private static final TimeDraw TIMEDRAW_NANOSEC
= new TimeDrawNanosec();
98 private static final TimeDraw TIMEDRAW_MICROSEC
= new TimeDrawMicrosec();
99 private static final TimeDraw TIMEDRAW_MILLISEC
= new TimeDrawMillisec();
100 private static final TimeDraw TIMEDRAW_SEC
= new TimeDrawSec();
101 private static final TimeDraw TIMEDRAW_ABS_NANOSEC
= new TimeDrawAbsNanoSec();
102 private static final TimeDraw TIMEDRAW_ABS_MICROSEC
= new TimeDrawAbsMicroSec();
103 private static final TimeDraw TIMEDRAW_ABS_MILLISEC
= new TimeDrawAbsMillisec();
104 private static final TimeDraw TIMEDRAW_ABS_SEC
= new TimeDrawAbsSec();
105 private static final TimeDraw TIMEDRAW_ABS_MIN
= new TimeDrawAbsMin();
106 private static final TimeDraw TIMEDRAW_ABS_HRS
= new TimeDrawAbsHrs();
107 private static final TimeDraw TIMEDRAW_ABS_DAY
= new TimeDrawAbsDay();
108 private static final TimeDraw TIMEDRAW_ABS_MONTH
= new TimeDrawAbsMonth();
109 private static final TimeDraw TIMEDRAW_ABS_YEAR
= new TimeDrawAbsYear();
110 private static final TimeDraw TIMEDRAW_NUMBER
= new TimeDrawNumber();
111 private static final TimeDraw TIMEDRAW_CYCLES
= new TimeDrawCycles();
113 private static final int DRAG_EXTERNAL
= -1;
114 private static final int NO_BUTTON
= 0;
115 private static final int LEFT_BUTTON
= 1;
117 private static final int MAX_LABEL_LENGTH
= 256;
119 private ITimeDataProvider fTimeProvider
;
120 private int fDragState
= NO_BUTTON
;
121 private int fDragX0
= 0;
122 private int fDragX
= 0;
123 private long fTime0bak
;
124 private long fTime1bak
;
125 private boolean fIsInUpdate
;
127 private List
<Integer
> fTickList
= new ArrayList
<>();
128 private List
<IMarkerEvent
> fMarkers
= null;
129 private boolean fMarkersVisible
= true;
132 * Standard constructor
135 * The parent composite object
137 * The color scheme to use
139 public TimeGraphScale(Composite parent
, TimeGraphColorScheme colors
) {
140 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.NO_FOCUS
| SWT
.DOUBLE_BUFFERED
);
141 TmfSignalManager
.register(this);
142 addMouseListener(this);
143 addMouseMoveListener(this);
144 TimeDraw
.updateTimeZone();
148 public void dispose() {
149 TmfSignalManager
.deregister(this);
154 * Assign the time provider for this scale
156 * @param timeProvider
157 * The provider to use
159 public void setTimeProvider(ITimeDataProvider timeProvider
) {
160 fTimeProvider
= timeProvider
;
164 * Get the time provider used by this scale
166 * @return The time provider
168 public ITimeDataProvider
getTimeProvider() {
169 return fTimeProvider
;
173 public Point
computeSize(int wHint
, int hHint
, boolean changed
) {
174 return super.computeSize(wHint
, fHeight
, changed
);
178 * Set the height of the scale
183 public void setHeight(int height
) {
184 if (fHeight
!= height
) {
186 getParent().layout(new Control
[] { this });
191 * Set the drag range to paint decorators
194 * The begin x-coordinate
196 * The end x-coordinate
198 public void setDragRange(int begin
, int end
) {
199 if (NO_BUTTON
== fDragState
|| DRAG_EXTERNAL
== fDragState
) {
200 fDragX0
= begin
- fTimeProvider
.getNameSpace();
201 fDragX
= end
- fTimeProvider
.getNameSpace();
202 if (begin
>= 0 || end
>= 0) {
203 fDragState
= DRAG_EXTERNAL
;
205 fDragState
= NO_BUTTON
;
212 * Get the list of visible ticks of the time axis.
214 * @return the list of visible tick x-coordinates
217 public List
<Integer
> getTickList() {
222 * Set the markers list.
225 * The markers list, or null
228 public void setMarkers(List
<IMarkerEvent
> markers
) {
233 * Set the markers visibility. The default is true.
236 * true to show the markers, false otherwise
239 public void setMarkersVisible(boolean visible
) {
240 fMarkersVisible
= visible
;
243 private long calcTimeDelta(int width
, double pixelsPerNanoSec
) {
245 double minDelta
= (pixelsPerNanoSec
== 0) ? YEAR_IN_NS
: width
/ pixelsPerNanoSec
;
247 if (fTimeProvider
!= null && fTimeProvider
.getTimeFormat() == TimeFormat
.CALENDAR
) {
248 if (minDelta
> MAX_MONTH_FACTOR
* MONTH_IN_NS
) {
250 } else if (minDelta
> MIN_MONTH_FACTOR
* MONTH_IN_NS
) {
251 unit
= MONTH_FACTOR
* MONTH_IN_NS
;
252 } else if (minDelta
> MAX_DAY_FACTOR
* DAY_IN_NS
) {
254 } else if (minDelta
> MAX_HOUR_FACTOR
* HOUR_IN_NS
) {
256 } else if (minDelta
> MIN_HOUR_FACTOR
* HOUR_IN_NS
) {
257 unit
= HOUR_FACTOR
* HOUR_IN_NS
;
258 } else if (minDelta
> MAX_MINUTE_FACTOR
* MIN_IN_NS
) {
260 } else if (minDelta
> MIN_MINUTE_FACTOR
* MIN_IN_NS
) {
261 unit
= MINUTE_FACTOR
* MIN_IN_NS
;
262 } else if (minDelta
> MAX_SECOND_FACTOR
* SEC_IN_NS
) {
264 } else if (minDelta
> MIN_SECOND_FACTOR
* SEC_IN_NS
) {
265 unit
= SECOND_FACTOR
* SEC_IN_NS
;
266 } else if (minDelta
<= 1) {
271 double log
= Math
.log10(minDelta
/ unit
);
272 long pow10
= (long) log
;
273 double remainder
= log
- pow10
;
274 if (remainder
< LOG10_1
) {
275 timeDelta
= (long) Math
.pow(BASE_10
, pow10
) * unit
;
276 } else if (remainder
< LOG10_2
) {
277 timeDelta
= 2 * (long) Math
.pow(BASE_10
, pow10
) * unit
;
278 } else if (remainder
< LOG10_3
&& unit
>= HOUR_IN_NS
&& unit
< YEAR_IN_NS
) {
279 timeDelta
= 3 * (long) Math
.pow(BASE_10
, pow10
) * unit
;
280 } else if (remainder
< LOG10_5
) {
281 timeDelta
= 5 * (long) Math
.pow(BASE_10
, pow10
) * unit
;
283 timeDelta
= 10 * (long) Math
.pow(BASE_10
, pow10
) * unit
;
285 if (timeDelta
<= 0) {
291 TimeDraw
getTimeDraw(long timeDelta
) {
293 if (fTimeProvider
!= null) {
294 switch (fTimeProvider
.getTimeFormat()) {
296 if (timeDelta
>= YEAR_IN_NS
) {
297 timeDraw
= TIMEDRAW_ABS_YEAR
;
298 } else if (timeDelta
>= MONTH_IN_NS
) {
299 timeDraw
= TIMEDRAW_ABS_MONTH
;
300 } else if (timeDelta
>= DAY_IN_NS
) {
301 timeDraw
= TIMEDRAW_ABS_DAY
;
302 } else if (timeDelta
>= HOUR_IN_NS
) {
303 timeDraw
= TIMEDRAW_ABS_HRS
;
304 } else if (timeDelta
>= MIN_IN_NS
) {
305 timeDraw
= TIMEDRAW_ABS_MIN
;
306 } else if (timeDelta
>= SEC_IN_NS
) {
307 timeDraw
= TIMEDRAW_ABS_SEC
;
308 } else if (timeDelta
>= MILLISEC_IN_NS
) {
309 timeDraw
= TIMEDRAW_ABS_MILLISEC
;
310 } else if (timeDelta
>= MICROSEC_IN_NS
) {
311 timeDraw
= TIMEDRAW_ABS_MICROSEC
;
313 timeDraw
= TIMEDRAW_ABS_NANOSEC
;
317 return TIMEDRAW_NUMBER
;
319 return TIMEDRAW_CYCLES
;
325 if (timeDelta
>= SEC_IN_NS
) {
326 timeDraw
= TIMEDRAW_SEC
;
327 } else if (timeDelta
>= MILLISEC_IN_NS
) {
328 timeDraw
= TIMEDRAW_MILLISEC
;
329 } else if (timeDelta
>= MICROSEC_IN_NS
) {
330 timeDraw
= TIMEDRAW_MICROSEC
;
332 timeDraw
= TIMEDRAW_NANOSEC
;
338 void paint(Rectangle rect
, PaintEvent e
) {
340 if (fIsInUpdate
|| null == fTimeProvider
) {
345 gc
.fillRectangle(rect
);
347 long time0
= fTimeProvider
.getTime0();
348 long time1
= fTimeProvider
.getTime1();
349 int leftSpace
= fTimeProvider
.getNameSpace();
350 int timeSpace
= fTimeProvider
.getTimeSpace();
352 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_BACKGROUND
));
353 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
354 Rectangle rect0
= new Rectangle(0, 0, 0, 0);
355 Utils
.init(rect0
, rect
);
357 // draw top left area
358 rect0
.width
= leftSpace
;
360 rect0
.width
-= X_OFFSET
;
361 Rectangle absHeaderRect
= new Rectangle(rect0
.x
, rect0
.y
, rect0
.width
, rect0
.height
);
363 rect0
.width
+= X_OFFSET
;
365 // prepare and draw right rect of the timescale
366 rect0
.x
+= leftSpace
;
367 rect0
.width
= rect
.width
- leftSpace
;
369 // draw bottom border and erase all other area
370 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1,
371 rect
.y
+ rect
.height
- 1);
373 gc
.fillRectangle(rect0
);
375 if (time1
<= time0
|| timeSpace
< 2) {
380 int numDigits
= calculateDigits(time0
, time1
);
382 int labelWidth
= gc
.getCharWidth('0') * numDigits
;
383 double pixelsPerNanoSec
= (timeSpace
<= RIGHT_MARGIN
) ?
0 :
384 (double) (timeSpace
- RIGHT_MARGIN
) / (time1
- time0
);
385 long timeDelta
= calcTimeDelta(labelWidth
, pixelsPerNanoSec
);
387 TimeDraw timeDraw
= getTimeDraw(timeDelta
);
389 // draw range decorators
390 if (DRAG_EXTERNAL
== fDragState
) {
391 int x1
= leftSpace
+ fDragX0
;
392 int x2
= leftSpace
+ fDragX
;
393 drawRangeDecorators(rect0
, gc
, x1
, x2
);
397 long selectionBegin
= fTimeProvider
.getSelectionBegin();
398 long selectionEnd
= fTimeProvider
.getSelectionEnd();
399 x1
= leftSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
400 x2
= leftSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
401 drawRangeDecorators(rect0
, gc
, x1
, x2
);
404 if (rect0
.isEmpty()) {
408 // draw time scale ticks
410 rect0
.height
= rect
.height
- Y_OFFSET
;
411 rect0
.width
= labelWidth
;
414 if (fTimeProvider
!= null && fTimeProvider
.getTimeFormat() == TimeFormat
.CALENDAR
) {
415 time
= floorToCalendar(time0
, timeDelta
);
417 time
= (time0
/ timeDelta
) * timeDelta
;
423 int y
= rect0
.y
+ rect0
.height
;
425 if (fTimeProvider
!= null && fTimeProvider
.getTimeFormat() == TimeFormat
.CALENDAR
) {
426 timeDraw
.drawAbsHeader(gc
, time
, absHeaderRect
);
429 List
<Integer
> tickList
= new ArrayList
<>();
431 int x
= rect
.x
+ leftSpace
+ (int) (Math
.floor((time
- time0
) * pixelsPerNanoSec
));
432 if (x
>= rect
.x
+ leftSpace
+ rect
.width
- rect0
.width
) {
435 if (x
>= rect
.x
+ leftSpace
) {
436 gc
.drawLine(x
, y
, x
, y
+ Y_OFFSET
);
438 if (x
+ rect0
.width
<= rect
.x
+ rect
.width
) {
439 timeDraw
.draw(gc
, time
, rect0
);
443 if (pixelsPerNanoSec
== 0 || time
> Long
.MAX_VALUE
- timeDelta
|| timeDelta
== 0) {
446 if (fTimeProvider
!= null && fTimeProvider
.getTimeFormat() == TimeFormat
.CALENDAR
) {
447 if (timeDelta
>= YEAR_IN_NS
) {
448 long millis
= time
/ MILLISEC_IN_NS
;
449 GREGORIAN_CALENDAR
.setTime(new Date(millis
));
450 GREGORIAN_CALENDAR
.add(Calendar
.YEAR
, (int) (timeDelta
/ YEAR_IN_NS
));
451 millis
= GREGORIAN_CALENDAR
.getTimeInMillis();
452 time
= millis
* MILLISEC_IN_NS
;
453 } else if (timeDelta
>= MONTH_IN_NS
) {
454 long millis
= time
/ MILLISEC_IN_NS
;
455 GREGORIAN_CALENDAR
.setTime(new Date(millis
));
456 GREGORIAN_CALENDAR
.add(Calendar
.MONTH
, (int) (timeDelta
/ MONTH_IN_NS
));
457 millis
= GREGORIAN_CALENDAR
.getTimeInMillis();
458 time
= millis
* MILLISEC_IN_NS
;
459 } else if (timeDelta
>= DAY_IN_NS
) {
460 long millis
= time
/ MILLISEC_IN_NS
;
461 GREGORIAN_CALENDAR
.setTime(new Date(millis
));
462 GREGORIAN_CALENDAR
.add(Calendar
.DAY_OF_MONTH
, (int) (timeDelta
/ DAY_IN_NS
));
463 millis
= GREGORIAN_CALENDAR
.getTimeInMillis();
464 time
= millis
* MILLISEC_IN_NS
;
472 fTickList
= tickList
;
474 // draw marker labels
475 if (fMarkersVisible
) {
476 drawMarkerLabels(fMarkers
, rect
, gc
, time0
, leftSpace
, pixelsPerNanoSec
);
480 private static void drawMarkerLabels(List
<IMarkerEvent
> markerEvents
, Rectangle rect
, GC gc
, long time0
, int leftSpace
, double pixelsPerNanoSec
) {
481 if (markerEvents
== null) {
484 for (IMarkerEvent markerEvent
: markerEvents
) {
485 String label
= markerEvent
.getLabel();
486 if (label
!= null && markerEvent
.getEntry() == null) {
487 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
488 int x
= rect
.x
+ leftSpace
+ (int) (Math
.floor((markerEvent
.getTime() - time0
) * pixelsPerNanoSec
));
489 int y
= rect
.y
+ rect
.height
- gc
.stringExtent(" ").y
+ 2; //$NON-NLS-1$
490 gc
.setForeground(markerEvent
.getColor());
491 Utils
.drawText(gc
, label
, x
, y
, true);
496 private static void drawRangeDecorators(Rectangle rect
, GC gc
, int x1
, int x2
) {
497 int y1
= rect
.y
+ rect
.height
- 9;
498 int y2
= rect
.y
+ rect
.height
- 5;
499 int ym
= (y1
+ y2
) / 2;
502 gc
.drawLine(x1
- 2, y1
, x1
- 2, y2
);
503 gc
.drawLine(x1
- 3, y1
, x1
- 1, y1
);
504 gc
.drawLine(x1
+ 1, y1
, x1
+ 1, y2
);
506 if (x2
>= rect
.x
&& Math
.abs(x2
- x1
- 2) > 3) {
508 gc
.drawLine(x2
- 2, y1
, x2
- 2, y2
);
509 gc
.drawLine(x2
- 3, y1
, x2
- 1, y1
);
511 if (x2
>= rect
.x
&& Math
.abs(x2
- x1
+ 3) > 3) {
513 gc
.drawLine(x2
+ 1, y1
, x2
+ 3, y1
);
514 gc
.drawLine(x2
+ 3, y1
, x2
+ 3, ym
);
515 gc
.drawLine(x2
+ 1, ym
, x2
+ 3, ym
);
516 gc
.drawLine(x2
+ 1, ym
, x2
+ 1, y2
);
517 gc
.drawLine(x2
+ 1, y2
, x2
+ 3, y2
);
521 private static long floorToCalendar(long time
, long timeDelta
) {
524 if (timeDelta
>= YEAR_IN_NS
) {
525 GREGORIAN_CALENDAR
.setTime(new Date(ret
/ MILLISEC_IN_NS
));
526 int year
= GREGORIAN_CALENDAR
.get(Calendar
.YEAR
);
527 int yearDelta
= (int) (timeDelta
/ YEAR_IN_NS
);
528 year
= (year
/ yearDelta
) * yearDelta
;
529 GREGORIAN_CALENDAR
.set(Calendar
.YEAR
, year
);
530 GREGORIAN_CALENDAR
.set(Calendar
.MONTH
, 0); // January 1st of year
531 GREGORIAN_CALENDAR
.set(Calendar
.DAY_OF_MONTH
, 1);
532 GREGORIAN_CALENDAR
.set(Calendar
.HOUR_OF_DAY
, 0);
533 GREGORIAN_CALENDAR
.set(Calendar
.MINUTE
, 0);
534 GREGORIAN_CALENDAR
.set(Calendar
.SECOND
, 0);
535 GREGORIAN_CALENDAR
.set(Calendar
.MILLISECOND
, 0);
536 ret
= GREGORIAN_CALENDAR
.getTimeInMillis() * MILLISEC_IN_NS
;
537 } else if (timeDelta
>= MONTH_IN_NS
) {
538 GREGORIAN_CALENDAR
.setTime(new Date(ret
/ MILLISEC_IN_NS
));
539 int month
= GREGORIAN_CALENDAR
.get(Calendar
.MONTH
);
540 int monthDelta
= (int) (timeDelta
/ MONTH_IN_NS
);
541 month
= (month
/ monthDelta
) * monthDelta
;
542 GREGORIAN_CALENDAR
.set(Calendar
.MONTH
, month
);
543 GREGORIAN_CALENDAR
.set(Calendar
.DAY_OF_MONTH
, 1); // 1st of month
544 GREGORIAN_CALENDAR
.set(Calendar
.HOUR_OF_DAY
, 0);
545 GREGORIAN_CALENDAR
.set(Calendar
.MINUTE
, 0);
546 GREGORIAN_CALENDAR
.set(Calendar
.SECOND
, 0);
547 GREGORIAN_CALENDAR
.set(Calendar
.MILLISECOND
, 0);
548 ret
= GREGORIAN_CALENDAR
.getTimeInMillis() * MILLISEC_IN_NS
;
550 long offset
= GREGORIAN_CALENDAR
.getTimeZone().getOffset(ret
/ MILLISEC_IN_NS
) * MILLISEC_IN_NS
;
552 ret
= (ret
/ timeDelta
) * timeDelta
;
558 private int calculateDigits(long time0
, long time1
) {
560 long timeRange
= time1
- time0
;
562 if (fTimeProvider
.getTimeFormat() == TimeFormat
.CALENDAR
) {
563 // Calculate the number of digits to represent the minutes provided
567 if (timeRange
< 10000) {
568 // HH:11:222:333:444__
570 } else if (timeRange
< 10000000) {
575 long sec
= time1
/ SEC_IN_NS
;
576 numDigits
= Long
.toString(sec
).length();
577 int thousandGroups
= (numDigits
- 1) / 3;
578 numDigits
+= thousandGroups
;
579 numDigits
+= 12; // .000 000 000
580 if (fTimeProvider
.getTimeFormat() == TimeFormat
.CYCLES
) {
581 numDigits
+= Messages
.Utils_ClockCyclesUnit
.length();
589 public void mouseDown(MouseEvent e
) {
590 getParent().setFocus();
591 if (fDragState
== NO_BUTTON
&& null != fTimeProvider
) {
592 int x
= e
.x
- fTimeProvider
.getNameSpace();
593 if (LEFT_BUTTON
== e
.button
&& x
> 0) {
595 fDragState
= LEFT_BUTTON
;
599 } else if (x
> getSize().x
- fTimeProvider
.getNameSpace()) {
600 x
= getSize().x
- fTimeProvider
.getNameSpace();
604 fTime0bak
= fTimeProvider
.getTime0();
605 fTime1bak
= fTimeProvider
.getTime1();
610 public void mouseUp(MouseEvent e
) {
611 if (e
.button
== LEFT_BUTTON
&& fDragState
== LEFT_BUTTON
) {
613 fDragState
= NO_BUTTON
;
615 // Notify time provider to check the need for listener notification
616 if (fDragX
!= fDragX0
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
617 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
623 public void mouseMove(MouseEvent e
) {
624 if (fDragX0
< 0 || fDragState
== NO_BUTTON
|| fTimeProvider
== null) {
627 Point size
= getSize();
628 int leftSpace
= fTimeProvider
.getNameSpace();
629 int x
= e
.x
- leftSpace
;
630 if (LEFT_BUTTON
== fDragState
) {
631 if (x
> 0 && size
.x
> leftSpace
&& fDragX
!= x
) {
633 if (fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
636 long interval
= (long) ((fTime1bak
- fTime0bak
) * ((double) fDragX0
/ fDragX
));
637 if (interval
== Long
.MAX_VALUE
) {
638 fTimeProvider
.setStartFinishTimeNotify(fTime0bak
, Long
.MAX_VALUE
);
640 long time1
= fTime0bak
+ (long) ((fTime1bak
- fTime0bak
) * ((double) fDragX0
/ fDragX
));
641 fTimeProvider
.setStartFinishTimeNotify(fTime0bak
, time1
);
648 public void mouseDoubleClick(MouseEvent e
) {
649 if (e
.button
== 1 && null != fTimeProvider
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1() && (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
650 fTimeProvider
.resetStartFinishTime();
651 fTime0bak
= fTimeProvider
.getTime0();
652 fTime1bak
= fTimeProvider
.getTime1();
657 * Update the display to use the updated timestamp format
660 * the incoming signal
663 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
664 TimeDraw
.updateTimeZone();
665 Utils
.updateTimeZone();
670 abstract class TimeDraw
{
671 protected static final long MICROSEC_IN_NS
= 1000;
672 protected static final long MILLISEC_IN_NS
= 1000000;
673 protected static final long MILLISEC_IN_US
= 1000;
674 protected static final long SEC_IN_NS
= 1000000000;
675 protected static final long SEC_IN_MS
= 1000;
676 private static final String S
= ""; //$NON-NLS-1$
677 private static final String S0
= "0"; //$NON-NLS-1$
678 private static final String S00
= "00"; //$NON-NLS-1$
679 protected static final long PAD_1000
= 1000;
680 protected static final SimpleDateFormat SEC_FORMAT_HEADER
=
681 new SimpleDateFormat("yyyy MMM dd");//$NON-NLS-1$
682 protected static final SimpleDateFormat SEC_FORMAT
=
683 new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
684 protected static final SimpleDateFormat MIN_FORMAT_HEADER
=
685 new SimpleDateFormat("yyyy MMM dd"); //$NON-NLS-1$
686 protected static final SimpleDateFormat MIN_FORMAT
=
687 new SimpleDateFormat("HH:mm"); //$NON-NLS-1$
688 protected static final SimpleDateFormat HOURS_FORMAT_HEADER
=
689 new SimpleDateFormat("yyyy"); //$NON-NLS-1$
690 protected static final SimpleDateFormat HOURS_FORMAT
=
691 new SimpleDateFormat("MMM dd HH:mm"); //$NON-NLS-1$
692 protected static final SimpleDateFormat DAY_FORMAT_HEADER
=
693 new SimpleDateFormat("yyyy"); //$NON-NLS-1$
694 protected static final SimpleDateFormat DAY_FORMAT
=
695 new SimpleDateFormat("MMM dd"); //$NON-NLS-1$
696 protected static final SimpleDateFormat MONTH_FORMAT
=
697 new SimpleDateFormat("yyyy MMM"); //$NON-NLS-1$
698 protected static final SimpleDateFormat YEAR_FORMAT
=
699 new SimpleDateFormat("yyyy"); //$NON-NLS-1$
701 protected static final List
<SimpleDateFormat
> formats
;
704 ImmutableList
.Builder
<SimpleDateFormat
> formatArrayBuilder
= ImmutableList
.<SimpleDateFormat
> builder();
705 formatArrayBuilder
.add(SEC_FORMAT
);
706 formatArrayBuilder
.add(SEC_FORMAT_HEADER
);
707 formatArrayBuilder
.add(MIN_FORMAT
);
708 formatArrayBuilder
.add(MIN_FORMAT_HEADER
);
709 formatArrayBuilder
.add(HOURS_FORMAT
);
710 formatArrayBuilder
.add(HOURS_FORMAT_HEADER
);
711 formatArrayBuilder
.add(DAY_FORMAT
);
712 formatArrayBuilder
.add(DAY_FORMAT_HEADER
);
713 formatArrayBuilder
.add(MONTH_FORMAT
);
714 formatArrayBuilder
.add(YEAR_FORMAT
);
715 formats
= formatArrayBuilder
.build();
719 * Updates the timezone using the preferences.
721 public static void updateTimeZone() {
722 final TimeZone timeZone
= TmfTimePreferences
.getTimeZone();
723 for (SimpleDateFormat sdf
: formats
) {
725 sdf
.setTimeZone(timeZone
);
730 static String
sep(long n
) {
731 StringBuilder retVal
= new StringBuilder();
732 String s
= Long
.toString(n
);
733 for (int i
= 0; i
< s
.length(); i
++) {
734 int pos
= s
.length() - i
- 1;
735 retVal
.append(s
.charAt(i
));
736 if (pos
% 3 == 0 && pos
!= 0) {
740 return retVal
.toString();
743 static String
pad(long n
) {
747 } else if (n
< 100) {
755 public abstract int draw(GC gc
, long time
, Rectangle rect
);
758 * Override to draw absolute time header. This is for the time information
759 * not shown in the draw of each tick
765 * @param absHeaderRect
768 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle absHeaderRect
) {
771 protected void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
, SimpleDateFormat dateFormat
) {
773 synchronized (dateFormat
) {
774 header
= dateFormat
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
776 int headerwidth
= gc
.stringExtent(header
).x
+ 4;
777 if (headerwidth
<= rect
.width
) {
778 rect
.x
+= (rect
.width
- headerwidth
);
779 Utils
.drawText(gc
, header
, rect
, true);
784 class TimeDrawSec
extends TimeDraw
{
786 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
787 long sec
= nanosec
/ SEC_IN_NS
;
788 return Utils
.drawText(gc
, sep(sec
), rect
, true);
792 class TimeDrawMillisec
extends TimeDraw
{
794 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
795 long millisec
= nanosec
/ MILLISEC_IN_NS
;
796 long ms
= millisec
% PAD_1000
;
797 long sec
= millisec
/ SEC_IN_MS
;
798 return Utils
.drawText(gc
, sep(sec
) + "." + pad(ms
), rect
, true); //$NON-NLS-1$
802 class TimeDrawMicrosec
extends TimeDraw
{
804 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
805 long microsec
= nanosec
/ MICROSEC_IN_NS
;
806 long us
= microsec
% PAD_1000
;
807 long millisec
= microsec
/ MILLISEC_IN_US
;
808 long ms
= millisec
% PAD_1000
;
809 long sec
= millisec
/ SEC_IN_MS
;
810 return Utils
.drawText(gc
, sep(sec
) + "." + pad(ms
) + " " + pad(us
), rect
, true); //$NON-NLS-1$ //$NON-NLS-2$
814 class TimeDrawNanosec
extends TimeDraw
{
816 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
817 long ns
= nanosec
% PAD_1000
;
818 long microsec
= nanosec
/ MICROSEC_IN_NS
;
819 long us
= microsec
% PAD_1000
;
820 long millisec
= microsec
/ MILLISEC_IN_US
;
821 long ms
= millisec
% PAD_1000
;
822 long sec
= millisec
/ SEC_IN_MS
;
823 return Utils
.drawText(gc
, sep(sec
) + "." + pad(ms
) + " " + pad(us
) + " " + pad(ns
), rect
, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
827 class TimeDrawAbsYear
extends TimeDraw
{
829 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
831 synchronized (YEAR_FORMAT
) {
832 stime
= YEAR_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
834 return Utils
.drawText(gc
, stime
, rect
, true);
838 class TimeDrawAbsMonth
extends TimeDraw
{
840 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
842 synchronized (MONTH_FORMAT
) {
843 stime
= MONTH_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
845 return Utils
.drawText(gc
, stime
, rect
, true);
849 class TimeDrawAbsDay
extends TimeDraw
{
851 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
853 synchronized (DAY_FORMAT
) {
854 stime
= DAY_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
856 return Utils
.drawText(gc
, stime
, rect
, true);
860 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
861 drawAbsHeader(gc
, nanosec
, rect
, DAY_FORMAT_HEADER
);
865 class TimeDrawAbsHrs
extends TimeDraw
{
867 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
869 synchronized (HOURS_FORMAT
) {
870 stime
= HOURS_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
872 return Utils
.drawText(gc
, stime
, rect
, true);
876 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
877 drawAbsHeader(gc
, nanosec
, rect
, HOURS_FORMAT_HEADER
);
881 class TimeDrawAbsMin
extends TimeDraw
{
883 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
885 synchronized (MIN_FORMAT
) {
886 stime
= MIN_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
888 return Utils
.drawText(gc
, stime
, rect
, true);
892 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
893 drawAbsHeader(gc
, nanosec
, rect
, MIN_FORMAT_HEADER
);
897 class TimeDrawAbsSec
extends TimeDraw
{
899 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
901 synchronized (SEC_FORMAT
) {
902 stime
= SEC_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
904 return Utils
.drawText(gc
, stime
, rect
, true);
908 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
909 drawAbsHeader(gc
, nanosec
, rect
, SEC_FORMAT_HEADER
);
913 class TimeDrawAbsMillisec
extends TimeDraw
{
915 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
917 synchronized (SEC_FORMAT
) {
918 stime
= SEC_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
920 String ns
= Utils
.formatNs(nanosec
, Resolution
.MILLISEC
);
921 return Utils
.drawText(gc
, stime
+ "." + ns
, rect
, true); //$NON-NLS-1$
925 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
926 drawAbsHeader(gc
, nanosec
, rect
, SEC_FORMAT_HEADER
);
930 class TimeDrawAbsMicroSec
extends TimeDraw
{
932 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
934 synchronized (SEC_FORMAT
) {
935 stime
= SEC_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
937 String micr
= Utils
.formatNs(nanosec
, Resolution
.MICROSEC
);
938 return Utils
.drawText(gc
, stime
+ "." + micr
, rect
, true); //$NON-NLS-1$
942 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
943 drawAbsHeader(gc
, nanosec
, rect
, SEC_FORMAT_HEADER
);
947 class TimeDrawAbsNanoSec
extends TimeDraw
{
949 public int draw(GC gc
, long nanosec
, Rectangle rect
) {
951 synchronized (SEC_FORMAT
) {
952 stime
= SEC_FORMAT
.format(new Date(nanosec
/ MILLISEC_IN_NS
));
954 String ns
= Utils
.formatNs(nanosec
, Resolution
.NANOSEC
);
955 return Utils
.drawText(gc
, stime
+ "." + ns
, rect
, true); //$NON-NLS-1$
959 public void drawAbsHeader(GC gc
, long nanosec
, Rectangle rect
) {
960 drawAbsHeader(gc
, nanosec
, rect
, SEC_FORMAT_HEADER
);
964 class TimeDrawNumber
extends TimeDraw
{
966 public int draw(GC gc
, long time
, Rectangle rect
) {
967 String stime
= NumberFormat
.getInstance().format(time
);
968 return Utils
.drawText(gc
, stime
, rect
, true);
972 class TimeDrawCycles
extends TimeDraw
{
974 public int draw(GC gc
, long time
, Rectangle rect
) {
975 String stime
= Utils
.formatTime(time
, TimeFormat
.CYCLES
, Resolution
.SECONDS
);
976 return Utils
.drawText(gc
, stime
, rect
, true);