tmf: Add time graph marker axis for marker labels
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / widgets / TimeGraphScale.java
CommitLineData
8e1f81f1 1/*****************************************************************************
8910dea2 2 * Copyright (c) 2007, 2016 Intel Corporation, Ericsson
8e1f81f1
PT
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
c1cd9635 13 * Marc-Andre Laperle - Add time zone preference
8e1f81f1
PT
14 *****************************************************************************/
15
2bdf0193 16package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets;
8e1f81f1 17
026664b7 18import java.text.NumberFormat;
8e1f81f1 19import java.text.SimpleDateFormat;
55d1fc96 20import java.util.ArrayList;
8e1f81f1
PT
21import java.util.Calendar;
22import java.util.Date;
6dcc38b9 23import java.util.List;
c1cd9635 24import java.util.TimeZone;
8e1f81f1 25
8e1f81f1
PT
26import org.eclipse.swt.SWT;
27import org.eclipse.swt.events.MouseEvent;
28import org.eclipse.swt.events.MouseListener;
29import org.eclipse.swt.events.MouseMoveListener;
30import org.eclipse.swt.events.PaintEvent;
31import org.eclipse.swt.graphics.GC;
32import org.eclipse.swt.graphics.Point;
33import org.eclipse.swt.graphics.Rectangle;
34import org.eclipse.swt.widgets.Composite;
3573a01c 35import org.eclipse.swt.widgets.Control;
2bdf0193
AM
36import org.eclipse.tracecompass.internal.tmf.ui.Messages;
37import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
38import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
39import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
40import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimePreferences;
41import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
42import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
8e1f81f1 43
6dcc38b9
MK
44import com.google.common.collect.ImmutableList;
45
8e1f81f1
PT
46/**
47 * Implementation of the scale for the time graph view.
48 *
49 * This goes above the "gantt chart" area.
50 *
51 * @version 1.0
52 * @author Alvaro Sanchez-Leon
53 * @author Patrick Tasse
54 */
55public class TimeGraphScale extends TimeGraphBaseControl implements
56 MouseListener, MouseMoveListener {
57
83dcfe09
MK
58 private static final int BASE_10 = 10;
59 private static final int X_OFFSET = 4;
60 private static final int Y_OFFSET = 4;
61
62 private static final int MIN_SECOND_FACTOR = 20;
63 private static final int SECOND_FACTOR = 30;
64 private static final int MAX_SECOND_FACTOR = 30;
65
66 private static final int MIN_MINUTE_FACTOR = 10;
67 private static final int MINUTE_FACTOR = 15;
68 private static final int MAX_MINUTE_FACTOR = 30;
69
70 private static final int MIN_HOUR_FACTOR = 3;
71 private static final int HOUR_FACTOR = 6;
72 private static final int MAX_HOUR_FACTOR = 12;
73
74 private static final int MAX_DAY_FACTOR = 10;
75
76 private static final int MAX_MONTH_FACTOR = 6;
77 private static final int MONTH_FACTOR = 6;
78 private static final int MIN_MONTH_FACTOR = 3;
79
f1fae91f
PT
80 private static final long MICROSEC_IN_NS = 1000;
81 private static final long MILLISEC_IN_NS = 1000000;
8e1f81f1
PT
82 private static final long SEC_IN_NS = 1000000000;
83 private static final long MIN_IN_NS = 60 * SEC_IN_NS;
84 private static final long HOUR_IN_NS = 60 * MIN_IN_NS;
85 private static final long DAY_IN_NS = 24 * HOUR_IN_NS;
86 private static final long MONTH_IN_NS = 31 * DAY_IN_NS; // upper limit
87 private static final long YEAR_IN_NS = 366 * DAY_IN_NS; // upper limit
88
89 private static final double LOG10_1 = Math.log10(1);
90 private static final double LOG10_2 = Math.log10(2);
91 private static final double LOG10_3 = Math.log10(3);
92 private static final double LOG10_5 = Math.log10(5);
93
94 private static final Calendar GREGORIAN_CALENDAR = Calendar.getInstance();
95
f1fae91f
PT
96 private static final TimeDraw TIMEDRAW_NANOSEC = new TimeDrawNanosec();
97 private static final TimeDraw TIMEDRAW_MICROSEC = new TimeDrawMicrosec();
98 private static final TimeDraw TIMEDRAW_MILLISEC = new TimeDrawMillisec();
99 private static final TimeDraw TIMEDRAW_SEC = new TimeDrawSec();
100 private static final TimeDraw TIMEDRAW_ABS_NANOSEC = new TimeDrawAbsNanoSec();
101 private static final TimeDraw TIMEDRAW_ABS_MICROSEC = new TimeDrawAbsMicroSec();
102 private static final TimeDraw TIMEDRAW_ABS_MILLISEC = new TimeDrawAbsMillisec();
103 private static final TimeDraw TIMEDRAW_ABS_SEC = new TimeDrawAbsSec();
104 private static final TimeDraw TIMEDRAW_ABS_MIN = new TimeDrawAbsMin();
105 private static final TimeDraw TIMEDRAW_ABS_HRS = new TimeDrawAbsHrs();
106 private static final TimeDraw TIMEDRAW_ABS_DAY = new TimeDrawAbsDay();
107 private static final TimeDraw TIMEDRAW_ABS_MONTH = new TimeDrawAbsMonth();
108 private static final TimeDraw TIMEDRAW_ABS_YEAR = new TimeDrawAbsYear();
109 private static final TimeDraw TIMEDRAW_NUMBER = new TimeDrawNumber();
0fab12b0 110 private static final TimeDraw TIMEDRAW_CYCLES = new TimeDrawCycles();
f1fae91f 111
0fcf3b09 112 private static final int DRAG_EXTERNAL = -1;
f1fae91f
PT
113 private static final int NO_BUTTON = 0;
114 private static final int LEFT_BUTTON = 1;
f1fae91f
PT
115
116 private ITimeDataProvider fTimeProvider;
117 private int fDragState = NO_BUTTON;
118 private int fDragX0 = 0;
119 private int fDragX = 0;
120 private long fTime0bak;
121 private long fTime1bak;
122 private boolean fIsInUpdate;
123 private int fHeight;
55d1fc96 124 private List<Integer> fTickList = new ArrayList<>();
f1fae91f
PT
125
126 /**
127 * Standard constructor
128 *
129 * @param parent
130 * The parent composite object
131 * @param colors
132 * The color scheme to use
133 */
134 public TimeGraphScale(Composite parent, TimeGraphColorScheme colors) {
135 super(parent, colors, SWT.NO_BACKGROUND | SWT.NO_FOCUS | SWT.DOUBLE_BUFFERED);
c1cd9635 136 TmfSignalManager.register(this);
f1fae91f
PT
137 addMouseListener(this);
138 addMouseMoveListener(this);
c1cd9635
MAL
139 TimeDraw.updateTimeZone();
140 }
141
142 @Override
143 public void dispose() {
144 TmfSignalManager.deregister(this);
145 super.dispose();
f1fae91f 146 }
8e1f81f1
PT
147
148 /**
149 * Assign the time provider for this scale
150 *
151 * @param timeProvider
152 * The provider to use
153 */
154 public void setTimeProvider(ITimeDataProvider timeProvider) {
f1fae91f 155 fTimeProvider = timeProvider;
8e1f81f1
PT
156 }
157
0fab12b0
PT
158 /**
159 * Get the time provider used by this scale
160 *
161 * @return The time provider
0fab12b0
PT
162 */
163 public ITimeDataProvider getTimeProvider() {
164 return fTimeProvider;
165 }
166
8e1f81f1
PT
167 @Override
168 public Point computeSize(int wHint, int hHint, boolean changed) {
f1fae91f 169 return super.computeSize(wHint, fHeight, changed);
8e1f81f1
PT
170 }
171
172 /**
173 * Set the height of the scale
174 *
175 * @param height
176 * The height to use
177 */
178 public void setHeight(int height) {
3573a01c
MAL
179 if (fHeight != height) {
180 fHeight = height;
181 getParent().layout(new Control[] { this });
182 }
8e1f81f1
PT
183 }
184
0fcf3b09
PT
185 /**
186 * Set the drag range to paint decorators
187 *
188 * @param begin
189 * The begin x-coordinate
190 * @param end
191 * The end x-coordinate
0fcf3b09
PT
192 */
193 public void setDragRange(int begin, int end) {
194 if (NO_BUTTON == fDragState || DRAG_EXTERNAL == fDragState) {
195 fDragX0 = begin - fTimeProvider.getNameSpace();
196 fDragX = end - fTimeProvider.getNameSpace();
197 if (begin >= 0 || end >= 0) {
198 fDragState = DRAG_EXTERNAL;
199 } else {
200 fDragState = NO_BUTTON;
201 }
202 }
203 redraw();
204 }
205
55d1fc96
PT
206 /**
207 * Get the list of visible ticks of the time axis.
208 *
209 * @return the list of visible tick x-coordinates
210 * @since 2.0
211 */
212 public List<Integer> getTickList() {
213 return fTickList;
214 }
215
f1fae91f
PT
216 private long calcTimeDelta(int width, double pixelsPerNanoSec) {
217 long timeDelta;
8e1f81f1
PT
218 double minDelta = (pixelsPerNanoSec == 0) ? YEAR_IN_NS : width / pixelsPerNanoSec;
219 long unit = 1;
0fab12b0 220 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
83dcfe09 221 if (minDelta > MAX_MONTH_FACTOR * MONTH_IN_NS) {
8e1f81f1 222 unit = YEAR_IN_NS;
83dcfe09
MK
223 } else if (minDelta > MIN_MONTH_FACTOR * MONTH_IN_NS) {
224 unit = MONTH_FACTOR * MONTH_IN_NS;
225 } else if (minDelta > MAX_DAY_FACTOR * DAY_IN_NS) {
8e1f81f1 226 unit = MONTH_IN_NS;
83dcfe09 227 } else if (minDelta > MAX_HOUR_FACTOR * HOUR_IN_NS) {
8e1f81f1 228 unit = DAY_IN_NS;
83dcfe09
MK
229 } else if (minDelta > MIN_HOUR_FACTOR * HOUR_IN_NS) {
230 unit = HOUR_FACTOR * HOUR_IN_NS;
231 } else if (minDelta > MAX_MINUTE_FACTOR * MIN_IN_NS) {
8e1f81f1 232 unit = HOUR_IN_NS;
83dcfe09
MK
233 } else if (minDelta > MIN_MINUTE_FACTOR * MIN_IN_NS) {
234 unit = MINUTE_FACTOR * MIN_IN_NS;
235 } else if (minDelta > MAX_SECOND_FACTOR * SEC_IN_NS) {
8e1f81f1 236 unit = MIN_IN_NS;
83dcfe09
MK
237 } else if (minDelta > MIN_SECOND_FACTOR * SEC_IN_NS) {
238 unit = SECOND_FACTOR * SEC_IN_NS;
8e1f81f1 239 } else if (minDelta <= 1) {
f1fae91f
PT
240 timeDelta = 1;
241 return timeDelta;
8e1f81f1
PT
242 }
243 }
244 double log = Math.log10(minDelta / unit);
245 long pow10 = (long) log;
246 double remainder = log - pow10;
247 if (remainder < LOG10_1) {
83dcfe09 248 timeDelta = (long) Math.pow(BASE_10, pow10) * unit;
8e1f81f1 249 } else if (remainder < LOG10_2) {
83dcfe09 250 timeDelta = 2 * (long) Math.pow(BASE_10, pow10) * unit;
8e1f81f1 251 } else if (remainder < LOG10_3 && unit >= HOUR_IN_NS && unit < YEAR_IN_NS) {
83dcfe09 252 timeDelta = 3 * (long) Math.pow(BASE_10, pow10) * unit;
8e1f81f1 253 } else if (remainder < LOG10_5) {
83dcfe09 254 timeDelta = 5 * (long) Math.pow(BASE_10, pow10) * unit;
8e1f81f1 255 } else {
83dcfe09 256 timeDelta = 10 * (long) Math.pow(BASE_10, pow10) * unit;
8e1f81f1 257 }
f1fae91f
PT
258 if (timeDelta <= 0) {
259 timeDelta = 1;
f85266f3 260 }
f1fae91f 261 return timeDelta;
8e1f81f1
PT
262 }
263
8e1f81f1
PT
264 TimeDraw getTimeDraw(long timeDelta) {
265 TimeDraw timeDraw;
f1fae91f 266 if (fTimeProvider != null) {
0fab12b0
PT
267 switch (fTimeProvider.getTimeFormat()) {
268 case CALENDAR:
026664b7
XR
269 if (timeDelta >= YEAR_IN_NS) {
270 timeDraw = TIMEDRAW_ABS_YEAR;
271 } else if (timeDelta >= MONTH_IN_NS) {
272 timeDraw = TIMEDRAW_ABS_MONTH;
273 } else if (timeDelta >= DAY_IN_NS) {
274 timeDraw = TIMEDRAW_ABS_DAY;
275 } else if (timeDelta >= HOUR_IN_NS) {
276 timeDraw = TIMEDRAW_ABS_HRS;
277 } else if (timeDelta >= MIN_IN_NS) {
278 timeDraw = TIMEDRAW_ABS_MIN;
279 } else if (timeDelta >= SEC_IN_NS) {
280 timeDraw = TIMEDRAW_ABS_SEC;
f1fae91f 281 } else if (timeDelta >= MILLISEC_IN_NS) {
026664b7 282 timeDraw = TIMEDRAW_ABS_MILLISEC;
f1fae91f 283 } else if (timeDelta >= MICROSEC_IN_NS) {
026664b7
XR
284 timeDraw = TIMEDRAW_ABS_MICROSEC;
285 } else {
286 timeDraw = TIMEDRAW_ABS_NANOSEC;
287 }
288 return timeDraw;
0fab12b0
PT
289 case NUMBER:
290 return TIMEDRAW_NUMBER;
291 case CYCLES:
292 return TIMEDRAW_CYCLES;
293 case RELATIVE:
294 default:
8e1f81f1 295 }
026664b7 296
8e1f81f1 297 }
f1fae91f 298 if (timeDelta >= SEC_IN_NS) {
8e1f81f1 299 timeDraw = TIMEDRAW_SEC;
f1fae91f 300 } else if (timeDelta >= MILLISEC_IN_NS) {
8e1f81f1 301 timeDraw = TIMEDRAW_MILLISEC;
f1fae91f 302 } else if (timeDelta >= MICROSEC_IN_NS) {
8e1f81f1
PT
303 timeDraw = TIMEDRAW_MICROSEC;
304 } else {
305 timeDraw = TIMEDRAW_NANOSEC;
306 }
307 return timeDraw;
308 }
309
310 @Override
311 void paint(Rectangle rect, PaintEvent e) {
312
f1fae91f 313 if (fIsInUpdate || null == fTimeProvider) {
8e1f81f1
PT
314 return;
315 }
316
317 GC gc = e.gc;
318 gc.fillRectangle(rect);
319
f1fae91f
PT
320 long time0 = fTimeProvider.getTime0();
321 long time1 = fTimeProvider.getTime1();
f1fae91f
PT
322 int leftSpace = fTimeProvider.getNameSpace();
323 int timeSpace = fTimeProvider.getTimeSpace();
8e1f81f1 324
f1fae91f
PT
325 gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_BACKGROUND));
326 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND));
327 Rectangle rect0 = new Rectangle(0, 0, 0, 0);
328 Utils.init(rect0, rect);
8e1f81f1
PT
329
330 // draw top left area
f1fae91f 331 rect0.width = leftSpace;
83dcfe09
MK
332 rect0.x += X_OFFSET;
333 rect0.width -= X_OFFSET;
f1fae91f 334 Rectangle absHeaderRect = new Rectangle(rect0.x, rect0.y, rect0.width, rect0.height);
83dcfe09
MK
335 rect0.x -= X_OFFSET;
336 rect0.width += X_OFFSET;
8e1f81f1
PT
337
338 // prepare and draw right rect of the timescale
f1fae91f
PT
339 rect0.x += leftSpace;
340 rect0.width = rect.width - leftSpace;
8e1f81f1
PT
341
342 // draw bottom border and erase all other area
343 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1,
344 rect.y + rect.height - 1);
f1fae91f
PT
345 rect0.height--;
346 gc.fillRectangle(rect0);
8e1f81f1
PT
347
348 if (time1 <= time0 || timeSpace < 2) {
55d1fc96 349 fTickList.clear();
8e1f81f1
PT
350 return;
351 }
352
353 int numDigits = calculateDigits(time0, time1);
354
355 int labelWidth = gc.getCharWidth('0') * numDigits;
356 double pixelsPerNanoSec = (timeSpace <= RIGHT_MARGIN) ? 0 :
6dcc38b9 357 (double) (timeSpace - RIGHT_MARGIN) / (time1 - time0);
f1fae91f 358 long timeDelta = calcTimeDelta(labelWidth, pixelsPerNanoSec);
8e1f81f1 359
f1fae91f 360 TimeDraw timeDraw = getTimeDraw(timeDelta);
8e1f81f1 361
0fcf3b09
PT
362 // draw range decorators
363 if (DRAG_EXTERNAL == fDragState) {
85203d74
PT
364 int x1 = leftSpace + fDragX0;
365 int x2 = leftSpace + fDragX;
0fcf3b09
PT
366 drawRangeDecorators(rect0, gc, x1, x2);
367 } else {
368 int x1;
369 int x2;
baf92cac
AM
370 long selectionBegin = fTimeProvider.getSelectionBegin();
371 long selectionEnd = fTimeProvider.getSelectionEnd();
372 x1 = leftSpace + (int) ((selectionBegin - time0) * pixelsPerNanoSec);
373 x2 = leftSpace + (int) ((selectionEnd - time0) * pixelsPerNanoSec);
0fcf3b09 374 drawRangeDecorators(rect0, gc, x1, x2);
8e1f81f1
PT
375 }
376
f1fae91f 377 if (rect0.isEmpty()) {
8e1f81f1
PT
378 return;
379 }
380
8e1f81f1 381 // draw time scale ticks
f1fae91f 382 rect0.y = rect.y;
83dcfe09 383 rect0.height = rect.height - Y_OFFSET;
f1fae91f 384 rect0.width = labelWidth;
8e1f81f1
PT
385
386 long time;
0fab12b0 387 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
f1fae91f 388 time = floorToCalendar(time0, timeDelta);
8e1f81f1 389 } else {
f1fae91f 390 time = (time0 / timeDelta) * timeDelta;
8e1f81f1 391 if (time != time0) {
f1fae91f 392 time += timeDelta;
8e1f81f1
PT
393 }
394 }
395
f1fae91f 396 int y = rect0.y + rect0.height;
8e1f81f1 397
0fab12b0 398 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
8e1f81f1
PT
399 timeDraw.drawAbsHeader(gc, time, absHeaderRect);
400 }
401
55d1fc96 402 List<Integer> tickList = new ArrayList<>();
8e1f81f1 403 while (true) {
0fcf3b09 404 int x = rect.x + leftSpace + (int) (Math.floor((time - time0) * pixelsPerNanoSec));
f1fae91f 405 if (x >= rect.x + leftSpace + rect.width - rect0.width) {
8e1f81f1
PT
406 break;
407 }
408 if (x >= rect.x + leftSpace) {
83dcfe09 409 gc.drawLine(x, y, x, y + Y_OFFSET);
f1fae91f
PT
410 rect0.x = x;
411 if (x + rect0.width <= rect.x + rect.width) {
412 timeDraw.draw(gc, time, rect0);
8e1f81f1 413 }
55d1fc96 414 tickList.add(x);
8e1f81f1 415 }
f1fae91f 416 if (pixelsPerNanoSec == 0 || time > Long.MAX_VALUE - timeDelta || timeDelta == 0) {
8e1f81f1
PT
417 break;
418 }
0fab12b0 419 if (fTimeProvider != null && fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
f1fae91f
PT
420 if (timeDelta >= YEAR_IN_NS) {
421 long millis = time / MILLISEC_IN_NS;
8e1f81f1 422 GREGORIAN_CALENDAR.setTime(new Date(millis));
f1fae91f 423 GREGORIAN_CALENDAR.add(Calendar.YEAR, (int) (timeDelta / YEAR_IN_NS));
8e1f81f1 424 millis = GREGORIAN_CALENDAR.getTimeInMillis();
f1fae91f
PT
425 time = millis * MILLISEC_IN_NS;
426 } else if (timeDelta >= MONTH_IN_NS) {
427 long millis = time / MILLISEC_IN_NS;
8e1f81f1 428 GREGORIAN_CALENDAR.setTime(new Date(millis));
f1fae91f 429 GREGORIAN_CALENDAR.add(Calendar.MONTH, (int) (timeDelta / MONTH_IN_NS));
8e1f81f1 430 millis = GREGORIAN_CALENDAR.getTimeInMillis();
f1fae91f
PT
431 time = millis * MILLISEC_IN_NS;
432 } else if (timeDelta >= DAY_IN_NS) {
433 long millis = time / MILLISEC_IN_NS;
8e1f81f1 434 GREGORIAN_CALENDAR.setTime(new Date(millis));
f1fae91f 435 GREGORIAN_CALENDAR.add(Calendar.DAY_OF_MONTH, (int) (timeDelta / DAY_IN_NS));
8e1f81f1 436 millis = GREGORIAN_CALENDAR.getTimeInMillis();
f1fae91f 437 time = millis * MILLISEC_IN_NS;
8e1f81f1 438 } else {
f1fae91f 439 time += timeDelta;
8e1f81f1
PT
440 }
441 } else {
f1fae91f 442 time += timeDelta;
8e1f81f1
PT
443 }
444 }
55d1fc96 445 fTickList = tickList;
8e1f81f1
PT
446 }
447
0fcf3b09
PT
448 private static void drawRangeDecorators(Rectangle rect, GC gc, int x1, int x2) {
449 int y1 = rect.y + rect.height - 9;
450 int y2 = rect.y + rect.height - 5;
451 int ym = (y1 + y2) / 2;
452 if (x1 >= rect.x) {
453 // T1
85203d74
PT
454 gc.drawLine(x1 - 2, y1, x1 - 2, y2);
455 gc.drawLine(x1 - 3, y1, x1 - 1, y1);
456 gc.drawLine(x1 + 1, y1, x1 + 1, y2);
0fcf3b09 457 }
85203d74
PT
458 if (x2 >= rect.x && Math.abs(x2 - x1 - 2) > 3) {
459 // T of T2
0fcf3b09
PT
460 gc.drawLine(x2 - 2, y1, x2 - 2, y2);
461 gc.drawLine(x2 - 3, y1, x2 - 1, y1);
462 }
85203d74
PT
463 if (x2 >= rect.x && Math.abs(x2 - x1 + 3) > 3) {
464 // 2 of T2
0fcf3b09
PT
465 gc.drawLine(x2 + 1, y1, x2 + 3, y1);
466 gc.drawLine(x2 + 3, y1, x2 + 3, ym);
467 gc.drawLine(x2 + 1, ym, x2 + 3, ym);
468 gc.drawLine(x2 + 1, ym, x2 + 1, y2);
469 gc.drawLine(x2 + 1, y2, x2 + 3, y2);
470 }
471 }
472
f1fae91f 473 private static long floorToCalendar(long time, long timeDelta) {
41b5c37f
AM
474 long ret = time;
475
f1fae91f
PT
476 if (timeDelta >= YEAR_IN_NS) {
477 GREGORIAN_CALENDAR.setTime(new Date(ret / MILLISEC_IN_NS));
8e1f81f1
PT
478 int year = GREGORIAN_CALENDAR.get(Calendar.YEAR);
479 int yearDelta = (int) (timeDelta / YEAR_IN_NS);
480 year = (year / yearDelta) * yearDelta;
481 GREGORIAN_CALENDAR.set(Calendar.YEAR, year);
482 GREGORIAN_CALENDAR.set(Calendar.MONTH, 0); // January 1st of year
483 GREGORIAN_CALENDAR.set(Calendar.DAY_OF_MONTH, 1);
484 GREGORIAN_CALENDAR.set(Calendar.HOUR_OF_DAY, 0);
485 GREGORIAN_CALENDAR.set(Calendar.MINUTE, 0);
486 GREGORIAN_CALENDAR.set(Calendar.SECOND, 0);
487 GREGORIAN_CALENDAR.set(Calendar.MILLISECOND, 0);
f1fae91f
PT
488 ret = GREGORIAN_CALENDAR.getTimeInMillis() * MILLISEC_IN_NS;
489 } else if (timeDelta >= MONTH_IN_NS) {
490 GREGORIAN_CALENDAR.setTime(new Date(ret / MILLISEC_IN_NS));
8e1f81f1
PT
491 int month = GREGORIAN_CALENDAR.get(Calendar.MONTH);
492 int monthDelta = (int) (timeDelta / MONTH_IN_NS);
493 month = (month / monthDelta) * monthDelta;
494 GREGORIAN_CALENDAR.set(Calendar.MONTH, month);
495 GREGORIAN_CALENDAR.set(Calendar.DAY_OF_MONTH, 1); // 1st of month
496 GREGORIAN_CALENDAR.set(Calendar.HOUR_OF_DAY, 0);
497 GREGORIAN_CALENDAR.set(Calendar.MINUTE, 0);
498 GREGORIAN_CALENDAR.set(Calendar.SECOND, 0);
499 GREGORIAN_CALENDAR.set(Calendar.MILLISECOND, 0);
f1fae91f 500 ret = GREGORIAN_CALENDAR.getTimeInMillis() * MILLISEC_IN_NS;
8e1f81f1 501 } else {
f1fae91f 502 long offset = GREGORIAN_CALENDAR.getTimeZone().getOffset(ret / MILLISEC_IN_NS) * MILLISEC_IN_NS;
41b5c37f
AM
503 ret += offset;
504 ret = (ret / timeDelta) * timeDelta;
505 ret -= offset;
8e1f81f1 506 }
41b5c37f 507 return ret;
8e1f81f1
PT
508 }
509
510 private int calculateDigits(long time0, long time1) {
511 int numDigits = 5;
512 long timeRange = time1 - time0;
513
0fab12b0 514 if (fTimeProvider.getTimeFormat() == TimeFormat.CALENDAR) {
8e1f81f1
PT
515 // Calculate the number of digits to represent the minutes provided
516 // 11:222
517 // HH:mm:ss
518 numDigits += 8;
519 if (timeRange < 10000) {
520 // HH:11:222:333:444__
521 numDigits += 10;
522 } else if (timeRange < 10000000) {
523 // HH:11:222:333__
524 numDigits += 6;
525 }
526 } else {
f1fae91f 527 long sec = time1 / SEC_IN_NS;
8e1f81f1
PT
528 numDigits = Long.toString(sec).length();
529 int thousandGroups = (numDigits - 1) / 3;
530 numDigits += thousandGroups;
531 numDigits += 12; // .000 000 000
0fab12b0
PT
532 if (fTimeProvider.getTimeFormat() == TimeFormat.CYCLES) {
533 numDigits += Messages.Utils_ClockCyclesUnit.length();
534 }
8e1f81f1
PT
535 }
536
537 return numDigits;
538 }
539
540 @Override
541 public void mouseDown(MouseEvent e) {
542 getParent().setFocus();
f1fae91f
PT
543 if (fDragState == NO_BUTTON && null != fTimeProvider) {
544 int x = e.x - fTimeProvider.getNameSpace();
545 if (LEFT_BUTTON == e.button && x > 0) {
8e1f81f1 546 setCapture(true);
f1fae91f 547 fDragState = LEFT_BUTTON;
8e1f81f1
PT
548 }
549 if (x < 0) {
550 x = 0;
f1fae91f
PT
551 } else if (x > getSize().x - fTimeProvider.getNameSpace()) {
552 x = getSize().x - fTimeProvider.getNameSpace();
8e1f81f1 553 }
f1fae91f
PT
554 fDragX = x;
555 fDragX0 = x;
556 fTime0bak = fTimeProvider.getTime0();
557 fTime1bak = fTimeProvider.getTime1();
8e1f81f1
PT
558 }
559 }
560
561 @Override
562 public void mouseUp(MouseEvent e) {
f1fae91f 563 if (e.button == LEFT_BUTTON && fDragState == LEFT_BUTTON) {
8e1f81f1 564 setCapture(false);
f1fae91f 565 fDragState = NO_BUTTON;
8e1f81f1
PT
566
567 // Notify time provider to check the need for listener notification
f1fae91f
PT
568 if (fDragX != fDragX0 && fTimeProvider.getTime0() != fTimeProvider.getTime1()) {
569 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1());
8e1f81f1 570 }
8e1f81f1
PT
571 }
572 }
573
574 @Override
575 public void mouseMove(MouseEvent e) {
f1fae91f 576 if (fDragX0 < 0 || fDragState == NO_BUTTON || fTimeProvider == null) {
8e1f81f1
PT
577 return;
578 }
579 Point size = getSize();
f1fae91f 580 int leftSpace = fTimeProvider.getNameSpace();
8e1f81f1 581 int x = e.x - leftSpace;
f1fae91f
PT
582 if (LEFT_BUTTON == fDragState) {
583 if (x > 0 && size.x > leftSpace && fDragX != x) {
584 fDragX = x;
585 if (fTimeProvider.getTime0() == fTimeProvider.getTime1()) {
8e1f81f1
PT
586 return;
587 }
f1fae91f 588 long interval = (long) ((fTime1bak - fTime0bak) * ((double) fDragX0 / fDragX));
8e1f81f1 589 if (interval == Long.MAX_VALUE) {
88de10c6 590 fTimeProvider.setStartFinishTimeNotify(fTime0bak, Long.MAX_VALUE);
8e1f81f1 591 } else {
f1fae91f 592 long time1 = fTime0bak + (long) ((fTime1bak - fTime0bak) * ((double) fDragX0 / fDragX));
88de10c6 593 fTimeProvider.setStartFinishTimeNotify(fTime0bak, time1);
8e1f81f1
PT
594 }
595 }
8e1f81f1
PT
596 }
597 }
598
599 @Override
600 public void mouseDoubleClick(MouseEvent e) {
f1fae91f
PT
601 if (e.button == 1 && null != fTimeProvider && fTimeProvider.getTime0() != fTimeProvider.getTime1() && (e.stateMask & SWT.BUTTON_MASK) == 0) {
602 fTimeProvider.resetStartFinishTime();
f1fae91f
PT
603 fTime0bak = fTimeProvider.getTime0();
604 fTime1bak = fTimeProvider.getTime1();
8e1f81f1
PT
605 }
606 }
c1cd9635 607
6dcc38b9 608 /**
c1cd9635
MAL
609 * Update the display to use the updated timestamp format
610 *
6dcc38b9
MK
611 * @param signal
612 * the incoming signal
c1cd9635
MAL
613 */
614 @TmfSignalHandler
615 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
616 TimeDraw.updateTimeZone();
617 Utils.updateTimeZone();
618 redraw();
619 }
8e1f81f1
PT
620}
621
622abstract class TimeDraw {
f1fae91f
PT
623 protected static final long MICROSEC_IN_NS = 1000;
624 protected static final long MILLISEC_IN_NS = 1000000;
625 protected static final long MILLISEC_IN_US = 1000;
626 protected static final long SEC_IN_NS = 1000000000;
627 protected static final long SEC_IN_MS = 1000;
6dcc38b9
MK
628 private static final String S = ""; //$NON-NLS-1$
629 private static final String S0 = "0"; //$NON-NLS-1$
f1fae91f
PT
630 private static final String S00 = "00"; //$NON-NLS-1$
631 protected static final long PAD_1000 = 1000;
6dcc38b9
MK
632 protected static final SimpleDateFormat SEC_FORMAT_HEADER =
633 new SimpleDateFormat("yyyy MMM dd");//$NON-NLS-1$
634 protected static final SimpleDateFormat SEC_FORMAT =
635 new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
636 protected static final SimpleDateFormat MIN_FORMAT_HEADER =
637 new SimpleDateFormat("yyyy MMM dd"); //$NON-NLS-1$
638 protected static final SimpleDateFormat MIN_FORMAT =
639 new SimpleDateFormat("HH:mm"); //$NON-NLS-1$
640 protected static final SimpleDateFormat HOURS_FORMAT_HEADER =
641 new SimpleDateFormat("yyyy"); //$NON-NLS-1$
642 protected static final SimpleDateFormat HOURS_FORMAT =
643 new SimpleDateFormat("MMM dd HH:mm"); //$NON-NLS-1$
644 protected static final SimpleDateFormat DAY_FORMAT_HEADER =
645 new SimpleDateFormat("yyyy"); //$NON-NLS-1$
646 protected static final SimpleDateFormat DAY_FORMAT =
647 new SimpleDateFormat("MMM dd"); //$NON-NLS-1$
648 protected static final SimpleDateFormat MONTH_FORMAT =
649 new SimpleDateFormat("yyyy MMM"); //$NON-NLS-1$
650 protected static final SimpleDateFormat YEAR_FORMAT =
651 new SimpleDateFormat("yyyy"); //$NON-NLS-1$
652
653 protected static final List<SimpleDateFormat> formats;
654 static
655 {
656 ImmutableList.Builder<SimpleDateFormat> formatArrayBuilder = ImmutableList.<SimpleDateFormat> builder();
657 formatArrayBuilder.add(SEC_FORMAT);
658 formatArrayBuilder.add(SEC_FORMAT_HEADER);
659 formatArrayBuilder.add(MIN_FORMAT);
660 formatArrayBuilder.add(MIN_FORMAT_HEADER);
661 formatArrayBuilder.add(HOURS_FORMAT);
662 formatArrayBuilder.add(HOURS_FORMAT_HEADER);
663 formatArrayBuilder.add(DAY_FORMAT);
664 formatArrayBuilder.add(DAY_FORMAT_HEADER);
665 formatArrayBuilder.add(MONTH_FORMAT);
666 formatArrayBuilder.add(YEAR_FORMAT);
667 formats = formatArrayBuilder.build();
668 }
c1cd9635
MAL
669
670 /**
671 * Updates the timezone using the preferences.
672 */
673 public static void updateTimeZone() {
7c34a4ea 674 final TimeZone timeZone = TmfTimePreferences.getTimeZone();
6dcc38b9
MK
675 for (SimpleDateFormat sdf : formats) {
676 synchronized (sdf) {
677 sdf.setTimeZone(timeZone);
678 }
c1cd9635
MAL
679 }
680 }
681
8e1f81f1
PT
682 static String sep(long n) {
683 StringBuilder retVal = new StringBuilder();
684 String s = Long.toString(n);
685 for (int i = 0; i < s.length(); i++) {
686 int pos = s.length() - i - 1;
687 retVal.append(s.charAt(i));
688 if (pos % 3 == 0 && pos != 0) {
689 retVal.append(' ');
690 }
691 }
692 return retVal.toString();
693 }
694
695 static String pad(long n) {
696 String s;
697 if (n < 10) {
698 s = S00;
699 } else if (n < 100) {
700 s = S0;
701 } else {
702 s = S;
703 }
704 return s + n;
705 }
706
0fab12b0 707 public abstract int draw(GC gc, long time, Rectangle rect);
8e1f81f1 708
a0a88f65 709 /**
f1fae91f 710 * Override to draw absolute time header. This is for the time information
a0a88f65
AM
711 * not shown in the draw of each tick
712 *
713 * @param gc
714 * Graphics context
f1fae91f
PT
715 * @param nanosec
716 * time in nanosec
a0a88f65
AM
717 * @param absHeaderRect
718 * Header rectangle
719 */
f1fae91f 720 public void drawAbsHeader(GC gc, long nanosec, Rectangle absHeaderRect) {
8e1f81f1 721 }
2b3be043
JCK
722
723 protected void drawAbsHeader(GC gc, long nanosec, Rectangle rect, SimpleDateFormat dateFormat) {
724 String header;
725 synchronized (dateFormat) {
726 header = dateFormat.format(new Date(nanosec / MILLISEC_IN_NS));
727 }
728 int headerwidth = gc.stringExtent(header).x + 4;
729 if (headerwidth <= rect.width) {
730 rect.x += (rect.width - headerwidth);
731 Utils.drawText(gc, header, rect, true);
732 }
733 }
8e1f81f1
PT
734}
735
736class TimeDrawSec extends TimeDraw {
8e1f81f1 737 @Override
0fab12b0 738 public int draw(GC gc, long nanosec, Rectangle rect) {
f1fae91f 739 long sec = nanosec / SEC_IN_NS;
0fab12b0 740 return Utils.drawText(gc, sep(sec), rect, true);
8e1f81f1
PT
741 }
742}
743
744class TimeDrawMillisec extends TimeDraw {
8e1f81f1 745 @Override
0fab12b0 746 public int draw(GC gc, long nanosec, Rectangle rect) {
f1fae91f
PT
747 long millisec = nanosec / MILLISEC_IN_NS;
748 long ms = millisec % PAD_1000;
749 long sec = millisec / SEC_IN_MS;
0fab12b0 750 return Utils.drawText(gc, sep(sec) + "." + pad(ms), rect, true); //$NON-NLS-1$
8e1f81f1
PT
751 }
752}
753
754class TimeDrawMicrosec extends TimeDraw {
8e1f81f1 755 @Override
0fab12b0 756 public int draw(GC gc, long nanosec, Rectangle rect) {
f1fae91f
PT
757 long microsec = nanosec / MICROSEC_IN_NS;
758 long us = microsec % PAD_1000;
759 long millisec = microsec / MILLISEC_IN_US;
760 long ms = millisec % PAD_1000;
761 long sec = millisec / SEC_IN_MS;
0fab12b0 762 return Utils.drawText(gc, sep(sec) + "." + pad(ms) + " " + pad(us), rect, true); //$NON-NLS-1$ //$NON-NLS-2$
8e1f81f1
PT
763 }
764}
765
766class TimeDrawNanosec extends TimeDraw {
8e1f81f1 767 @Override
0fab12b0 768 public int draw(GC gc, long nanosec, Rectangle rect) {
f1fae91f
PT
769 long ns = nanosec % PAD_1000;
770 long microsec = nanosec / MICROSEC_IN_NS;
771 long us = microsec % PAD_1000;
772 long millisec = microsec / MILLISEC_IN_US;
773 long ms = millisec % PAD_1000;
774 long sec = millisec / SEC_IN_MS;
0fab12b0 775 return Utils.drawText(gc, sep(sec) + "." + pad(ms) + " " + pad(us) + " " + pad(ns), rect, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
8e1f81f1
PT
776 }
777}
778
779class TimeDrawAbsYear extends TimeDraw {
8e1f81f1 780 @Override
0fab12b0 781 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
782 String stime;
783 synchronized (YEAR_FORMAT) {
784 stime = YEAR_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
785 }
0fab12b0 786 return Utils.drawText(gc, stime, rect, true);
8e1f81f1 787 }
8e1f81f1
PT
788}
789
790class TimeDrawAbsMonth extends TimeDraw {
8e1f81f1 791 @Override
0fab12b0 792 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
793 String stime;
794 synchronized (MONTH_FORMAT) {
795 stime = MONTH_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
796 }
0fab12b0 797 return Utils.drawText(gc, stime, rect, true);
8e1f81f1 798 }
8e1f81f1
PT
799}
800
801class TimeDrawAbsDay extends TimeDraw {
8e1f81f1 802 @Override
0fab12b0 803 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
804 String stime;
805 synchronized (DAY_FORMAT) {
806 stime = DAY_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
807 }
0fab12b0 808 return Utils.drawText(gc, stime, rect, true);
8e1f81f1
PT
809 }
810
811 @Override
f1fae91f 812 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 813 drawAbsHeader(gc, nanosec, rect, DAY_FORMAT_HEADER);
8e1f81f1 814 }
8e1f81f1
PT
815}
816
817class TimeDrawAbsHrs extends TimeDraw {
8e1f81f1 818 @Override
0fab12b0 819 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
820 String stime;
821 synchronized (HOURS_FORMAT) {
822 stime = HOURS_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
823 }
0fab12b0 824 return Utils.drawText(gc, stime, rect, true);
8e1f81f1
PT
825 }
826
827 @Override
f1fae91f 828 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 829 drawAbsHeader(gc, nanosec, rect, HOURS_FORMAT_HEADER);
8e1f81f1 830 }
8e1f81f1
PT
831}
832
833class TimeDrawAbsMin extends TimeDraw {
8e1f81f1 834 @Override
0fab12b0 835 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
836 String stime;
837 synchronized (MIN_FORMAT) {
838 stime = MIN_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
839 }
0fab12b0 840 return Utils.drawText(gc, stime, rect, true);
8e1f81f1
PT
841 }
842
843 @Override
f1fae91f 844 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 845 drawAbsHeader(gc, nanosec, rect, MIN_FORMAT_HEADER);
8e1f81f1 846 }
8e1f81f1
PT
847}
848
849class TimeDrawAbsSec extends TimeDraw {
8e1f81f1 850 @Override
0fab12b0 851 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
852 String stime;
853 synchronized (SEC_FORMAT) {
854 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
855 }
0fab12b0 856 return Utils.drawText(gc, stime, rect, true);
8e1f81f1
PT
857 }
858
859 @Override
f1fae91f 860 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 861 drawAbsHeader(gc, nanosec, rect, SEC_FORMAT_HEADER);
8e1f81f1 862 }
8e1f81f1
PT
863}
864
865class TimeDrawAbsMillisec extends TimeDraw {
8e1f81f1 866 @Override
0fab12b0 867 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
868 String stime;
869 synchronized (SEC_FORMAT) {
870 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
871 }
f1fae91f 872 String ns = Utils.formatNs(nanosec, Resolution.MILLISEC);
0fab12b0 873 return Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$
8e1f81f1
PT
874 }
875
876 @Override
f1fae91f 877 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 878 drawAbsHeader(gc, nanosec, rect, SEC_FORMAT_HEADER);
8e1f81f1 879 }
8e1f81f1
PT
880}
881
882class TimeDrawAbsMicroSec extends TimeDraw {
8e1f81f1 883 @Override
0fab12b0 884 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
885 String stime;
886 synchronized (SEC_FORMAT) {
887 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
888 }
f1fae91f 889 String micr = Utils.formatNs(nanosec, Resolution.MICROSEC);
0fab12b0 890 return Utils.drawText(gc, stime + "." + micr, rect, true); //$NON-NLS-1$
8e1f81f1
PT
891 }
892
893 @Override
f1fae91f 894 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 895 drawAbsHeader(gc, nanosec, rect, SEC_FORMAT_HEADER);
8e1f81f1 896 }
8e1f81f1
PT
897}
898
899class TimeDrawAbsNanoSec extends TimeDraw {
8e1f81f1 900 @Override
0fab12b0 901 public int draw(GC gc, long nanosec, Rectangle rect) {
6dcc38b9
MK
902 String stime;
903 synchronized (SEC_FORMAT) {
904 stime = SEC_FORMAT.format(new Date(nanosec / MILLISEC_IN_NS));
905 }
f1fae91f 906 String ns = Utils.formatNs(nanosec, Resolution.NANOSEC);
0fab12b0 907 return Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$
8e1f81f1
PT
908 }
909
910 @Override
f1fae91f 911 public void drawAbsHeader(GC gc, long nanosec, Rectangle rect) {
2b3be043 912 drawAbsHeader(gc, nanosec, rect, SEC_FORMAT_HEADER);
8e1f81f1 913 }
8e1f81f1 914}
026664b7 915
026664b7 916class TimeDrawNumber extends TimeDraw {
026664b7 917 @Override
0fab12b0 918 public int draw(GC gc, long time, Rectangle rect) {
026664b7 919 String stime = NumberFormat.getInstance().format(time);
0fab12b0
PT
920 return Utils.drawText(gc, stime, rect, true);
921 }
922}
923
924class TimeDrawCycles extends TimeDraw {
925 @Override
926 public int draw(GC gc, long time, Rectangle rect) {
927 String stime = Utils.formatTime(time, TimeFormat.CYCLES, Resolution.SECONDS);
928 return Utils.drawText(gc, stime, rect, true);
026664b7 929 }
2b3be043 930}
This page took 0.12757 seconds and 5 git commands to generate.