f99d62b53938b7407a7f097a71ff41096cc5461f
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / widgets / TimeGraphScale.java
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
7 *
8 * Contributors:
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 *****************************************************************************/
15
16 package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets;
17
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;
25
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;
44
45 import com.google.common.collect.ImmutableList;
46
47 /**
48 * Implementation of the scale for the time graph view.
49 *
50 * This goes above the "gantt chart" area.
51 *
52 * @version 1.0
53 * @author Alvaro Sanchez-Leon
54 * @author Patrick Tasse
55 */
56 public class TimeGraphScale extends TimeGraphBaseControl implements
57 MouseListener, MouseMoveListener {
58
59 private static final int BASE_10 = 10;
60 private static final int X_OFFSET = 4;
61 private static final int Y_OFFSET = 4;
62
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;
66
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;
70
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;
74
75 private static final int MAX_DAY_FACTOR = 10;
76
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;
80
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
89
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);
94
95 private static final Calendar GREGORIAN_CALENDAR = Calendar.getInstance();
96
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();
112
113 private static final int DRAG_EXTERNAL = -1;
114 private static final int NO_BUTTON = 0;
115 private static final int LEFT_BUTTON = 1;
116
117 private static final int MAX_LABEL_LENGTH = 256;
118
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;
126 private int fHeight;
127 private List<Integer> fTickList = new ArrayList<>();
128 private List<IMarkerEvent> fMarkers = null;
129 private boolean fMarkersVisible = true;
130
131 /**
132 * Standard constructor
133 *
134 * @param parent
135 * The parent composite object
136 * @param colors
137 * The color scheme to use
138 */
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();
145 }
146
147 @Override
148 public void dispose() {
149 TmfSignalManager.deregister(this);
150 super.dispose();
151 }
152
153 /**
154 * Assign the time provider for this scale
155 *
156 * @param timeProvider
157 * The provider to use
158 */
159 public void setTimeProvider(ITimeDataProvider timeProvider) {
160 fTimeProvider = timeProvider;
161 }
162
163 /**
164 * Get the time provider used by this scale
165 *
166 * @return The time provider
167 */
168 public ITimeDataProvider getTimeProvider() {
169 return fTimeProvider;
170 }
171
172 @Override
173 public Point computeSize(int wHint, int hHint, boolean changed) {
174 return super.computeSize(wHint, fHeight, changed);
175 }
176
177 /**
178 * Set the height of the scale
179 *
180 * @param height
181 * The height to use
182 */
183 public void setHeight(int height) {
184 if (fHeight != height) {
185 fHeight = height;
186 getParent().layout(new Control[] { this });
187 }
188 }
189
190 /**
191 * Set the drag range to paint decorators
192 *
193 * @param begin
194 * The begin x-coordinate
195 * @param end
196 * The end x-coordinate
197 */
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;
204 } else {
205 fDragState = NO_BUTTON;
206 }
207 }
208 redraw();
209 }
210
211 /**
212 * Get the list of visible ticks of the time axis.
213 *
214 * @return the list of visible tick x-coordinates
215 * @since 2.0
216 */
217 public List<Integer> getTickList() {
218 return fTickList;
219 }
220
221 /**
222 * Set the markers list.
223 *
224 * @param markers
225 * The markers list, or null
226 * @since 2.0
227 */
228 public void setMarkers(List<IMarkerEvent> markers) {
229 fMarkers = markers;
230 }
231
232 /**
233 * Set the markers visibility. The default is true.
234 *
235 * @param visible
236 * true to show the markers, false otherwise
237 * @since 2.0
238 */
239 public void setMarkersVisible(boolean visible) {
240 fMarkersVisible = visible;
241 }
242
243 private long calcTimeDelta(int width, double pixelsPerNanoSec) {
244 long timeDelta;
245 double minDelta = (pixelsPerNanoSec == 0) ? YEAR_IN_NS : width / pixelsPerNanoSec;
246 long unit = 1;
247 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
248 if (minDelta > MAX_MONTH_FACTOR * MONTH_IN_NS) {
249 unit = YEAR_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) {
253 unit = MONTH_IN_NS;
254 } else if (minDelta > MAX_HOUR_FACTOR * HOUR_IN_NS) {
255 unit = DAY_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) {
259 unit = HOUR_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) {
263 unit = MIN_IN_NS;
264 } else if (minDelta > MIN_SECOND_FACTOR * SEC_IN_NS) {
265 unit = SECOND_FACTOR * SEC_IN_NS;
266 } else if (minDelta <= 1) {
267 timeDelta = 1;
268 return timeDelta;
269 }
270 }
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;
282 } else {
283 timeDelta = 10 * (long) Math.pow(BASE_10, pow10) * unit;
284 }
285 if (timeDelta <= 0) {
286 timeDelta = 1;
287 }
288 return timeDelta;
289 }
290
291 TimeDraw getTimeDraw(long timeDelta) {
292 TimeDraw timeDraw;
293 if (fTimeProvider != null) {
294 switch (fTimeProvider.getTimeFormat()) {
295 case CALENDAR:
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;
312 } else {
313 timeDraw = TIMEDRAW_ABS_NANOSEC;
314 }
315 return timeDraw;
316 case NUMBER:
317 return TIMEDRAW_NUMBER;
318 case CYCLES:
319 return TIMEDRAW_CYCLES;
320 case RELATIVE:
321 default:
322 }
323
324 }
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;
331 } else {
332 timeDraw = TIMEDRAW_NANOSEC;
333 }
334 return timeDraw;
335 }
336
337 @Override
338 void paint(Rectangle rect, PaintEvent e) {
339
340 if (fIsInUpdate || null == fTimeProvider) {
341 return;
342 }
343
344 GC gc = e.gc;
345 gc.fillRectangle(rect);
346
347 long time0 = fTimeProvider.getTime0();
348 long time1 = fTimeProvider.getTime1();
349 int leftSpace = fTimeProvider.getNameSpace();
350 int timeSpace = fTimeProvider.getTimeSpace();
351
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);
356
357 // draw top left area
358 rect0.width = leftSpace;
359 rect0.x += X_OFFSET;
360 rect0.width -= X_OFFSET;
361 Rectangle absHeaderRect = new Rectangle(rect0.x, rect0.y, rect0.width, rect0.height);
362 rect0.x -= X_OFFSET;
363 rect0.width += X_OFFSET;
364
365 // prepare and draw right rect of the timescale
366 rect0.x += leftSpace;
367 rect0.width = rect.width - leftSpace;
368
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);
372 rect0.height--;
373 gc.fillRectangle(rect0);
374
375 if (time1 <= time0 || timeSpace < 2) {
376 fTickList.clear();
377 return;
378 }
379
380 int numDigits = calculateDigits(time0, time1);
381
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);
386
387 TimeDraw timeDraw = getTimeDraw(timeDelta);
388
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);
394 } else {
395 int x1;
396 int 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);
402 }
403
404 if (rect0.isEmpty()) {
405 return;
406 }
407
408 // draw time scale ticks
409 rect0.y = rect.y;
410 rect0.height = rect.height - Y_OFFSET;
411 rect0.width = labelWidth;
412
413 long time;
414 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
415 time = floorToCalendar(time0, timeDelta);
416 } else {
417 time = (time0 / timeDelta) * timeDelta;
418 if (time != time0) {
419 time += timeDelta;
420 }
421 }
422
423 int y = rect0.y + rect0.height;
424
425 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
426 timeDraw.drawAbsHeader(gc, time, absHeaderRect);
427 }
428
429 List<Integer> tickList = new ArrayList<>();
430 while (true) {
431 int x = rect.x + leftSpace + (int) (Math.floor((time - time0) * pixelsPerNanoSec));
432 if (x >= rect.x + leftSpace + rect.width - rect0.width) {
433 break;
434 }
435 if (x >= rect.x + leftSpace) {
436 gc.drawLine(x, y, x, y + Y_OFFSET);
437 rect0.x = x;
438 if (x + rect0.width <= rect.x + rect.width) {
439 timeDraw.draw(gc, time, rect0);
440 }
441 tickList.add(x);
442 }
443 if (pixelsPerNanoSec == 0 || time > Long.MAX_VALUE - timeDelta || timeDelta == 0) {
444 break;
445 }
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;
465 } else {
466 time += timeDelta;
467 }
468 } else {
469 time += timeDelta;
470 }
471 }
472 fTickList = tickList;
473
474 // draw marker labels
475 if (fMarkersVisible) {
476 drawMarkerLabels(fMarkers, rect, gc, time0, leftSpace, pixelsPerNanoSec);
477 }
478 }
479
480 private static void drawMarkerLabels(List<IMarkerEvent> markerEvents, Rectangle rect, GC gc, long time0, int leftSpace, double pixelsPerNanoSec) {
481 if (markerEvents == null) {
482 return;
483 }
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);
492 }
493 }
494 }
495
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;
500 if (x1 >= rect.x) {
501 // T1
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);
505 }
506 if (x2 >= rect.x && Math.abs(x2 - x1 - 2) > 3) {
507 // T of T2
508 gc.drawLine(x2 - 2, y1, x2 - 2, y2);
509 gc.drawLine(x2 - 3, y1, x2 - 1, y1);
510 }
511 if (x2 >= rect.x && Math.abs(x2 - x1 + 3) > 3) {
512 // 2 of T2
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);
518 }
519 }
520
521 private static long floorToCalendar(long time, long timeDelta) {
522 long ret = time;
523
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;
549 } else {
550 long offset = GREGORIAN_CALENDAR.getTimeZone().getOffset(ret / MILLISEC_IN_NS) * MILLISEC_IN_NS;
551 ret += offset;
552 ret = (ret / timeDelta) * timeDelta;
553 ret -= offset;
554 }
555 return ret;
556 }
557
558 private int calculateDigits(long time0, long time1) {
559 int numDigits = 5;
560 long timeRange = time1 - time0;
561
562 if (fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
563 // Calculate the number of digits to represent the minutes provided
564 // 11:222
565 // HH:mm:ss
566 numDigits += 8;
567 if (timeRange < 10000) {
568 // HH:11:222:333:444__
569 numDigits += 10;
570 } else if (timeRange < 10000000) {
571 // HH:11:222:333__
572 numDigits += 6;
573 }
574 } else {
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();
582 }
583 }
584
585 return numDigits;
586 }
587
588 @Override
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) {
594 setCapture(true);
595 fDragState = LEFT_BUTTON;
596 }
597 if (x < 0) {
598 x = 0;
599 } else if (x > getSize().x - fTimeProvider.getNameSpace()) {
600 x = getSize().x - fTimeProvider.getNameSpace();
601 }
602 fDragX = x;
603 fDragX0 = x;
604 fTime0bak = fTimeProvider.getTime0();
605 fTime1bak = fTimeProvider.getTime1();
606 }
607 }
608
609 @Override
610 public void mouseUp(MouseEvent e) {
611 if (e.button == LEFT_BUTTON && fDragState == LEFT_BUTTON) {
612 setCapture(false);
613 fDragState = NO_BUTTON;
614
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());
618 }
619 }
620 }
621
622 @Override
623 public void mouseMove(MouseEvent e) {
624 if (fDragX0 < 0 || fDragState == NO_BUTTON || fTimeProvider == null) {
625 return;
626 }
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) {
632 fDragX = x;
633 if (fTimeProvider.getTime0() == fTimeProvider.getTime1()) {
634 return;
635 }
636 long interval = (long) ((fTime1bak - fTime0bak) * ((double) fDragX0 / fDragX));
637 if (interval == Long.MAX_VALUE) {
638 fTimeProvider.setStartFinishTimeNotify(fTime0bak, Long.MAX_VALUE);
639 } else {
640 long time1 = fTime0bak + (long) ((fTime1bak - fTime0bak) * ((double) fDragX0 / fDragX));
641 fTimeProvider.setStartFinishTimeNotify(fTime0bak, time1);
642 }
643 }
644 }
645 }
646
647 @Override
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();
653 }
654 }
655
656 /**
657 * Update the display to use the updated timestamp format
658 *
659 * @param signal
660 * the incoming signal
661 */
662 @TmfSignalHandler
663 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
664 TimeDraw.updateTimeZone();
665 Utils.updateTimeZone();
666 redraw();
667 }
668 }
669
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$
700
701 protected static final List<SimpleDateFormat> formats;
702 static
703 {
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();
716 }
717
718 /**
719 * Updates the timezone using the preferences.
720 */
721 public static void updateTimeZone() {
722 final TimeZone timeZone = TmfTimePreferences.getTimeZone();
723 for (SimpleDateFormat sdf : formats) {
724 synchronized (sdf) {
725 sdf.setTimeZone(timeZone);
726 }
727 }
728 }
729
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) {
737 retVal.append(' ');
738 }
739 }
740 return retVal.toString();
741 }
742
743 static String pad(long n) {
744 String s;
745 if (n < 10) {
746 s = S00;
747 } else if (n < 100) {
748 s = S0;
749 } else {
750 s = S;
751 }
752 return s + n;
753 }
754
755 public abstract int draw(GC gc, long time, Rectangle rect);
756
757 /**
758 * Override to draw absolute time header. This is for the time information
759 * not shown in the draw of each tick
760 *
761 * @param gc
762 * Graphics context
763 * @param nanosec
764 * time in nanosec
765 * @param absHeaderRect
766 * Header rectangle
767 */
768 public void drawAbsHeader(GC gc, long nanosec, Rectangle absHeaderRect) {
769 }
770 }
771
772 class TimeDrawSec extends TimeDraw {
773 @Override
774 public int draw(GC gc, long nanosec, Rectangle rect) {
775 long sec = nanosec / SEC_IN_NS;
776 return Utils.drawText(gc, sep(sec), rect, true);
777 }
778 }
779
780 class TimeDrawMillisec extends TimeDraw {
781 @Override
782 public int draw(GC gc, long nanosec, Rectangle rect) {
783 long millisec = nanosec / MILLISEC_IN_NS;
784 long ms = millisec % PAD_1000;
785 long sec = millisec / SEC_IN_MS;
786 return Utils.drawText(gc, sep(sec) + "." + pad(ms), rect, true); //$NON-NLS-1$
787 }
788 }
789
790 class TimeDrawMicrosec extends TimeDraw {
791 @Override
792 public int draw(GC gc, long nanosec, Rectangle rect) {
793 long microsec = nanosec / MICROSEC_IN_NS;
794 long us = microsec % PAD_1000;
795 long millisec = microsec / MILLISEC_IN_US;
796 long ms = millisec % PAD_1000;
797 long sec = millisec / SEC_IN_MS;
798 return Utils.drawText(gc, sep(sec) + "." + pad(ms) + " " + pad(us), rect, true); //$NON-NLS-1$ //$NON-NLS-2$
799 }
800 }
801
802 class TimeDrawNanosec extends TimeDraw {
803 @Override
804 public int draw(GC gc, long nanosec, Rectangle rect) {
805 long ns = nanosec % PAD_1000;
806 long microsec = nanosec / MICROSEC_IN_NS;
807 long us = microsec % PAD_1000;
808 long millisec = microsec / MILLISEC_IN_US;
809 long ms = millisec % PAD_1000;
810 long sec = millisec / SEC_IN_MS;
811 return Utils.drawText(gc, sep(sec) + "." + pad(ms) + " " + pad(us) + " " + pad(ns), rect, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
812 }
813 }
814
815 class TimeDrawAbsYear extends TimeDraw {
816 @Override
817 public int draw(GC gc, long nanosec, Rectangle rect) {
818 String stime;
819 synchronized (YEAR_FORMAT) {
820 stime = YEAR_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
821 }
822 return Utils.drawText(gc, stime, rect, true);
823 }
824 }
825
826 class TimeDrawAbsMonth extends TimeDraw {
827 @Override
828 public int draw(GC gc, long nanosec, Rectangle rect) {
829 String stime;
830 synchronized (MONTH_FORMAT) {
831 stime = MONTH_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
832 }
833 return Utils.drawText(gc, stime, rect, true);
834 }
835 }
836
837 class TimeDrawAbsDay extends TimeDraw {
838 @Override
839 public int draw(GC gc, long nanosec, Rectangle rect) {
840 String stime;
841 synchronized (DAY_FORMAT) {
842 stime = DAY_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
843 }
844 return Utils.drawText(gc, stime, rect, true);
845 }
846
847 @Override
848 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
849 String header;
850 synchronized (DAY_FORMAT_HEADER) {
851 header = DAY_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
852 }
853 int headerwidth = gc.stringExtent(header).x + 4;
854 if (headerwidth <= rect.width) {
855 rect.x += (rect.width - headerwidth);
856 Utils.drawText(gc, header, rect, true);
857 }
858 }
859 }
860
861 class TimeDrawAbsHrs extends TimeDraw {
862 @Override
863 public int draw(GC gc, long nanosec, Rectangle rect) {
864 String stime;
865 synchronized (HOURS_FORMAT) {
866 stime = HOURS_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
867 }
868 return Utils.drawText(gc, stime, rect, true);
869 }
870
871 @Override
872 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
873 String header;
874 synchronized (HOURS_FORMAT_HEADER) {
875 header = HOURS_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
876 }
877 int headerwidth = gc.stringExtent(header).x + 4;
878 if (headerwidth <= rect.width) {
879 rect.x += (rect.width - headerwidth);
880 Utils.drawText(gc, header, rect, true);
881 }
882 }
883 }
884
885 class TimeDrawAbsMin extends TimeDraw {
886 @Override
887 public int draw(GC gc, long nanosec, Rectangle rect) {
888 String stime;
889 synchronized (MIN_FORMAT) {
890 stime = MIN_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
891 }
892 return Utils.drawText(gc, stime, rect, true);
893 }
894
895 @Override
896 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
897 String header;
898 synchronized (MIN_FORMAT_HEADER) {
899 header = MIN_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
900 }
901 int headerwidth = gc.stringExtent(header).x + 4;
902 if (headerwidth <= rect.width) {
903 rect.x += (rect.width - headerwidth);
904 Utils.drawText(gc, header, rect, true);
905 }
906 }
907 }
908
909 class TimeDrawAbsSec extends TimeDraw {
910 @Override
911 public int draw(GC gc, long nanosec, Rectangle rect) {
912 String stime;
913 synchronized (SEC_FORMAT) {
914 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
915 }
916 return Utils.drawText(gc, stime, rect, true);
917 }
918
919 @Override
920 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
921 String header;
922 synchronized (SEC_FORMAT_HEADER) {
923 header = SEC_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
924 }
925 int headerwidth = gc.stringExtent(header).x + 4;
926 if (headerwidth <= rect.width) {
927 rect.x += (rect.width - headerwidth);
928 Utils.drawText(gc, header, rect, true);
929 }
930 }
931 }
932
933 class TimeDrawAbsMillisec extends TimeDraw {
934 @Override
935 public int draw(GC gc, long nanosec, Rectangle rect) {
936 String stime;
937 synchronized (SEC_FORMAT) {
938 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
939 }
940 String ns = Utils.formatNs(nanosec, Resolution.MILLISEC);
941 return Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$
942 }
943
944 @Override
945 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
946 String header;
947 synchronized (SEC_FORMAT_HEADER) {
948 header = SEC_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
949 }
950 int headerwidth = gc.stringExtent(header).x + 4;
951 if (headerwidth <= rect.width) {
952 rect.x += (rect.width - headerwidth);
953 Utils.drawText(gc, header, rect, true);
954 }
955 }
956 }
957
958 class TimeDrawAbsMicroSec extends TimeDraw {
959 @Override
960 public int draw(GC gc, long nanosec, Rectangle rect) {
961 String stime;
962 synchronized (SEC_FORMAT) {
963 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
964 }
965 String micr = Utils.formatNs(nanosec, Resolution.MICROSEC);
966 return Utils.drawText(gc, stime + "." + micr, rect, true); //$NON-NLS-1$
967 }
968
969 @Override
970 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
971 String header;
972 synchronized (SEC_FORMAT_HEADER) {
973 header = SEC_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
974 }
975 int headerwidth = gc.stringExtent(header).x + 4;
976 if (headerwidth <= rect.width) {
977 rect.x += (rect.width - headerwidth);
978 Utils.drawText(gc, header, rect, true);
979 }
980 }
981 }
982
983 class TimeDrawAbsNanoSec extends TimeDraw {
984 @Override
985 public int draw(GC gc, long nanosec, Rectangle rect) {
986 String stime;
987 synchronized (SEC_FORMAT) {
988 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
989 }
990 String ns = Utils.formatNs(nanosec, Resolution.NANOSEC);
991 return Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$
992 }
993
994 @Override
995 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
996 String header;
997 synchronized (SEC_FORMAT_HEADER) {
998 header = SEC_FORMAT_HEADER.format(new Date(nanosec / MILLISEC_IN_NS));
999 }
1000 int headerwidth = gc.stringExtent(header).x + 4;
1001 if (headerwidth <= rect.width) {
1002 rect.x += (rect.width - headerwidth);
1003 Utils.drawText(gc, header, rect, true);
1004 }
1005 }
1006 }
1007
1008 class TimeDrawNumber extends TimeDraw {
1009 @Override
1010 public int draw(GC gc, long time, Rectangle rect) {
1011 String stime = NumberFormat.getInstance().format(time);
1012 return Utils.drawText(gc, stime, rect, true);
1013 }
1014 }
1015
1016 class TimeDrawCycles extends TimeDraw {
1017 @Override
1018 public int draw(GC gc, long time, Rectangle rect) {
1019 String stime = Utils.formatTime(time, TimeFormat.CYCLES, Resolution.SECONDS);
1020 return Utils.drawText(gc, stime, rect, true);
1021 }
1022 }
This page took 0.060999 seconds and 4 git commands to generate.