Commit | Line | Data |
---|---|---|
8e1f81f1 PT |
1 | /***************************************************************************** |
2 | * Copyright (c) 2007, 2008 Intel Corporation, 2010, 2012 Ericsson. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Intel Corporation - Initial API and implementation | |
10 | * Ruslan A. Scherbakov, Intel - Initial API and implementation | |
11 | * Alvaro Sanchez-Leon - Updated for TMF | |
12 | * Patrick Tasse - Refactoring | |
13 | * | |
14 | *****************************************************************************/ | |
15 | ||
16 | package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets; | |
17 | ||
18 | import java.text.SimpleDateFormat; | |
19 | import java.util.Calendar; | |
20 | import java.util.Date; | |
21 | ||
22 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; | |
23 | import org.eclipse.swt.SWT; | |
24 | import org.eclipse.swt.events.MouseEvent; | |
25 | import org.eclipse.swt.events.MouseListener; | |
26 | import org.eclipse.swt.events.MouseMoveListener; | |
27 | import org.eclipse.swt.events.PaintEvent; | |
28 | import org.eclipse.swt.graphics.GC; | |
29 | import org.eclipse.swt.graphics.Point; | |
30 | import org.eclipse.swt.graphics.Rectangle; | |
31 | import org.eclipse.swt.widgets.Composite; | |
32 | ||
33 | /** | |
34 | * Implementation of the scale for the time graph view. | |
35 | * | |
36 | * This goes above the "gantt chart" area. | |
37 | * | |
38 | * @version 1.0 | |
39 | * @author Alvaro Sanchez-Leon | |
40 | * @author Patrick Tasse | |
41 | */ | |
42 | public class TimeGraphScale extends TimeGraphBaseControl implements | |
43 | MouseListener, MouseMoveListener { | |
44 | ||
45 | /** | |
46 | * Standard constructor | |
47 | * | |
48 | * @param parent | |
49 | * The parent composite object | |
50 | * @param colors | |
51 | * The color scheme to use | |
52 | */ | |
53 | public TimeGraphScale(Composite parent, TimeGraphColorScheme colors) { | |
54 | super(parent, colors, SWT.NO_BACKGROUND | SWT.NO_FOCUS | SWT.DOUBLE_BUFFERED); | |
55 | addMouseListener(this); | |
56 | addMouseMoveListener(this); | |
57 | } | |
58 | ||
59 | private static final long SEC_IN_NS = 1000000000; | |
60 | private static final long MIN_IN_NS = 60 * SEC_IN_NS; | |
61 | private static final long HOUR_IN_NS = 60 * MIN_IN_NS; | |
62 | private static final long DAY_IN_NS = 24 * HOUR_IN_NS; | |
63 | private static final long MONTH_IN_NS = 31 * DAY_IN_NS; // upper limit | |
64 | private static final long YEAR_IN_NS = 366 * DAY_IN_NS; // upper limit | |
65 | ||
66 | private static final double LOG10_1 = Math.log10(1); | |
67 | private static final double LOG10_2 = Math.log10(2); | |
68 | private static final double LOG10_3 = Math.log10(3); | |
69 | private static final double LOG10_5 = Math.log10(5); | |
70 | ||
71 | private static final Calendar GREGORIAN_CALENDAR = Calendar.getInstance(); | |
72 | ||
73 | private ITimeDataProvider _timeProvider; | |
74 | private int _dragState = 0; | |
75 | private int _dragX0 = 0; | |
76 | private int _dragX = 0; | |
77 | private long _time0bak; | |
78 | private long _time1bak; | |
79 | private boolean _isInUpdate; | |
80 | private final Rectangle _rect0 = new Rectangle(0, 0, 0, 0); | |
81 | private int _height; | |
82 | ||
83 | /** | |
84 | * Assign the time provider for this scale | |
85 | * | |
86 | * @param timeProvider | |
87 | * The provider to use | |
88 | */ | |
89 | public void setTimeProvider(ITimeDataProvider timeProvider) { | |
90 | _timeProvider = timeProvider; | |
91 | } | |
92 | ||
93 | private long _timeDelta; | |
94 | ||
95 | @Override | |
96 | public Point computeSize(int wHint, int hHint, boolean changed) { | |
97 | return super.computeSize(wHint, _height, changed); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Set the height of the scale | |
102 | * | |
103 | * @param height | |
104 | * The height to use | |
105 | */ | |
106 | public void setHeight(int height) { | |
107 | this._height = height; | |
108 | } | |
109 | ||
110 | private void calcTimeDelta(int width, double pixelsPerNanoSec) { | |
111 | double minDelta = (pixelsPerNanoSec == 0) ? YEAR_IN_NS : width / pixelsPerNanoSec; | |
112 | long unit = 1; | |
113 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
114 | if (minDelta > 6 * MONTH_IN_NS) { | |
115 | unit = YEAR_IN_NS; | |
116 | } else if (minDelta > 3 * MONTH_IN_NS) { | |
117 | unit = 6 * MONTH_IN_NS; | |
118 | } else if (minDelta > 10 * DAY_IN_NS) { | |
119 | unit = MONTH_IN_NS; | |
120 | } else if (minDelta > 12 * HOUR_IN_NS) { | |
121 | unit = DAY_IN_NS; | |
122 | } else if (minDelta > 3 * HOUR_IN_NS) { | |
123 | unit = 6 * HOUR_IN_NS; | |
124 | } else if (minDelta > 30 * MIN_IN_NS) { | |
125 | unit = HOUR_IN_NS; | |
126 | } else if (minDelta > 10 * MIN_IN_NS) { | |
127 | unit = 15 * MIN_IN_NS; | |
128 | } else if (minDelta > 30 * SEC_IN_NS) { | |
129 | unit = MIN_IN_NS; | |
130 | } else if (minDelta > 20 * SEC_IN_NS) { | |
131 | unit = 30 * SEC_IN_NS; | |
132 | } else if (minDelta <= 1) { | |
133 | _timeDelta = 1; | |
134 | return; | |
135 | } | |
136 | } | |
137 | double log = Math.log10(minDelta / unit); | |
138 | long pow10 = (long) log; | |
139 | double remainder = log - pow10; | |
140 | if (remainder < LOG10_1) { | |
141 | _timeDelta = (long) Math.pow(10, pow10) * unit; | |
142 | } else if (remainder < LOG10_2) { | |
143 | _timeDelta = 2 * (long) Math.pow(10, pow10) * unit; | |
144 | } else if (remainder < LOG10_3 && unit >= HOUR_IN_NS && unit < YEAR_IN_NS) { | |
145 | _timeDelta = 3 * (long) Math.pow(10, pow10) * unit; | |
146 | } else if (remainder < LOG10_5) { | |
147 | _timeDelta = 5 * (long) Math.pow(10, pow10) * unit; | |
148 | } else { | |
149 | _timeDelta = 10 * (long) Math.pow(10, pow10) * unit; | |
150 | } | |
f85266f3 XR |
151 | if (_timeDelta<=0) { |
152 | _timeDelta=1; | |
153 | } | |
8e1f81f1 PT |
154 | } |
155 | ||
156 | private static TimeDraw TIMEDRAW_NANOSEC = new TimeDrawNanosec(); | |
157 | private static TimeDraw TIMEDRAW_MICROSEC = new TimeDrawMicrosec(); | |
158 | private static TimeDraw TIMEDRAW_MILLISEC = new TimeDrawMillisec(); | |
159 | private static TimeDraw TIMEDRAW_SEC = new TimeDrawSec(); | |
160 | private static TimeDraw TIMEDRAW_ABS_NANOSEC = new TimeDrawAbsNanoSec(); | |
161 | private static TimeDraw TIMEDRAW_ABS_MICROSEC = new TimeDrawAbsMicroSec(); | |
162 | private static TimeDraw TIMEDRAW_ABS_MILLISEC = new TimeDrawAbsMillisec(); | |
163 | private static TimeDraw TIMEDRAW_ABS_SEC = new TimeDrawAbsSec(); | |
164 | private static TimeDraw TIMEDRAW_ABS_MIN = new TimeDrawAbsMin(); | |
165 | private static TimeDraw TIMEDRAW_ABS_HRS = new TimeDrawAbsHrs(); | |
166 | private static TimeDraw TIMEDRAW_ABS_DAY = new TimeDrawAbsDay(); | |
167 | private static TimeDraw TIMEDRAW_ABS_MONTH = new TimeDrawAbsMonth(); | |
168 | private static TimeDraw TIMEDRAW_ABS_YEAR = new TimeDrawAbsYear(); | |
169 | ||
170 | TimeDraw getTimeDraw(long timeDelta) { | |
171 | TimeDraw timeDraw; | |
172 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
173 | if (timeDelta >= YEAR_IN_NS) { | |
174 | timeDraw = TIMEDRAW_ABS_YEAR; | |
175 | } else if (timeDelta >= MONTH_IN_NS) { | |
176 | timeDraw = TIMEDRAW_ABS_MONTH; | |
177 | } else if (timeDelta >= DAY_IN_NS) { | |
178 | timeDraw = TIMEDRAW_ABS_DAY; | |
179 | } else if (timeDelta >= HOUR_IN_NS) { | |
180 | timeDraw = TIMEDRAW_ABS_HRS; | |
181 | } else if (timeDelta >= MIN_IN_NS) { | |
182 | timeDraw = TIMEDRAW_ABS_MIN; | |
183 | } else if (timeDelta >= SEC_IN_NS) { | |
184 | timeDraw = TIMEDRAW_ABS_SEC; | |
185 | } else if (timeDelta >= 1000000) { | |
186 | timeDraw = TIMEDRAW_ABS_MILLISEC; | |
187 | } else if (timeDelta >= 1000) { | |
188 | timeDraw = TIMEDRAW_ABS_MICROSEC; | |
189 | } else { | |
190 | timeDraw = TIMEDRAW_ABS_NANOSEC; | |
191 | } | |
192 | return timeDraw; | |
193 | } | |
194 | if (timeDelta >= 1000000000) { | |
195 | timeDraw = TIMEDRAW_SEC; | |
196 | } else if (timeDelta >= 1000000) { | |
197 | timeDraw = TIMEDRAW_MILLISEC; | |
198 | } else if (timeDelta >= 1000) { | |
199 | timeDraw = TIMEDRAW_MICROSEC; | |
200 | } else { | |
201 | timeDraw = TIMEDRAW_NANOSEC; | |
202 | } | |
203 | return timeDraw; | |
204 | } | |
205 | ||
206 | @Override | |
207 | void paint(Rectangle rect, PaintEvent e) { | |
208 | ||
209 | if (_isInUpdate || null == _timeProvider) { | |
210 | return; | |
211 | } | |
212 | ||
213 | GC gc = e.gc; | |
214 | gc.fillRectangle(rect); | |
215 | ||
216 | long time0 = _timeProvider.getTime0(); | |
217 | long time1 = _timeProvider.getTime1(); | |
218 | long selectedTime = _timeProvider.getSelectedTime(); | |
219 | int leftSpace = _timeProvider.getNameSpace(); | |
220 | int timeSpace = _timeProvider.getTimeSpace(); | |
221 | ||
222 | gc.setBackground(_colors.getColor(TimeGraphColorScheme.TOOL_BACKGROUND)); | |
223 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); | |
224 | Utils.init(_rect0, rect); | |
225 | ||
226 | // draw top left area | |
227 | _rect0.width = leftSpace; | |
228 | _rect0.x += 4; | |
229 | _rect0.width -= 4; | |
230 | Rectangle absHeaderRect = new Rectangle(_rect0.x, _rect0.y, _rect0.width, _rect0.height); | |
231 | _rect0.x -= 4; | |
232 | _rect0.width += 4; | |
233 | ||
234 | // prepare and draw right rect of the timescale | |
235 | _rect0.x += leftSpace; | |
236 | _rect0.width = rect.width - leftSpace; | |
237 | ||
238 | // draw bottom border and erase all other area | |
239 | gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, | |
240 | rect.y + rect.height - 1); | |
241 | _rect0.height--; | |
242 | gc.fillRectangle(_rect0); | |
243 | ||
5b2b9bd7 PT |
244 | if (3 == _dragState && null != _timeProvider) { |
245 | // draw selected zoom region background | |
246 | gc.setBackground(_colors.getBkColor(true, false, true)); | |
247 | if (_dragX0 < _dragX) { | |
248 | gc.fillRectangle(new Rectangle(leftSpace + _dragX0, _rect0.y, _dragX - _dragX0, _rect0.height)); | |
249 | } else if (_dragX0 > _dragX) { | |
250 | gc.fillRectangle(new Rectangle(leftSpace + _dragX, _rect0.y, _dragX0 - _dragX, _rect0.height)); | |
251 | } | |
252 | } | |
253 | ||
8e1f81f1 PT |
254 | if (time1 <= time0 || timeSpace < 2) { |
255 | return; | |
256 | } | |
257 | ||
258 | int numDigits = calculateDigits(time0, time1); | |
259 | ||
260 | int labelWidth = gc.getCharWidth('0') * numDigits; | |
261 | double pixelsPerNanoSec = (timeSpace <= RIGHT_MARGIN) ? 0 : | |
262 | (double) (timeSpace - RIGHT_MARGIN) / (time1 - time0); | |
263 | calcTimeDelta(labelWidth, pixelsPerNanoSec); | |
264 | ||
265 | TimeDraw timeDraw = getTimeDraw(_timeDelta); | |
266 | ||
5b2b9bd7 | 267 | // draw selected zoom region lines |
8e1f81f1 | 268 | if (3 == _dragState && null != _timeProvider) { |
5b2b9bd7 PT |
269 | gc.drawLine(leftSpace + _dragX0, rect.y, leftSpace + _dragX0, rect.y + rect.height); |
270 | gc.drawLine(leftSpace + _dragX, rect.y, leftSpace + _dragX, rect.y + rect.height); | |
8e1f81f1 PT |
271 | } |
272 | ||
273 | if (_rect0.isEmpty()) { | |
274 | return; | |
275 | } | |
276 | ||
277 | // draw selected time | |
278 | int x = _rect0.x + (int) ((selectedTime - time0) * pixelsPerNanoSec); | |
279 | if (x >= _rect0.x && x < _rect0.x + _rect0.width) { | |
280 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.SELECTED_TIME)); | |
281 | gc.drawLine(x, _rect0.y + _rect0.height - 6, x, _rect0.y | |
282 | + _rect0.height); | |
283 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); | |
284 | } | |
285 | ||
286 | // draw time scale ticks | |
287 | _rect0.y = rect.y; | |
288 | _rect0.height = rect.height - 4; | |
289 | _rect0.width = labelWidth; | |
290 | ||
291 | long time; | |
292 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
293 | time = floorToCalendar(time0, _timeDelta); | |
294 | } else { | |
295 | time = (time0 / _timeDelta) * _timeDelta; | |
296 | if (time != time0) { | |
297 | time += _timeDelta; | |
298 | } | |
299 | } | |
300 | ||
301 | int y = _rect0.y + _rect0.height; | |
302 | ||
303 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
304 | timeDraw.drawAbsHeader(gc, time, absHeaderRect); | |
305 | } | |
306 | ||
307 | while (true) { | |
308 | x = rect.x + leftSpace + (int) (Math.floor((time - time0) * pixelsPerNanoSec)); | |
309 | if (x >= rect.x + leftSpace + rect.width - _rect0.width) { | |
310 | break; | |
311 | } | |
312 | if (x >= rect.x + leftSpace) { | |
313 | gc.drawLine(x, y, x, y + 4); | |
314 | _rect0.x = x; | |
315 | if (x + _rect0.width <= rect.x + rect.width) { | |
316 | timeDraw.draw(gc, time, _rect0); | |
317 | } | |
318 | } | |
319 | if (pixelsPerNanoSec == 0 || time > Long.MAX_VALUE - _timeDelta || _timeDelta == 0) { | |
320 | break; | |
321 | } | |
322 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
323 | if (_timeDelta >= YEAR_IN_NS) { | |
324 | long millis = time / 1000000L; | |
325 | GREGORIAN_CALENDAR.setTime(new Date(millis)); | |
326 | GREGORIAN_CALENDAR.add(Calendar.YEAR, (int) (_timeDelta / YEAR_IN_NS)); | |
327 | millis = GREGORIAN_CALENDAR.getTimeInMillis(); | |
328 | time = millis * 1000000L; | |
329 | } else if (_timeDelta >= MONTH_IN_NS) { | |
330 | long millis = time / 1000000L; | |
331 | GREGORIAN_CALENDAR.setTime(new Date(millis)); | |
332 | GREGORIAN_CALENDAR.add(Calendar.MONTH, (int) (_timeDelta / MONTH_IN_NS)); | |
333 | millis = GREGORIAN_CALENDAR.getTimeInMillis(); | |
334 | time = millis * 1000000L; | |
335 | } else if (_timeDelta >= DAY_IN_NS) { | |
336 | long millis = time / 1000000L; | |
337 | GREGORIAN_CALENDAR.setTime(new Date(millis)); | |
338 | GREGORIAN_CALENDAR.add(Calendar.DAY_OF_MONTH, (int) (_timeDelta / DAY_IN_NS)); | |
339 | millis = GREGORIAN_CALENDAR.getTimeInMillis(); | |
340 | time = millis * 1000000L; | |
341 | } else { | |
342 | time += _timeDelta; | |
343 | } | |
344 | } else { | |
345 | time += _timeDelta; | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | private long floorToCalendar(long time, long timeDelta) { | |
351 | if (_timeDelta >= YEAR_IN_NS) { | |
352 | GREGORIAN_CALENDAR.setTime(new Date(time / 1000000)); | |
353 | int year = GREGORIAN_CALENDAR.get(Calendar.YEAR); | |
354 | int yearDelta = (int) (timeDelta / YEAR_IN_NS); | |
355 | year = (year / yearDelta) * yearDelta; | |
356 | GREGORIAN_CALENDAR.set(Calendar.YEAR, year); | |
357 | GREGORIAN_CALENDAR.set(Calendar.MONTH, 0); // January 1st of year | |
358 | GREGORIAN_CALENDAR.set(Calendar.DAY_OF_MONTH, 1); | |
359 | GREGORIAN_CALENDAR.set(Calendar.HOUR_OF_DAY, 0); | |
360 | GREGORIAN_CALENDAR.set(Calendar.MINUTE, 0); | |
361 | GREGORIAN_CALENDAR.set(Calendar.SECOND, 0); | |
362 | GREGORIAN_CALENDAR.set(Calendar.MILLISECOND, 0); | |
363 | time = GREGORIAN_CALENDAR.getTimeInMillis() * 1000000; | |
364 | } else if (_timeDelta >= MONTH_IN_NS) { | |
365 | GREGORIAN_CALENDAR.setTime(new Date(time / 1000000)); | |
366 | int month = GREGORIAN_CALENDAR.get(Calendar.MONTH); | |
367 | int monthDelta = (int) (timeDelta / MONTH_IN_NS); | |
368 | month = (month / monthDelta) * monthDelta; | |
369 | GREGORIAN_CALENDAR.set(Calendar.MONTH, month); | |
370 | GREGORIAN_CALENDAR.set(Calendar.DAY_OF_MONTH, 1); // 1st of month | |
371 | GREGORIAN_CALENDAR.set(Calendar.HOUR_OF_DAY, 0); | |
372 | GREGORIAN_CALENDAR.set(Calendar.MINUTE, 0); | |
373 | GREGORIAN_CALENDAR.set(Calendar.SECOND, 0); | |
374 | GREGORIAN_CALENDAR.set(Calendar.MILLISECOND, 0); | |
375 | time = GREGORIAN_CALENDAR.getTimeInMillis() * 1000000; | |
376 | } else { | |
377 | long offset = GREGORIAN_CALENDAR.getTimeZone().getOffset(time / 1000000L) * 1000000L; | |
378 | time += offset; | |
379 | time = (time / timeDelta) * timeDelta; | |
380 | time -= offset; | |
381 | } | |
382 | return time; | |
383 | } | |
384 | ||
385 | private int calculateDigits(long time0, long time1) { | |
386 | int numDigits = 5; | |
387 | long timeRange = time1 - time0; | |
388 | ||
389 | if (_timeProvider.isCalendarFormat()) { | |
390 | // Calculate the number of digits to represent the minutes provided | |
391 | // 11:222 | |
392 | // HH:mm:ss | |
393 | numDigits += 8; | |
394 | if (timeRange < 10000) { | |
395 | // HH:11:222:333:444__ | |
396 | numDigits += 10; | |
397 | } else if (timeRange < 10000000) { | |
398 | // HH:11:222:333__ | |
399 | numDigits += 6; | |
400 | } | |
401 | } else { | |
402 | long sec = time1 / 1000000000; | |
403 | numDigits = Long.toString(sec).length(); | |
404 | int thousandGroups = (numDigits - 1) / 3; | |
405 | numDigits += thousandGroups; | |
406 | numDigits += 12; // .000 000 000 | |
407 | } | |
408 | ||
409 | return numDigits; | |
410 | } | |
411 | ||
412 | @Override | |
413 | public void mouseDown(MouseEvent e) { | |
414 | getParent().setFocus(); | |
415 | if (_dragState == 0 && null != _timeProvider) { | |
416 | int x = e.x - _timeProvider.getNameSpace(); | |
417 | if (1 == e.button && x > 0) { | |
418 | setCapture(true); | |
419 | _dragState = 1; | |
420 | } else if (3 == e.button) { | |
421 | _dragState = 3; | |
422 | } | |
423 | if (x < 0) { | |
424 | x = 0; | |
425 | } else if (x > getSize().x - _timeProvider.getNameSpace()) { | |
426 | x = getSize().x - _timeProvider.getNameSpace(); | |
427 | } | |
428 | _dragX = _dragX0 = x; | |
429 | _time0bak = _timeProvider.getTime0(); | |
430 | _time1bak = _timeProvider.getTime1(); | |
431 | } | |
432 | } | |
433 | ||
434 | @Override | |
435 | public void mouseUp(MouseEvent e) { | |
436 | if (e.button == 1 && _dragState == 1) { | |
437 | setCapture(false); | |
438 | _dragState = 0; | |
439 | ||
440 | // Notify time provider to check the need for listener notification | |
441 | if (_dragX != _dragX0 && _timeProvider.getTime0() != _timeProvider.getTime1()) { | |
442 | _timeProvider.setStartFinishTimeNotify(_timeProvider.getTime0(), _timeProvider.getTime1()); | |
443 | } | |
444 | } else if (e.button == 3 && _dragState == 3 && null != _timeProvider) { | |
445 | _dragState = 0; | |
446 | if (_dragX0 == _dragX || _timeProvider.getTime0() == _timeProvider.getTime1()) { | |
5b2b9bd7 | 447 | redraw(); |
8e1f81f1 PT |
448 | return; |
449 | } | |
450 | int timeSpace = _timeProvider.getTimeSpace(); | |
451 | int leftSpace = _timeProvider.getNameSpace(); | |
452 | int x = Math.max(0, e.x - leftSpace); | |
453 | if (timeSpace > 0) { | |
454 | _dragX = x; | |
455 | if (_dragX0 > _dragX) { // drag right to left | |
456 | _dragX = _dragX0; | |
457 | _dragX0 = x; | |
458 | } | |
459 | long time0 = _time0bak + (long) ((_time1bak - _time0bak) * ((double) _dragX0 / timeSpace)); | |
460 | long time1 = _time0bak + (long) ((_time1bak - _time0bak) * ((double) _dragX / timeSpace)); | |
461 | ||
462 | _timeProvider.setStartFinishTimeNotify(time0, time1); | |
463 | _time0bak = _timeProvider.getTime0(); | |
464 | _time1bak = _timeProvider.getTime1(); | |
465 | } | |
466 | } | |
467 | } | |
468 | ||
469 | @Override | |
470 | public void mouseMove(MouseEvent e) { | |
471 | if (_dragX0 < 0 || _dragState == 0 || _timeProvider == null) { | |
472 | return; | |
473 | } | |
474 | Point size = getSize(); | |
475 | int leftSpace = _timeProvider.getNameSpace(); | |
476 | int timeSpace = _timeProvider.getTimeSpace(); | |
477 | int x = e.x - leftSpace; | |
478 | if (1 == _dragState) { | |
479 | if (x > 0 && size.x > leftSpace && _dragX != x) { | |
480 | _dragX = x; | |
481 | if (_timeProvider.getTime0() == _timeProvider.getTime1()) { | |
482 | return; | |
483 | } | |
484 | long interval = (long) ((_time1bak - _time0bak) * ((double) _dragX0 / _dragX)); | |
485 | if (interval == Long.MAX_VALUE) { | |
486 | _timeProvider.setStartFinishTime(_time0bak, Long.MAX_VALUE); | |
487 | } else { | |
488 | long time1 = _time0bak + (long) ((_time1bak - _time0bak) * ((double) _dragX0 / _dragX)); | |
489 | _timeProvider.setStartFinishTime(_time0bak, time1); | |
490 | } | |
491 | } | |
492 | } else if (3 == _dragState) { | |
493 | if (x < 0) { | |
494 | _dragX = 0; | |
495 | } else if (x > timeSpace) { | |
496 | _dragX = timeSpace; | |
497 | } else { | |
498 | _dragX = x; | |
499 | } | |
500 | redraw(); | |
501 | } | |
502 | } | |
503 | ||
504 | @Override | |
505 | public void mouseDoubleClick(MouseEvent e) { | |
5b2b9bd7 | 506 | if (e.button == 1 && null != _timeProvider && _timeProvider.getTime0() != _timeProvider.getTime1() && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
8e1f81f1 | 507 | _timeProvider.resetStartFinishTime(); |
13ccc36b | 508 | _timeProvider.notifyStartFinishTime(); |
8e1f81f1 PT |
509 | _time0bak = _timeProvider.getTime0(); |
510 | _time1bak = _timeProvider.getTime1(); | |
511 | } | |
512 | } | |
513 | } | |
514 | ||
515 | abstract class TimeDraw { | |
516 | static String S = "" ; //$NON-NLS-1$ | |
517 | static String S0 = "0" ; //$NON-NLS-1$ | |
518 | static String S00 = "00"; //$NON-NLS-1$ | |
519 | protected static final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$ | |
520 | protected static final SimpleDateFormat stimeformatheader = new SimpleDateFormat("yyyy MMM dd"); //$NON-NLS-1$ | |
521 | protected static final SimpleDateFormat sminformat = new SimpleDateFormat("HH:mm"); //$NON-NLS-1$ | |
522 | protected static final SimpleDateFormat sminformatheader = new SimpleDateFormat("yyyy MMM dd"); //$NON-NLS-1$ | |
523 | protected static final SimpleDateFormat shrsformat = new SimpleDateFormat("MMM dd HH:mm"); //$NON-NLS-1$ | |
524 | protected static final SimpleDateFormat shrsformatheader = new SimpleDateFormat("yyyy"); //$NON-NLS-1$ | |
525 | protected static final SimpleDateFormat sdayformat = new SimpleDateFormat("MMM dd"); //$NON-NLS-1$ | |
526 | protected static final SimpleDateFormat sdayformatheader = new SimpleDateFormat("yyyy"); //$NON-NLS-1$ | |
527 | protected static final SimpleDateFormat smonthformat = new SimpleDateFormat("yyyy MMM"); //$NON-NLS-1$ | |
528 | protected static final SimpleDateFormat syearformat = new SimpleDateFormat("yyyy"); //$NON-NLS-1$ | |
529 | ||
530 | static String sep(long n) { | |
531 | StringBuilder retVal = new StringBuilder(); | |
532 | String s = Long.toString(n); | |
533 | for (int i = 0; i < s.length(); i++) { | |
534 | int pos = s.length() - i - 1; | |
535 | retVal.append(s.charAt(i)); | |
536 | if (pos % 3 == 0 && pos != 0) { | |
537 | retVal.append(' '); | |
538 | } | |
539 | } | |
540 | return retVal.toString(); | |
541 | } | |
542 | ||
543 | static String pad(long n) { | |
544 | String s; | |
545 | if (n < 10) { | |
546 | s = S00; | |
547 | } else if (n < 100) { | |
548 | s = S0; | |
549 | } else { | |
550 | s = S; | |
551 | } | |
552 | return s + n; | |
553 | } | |
554 | ||
555 | public abstract void draw(GC gc, long time, Rectangle rect); | |
556 | ||
557 | public void drawAbsHeader(GC gc, long time, Rectangle absHeaderRect) { | |
558 | // Override to draw absolute time header | |
559 | // This is for the time information not shown in the draw of each tick | |
560 | } | |
561 | ||
562 | public abstract String hint(); | |
563 | } | |
564 | ||
565 | class TimeDrawSec extends TimeDraw { | |
566 | static String _hint = "sec"; //$NON-NLS-1$ | |
567 | ||
568 | @Override | |
569 | public void draw(GC gc, long time, Rectangle rect) { | |
570 | time /= 1000000000; | |
83f4e378 | 571 | Utils.drawText(gc, sep(time), rect, true); |
8e1f81f1 PT |
572 | } |
573 | ||
574 | @Override | |
575 | public String hint() { | |
576 | return _hint; | |
577 | } | |
578 | } | |
579 | ||
580 | class TimeDrawMillisec extends TimeDraw { | |
581 | static String _hint = "0.000"; //$NON-NLS-1$ | |
582 | ||
583 | @Override | |
584 | public void draw(GC gc, long time, Rectangle rect) { | |
585 | time /= 1000000; | |
586 | long ms = time % 1000; | |
587 | time /= 1000; | |
588 | Utils.drawText(gc, sep(time) + "." + pad(ms), rect, true); //$NON-NLS-1$ | |
589 | } | |
590 | ||
591 | @Override | |
592 | public String hint() { | |
593 | return _hint; | |
594 | } | |
595 | } | |
596 | ||
597 | class TimeDrawMicrosec extends TimeDraw { | |
598 | static String _hint = "0.000 000"; //$NON-NLS-1$ | |
599 | ||
600 | @Override | |
601 | public void draw(GC gc, long time, Rectangle rect) { | |
602 | time /= 1000; | |
603 | long mcs = time % 1000; | |
604 | time /= 1000; | |
605 | long ms = time % 1000; | |
606 | time /= 1000; | |
607 | Utils.drawText(gc, sep(time) + "." + pad(ms) + " " + pad(mcs), rect, true); //$NON-NLS-1$ //$NON-NLS-2$ | |
608 | } | |
609 | ||
610 | @Override | |
611 | public String hint() { | |
612 | return _hint; | |
613 | } | |
614 | } | |
615 | ||
616 | class TimeDrawNanosec extends TimeDraw { | |
617 | static String _hint = "0.000 000 000"; //$NON-NLS-1$ | |
618 | ||
619 | @Override | |
620 | public void draw(GC gc, long time, Rectangle rect) { | |
621 | long ns = time % 1000; | |
622 | time /= 1000; | |
623 | long mcs = time % 1000; | |
624 | time /= 1000; | |
625 | long ms = time % 1000; | |
626 | time /= 1000; | |
627 | Utils.drawText(gc, sep(time) + "." + pad(ms) + " " + pad(mcs) + " " + pad(ns), rect, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
628 | } | |
629 | ||
630 | @Override | |
631 | public String hint() { | |
632 | return _hint; | |
633 | } | |
634 | } | |
635 | ||
636 | class TimeDrawAbsYear extends TimeDraw { | |
637 | static String _hint = "YYYY"; //$NON-NLS-1$ | |
638 | ||
639 | @Override | |
640 | public void draw(GC gc, long time, Rectangle rect) { | |
641 | String stime = syearformat.format(new Date(time / 1000000)); | |
642 | Utils.drawText(gc, stime, rect, true); | |
643 | } | |
644 | ||
645 | @Override | |
646 | public String hint() { | |
647 | return _hint; | |
648 | } | |
649 | } | |
650 | ||
651 | class TimeDrawAbsMonth extends TimeDraw { | |
652 | static String _hint = "YYYY Mmm"; //$NON-NLS-1$ | |
653 | ||
654 | @Override | |
655 | public void draw(GC gc, long time, Rectangle rect) { | |
656 | String stime = smonthformat.format(new Date(time / 1000000)); | |
657 | Utils.drawText(gc, stime, rect, true); | |
658 | } | |
659 | ||
660 | @Override | |
661 | public String hint() { | |
662 | return _hint; | |
663 | } | |
664 | } | |
665 | ||
666 | class TimeDrawAbsDay extends TimeDraw { | |
667 | static String _hint = "Mmm dd"; //$NON-NLS-1$ | |
668 | ||
669 | @Override | |
670 | public void draw(GC gc, long time, Rectangle rect) { | |
671 | String stime = sdayformat.format(new Date(time / 1000000)); | |
672 | Utils.drawText(gc, stime, rect, true); | |
673 | } | |
674 | ||
675 | @Override | |
676 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
677 | String header = sdayformatheader.format(new Date(time / 1000000)); | |
678 | int headerwidth = gc.stringExtent(header).x + 4; | |
679 | if (headerwidth <= rect.width) { | |
680 | rect.x += (rect.width - headerwidth); | |
681 | Utils.drawText(gc, header, rect, true); | |
682 | } | |
683 | } | |
684 | ||
685 | @Override | |
686 | public String hint() { | |
687 | return _hint; | |
688 | } | |
689 | } | |
690 | ||
691 | class TimeDrawAbsHrs extends TimeDraw { | |
692 | static String _hint = "Mmm dd HH:mm"; //$NON-NLS-1$ | |
693 | ||
694 | @Override | |
695 | public void draw(GC gc, long time, Rectangle rect) { | |
696 | String stime = shrsformat.format(new Date(time / 1000000)); | |
697 | Utils.drawText(gc, stime, rect, true); | |
698 | } | |
699 | ||
700 | @Override | |
701 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
702 | String header = shrsformatheader.format(new Date(time / 1000000)); | |
703 | int headerwidth = gc.stringExtent(header).x + 4; | |
704 | if (headerwidth <= rect.width) { | |
705 | rect.x += (rect.width - headerwidth); | |
706 | Utils.drawText(gc, header, rect, true); | |
707 | } | |
708 | } | |
709 | ||
710 | @Override | |
711 | public String hint() { | |
712 | return _hint; | |
713 | } | |
714 | } | |
715 | ||
716 | class TimeDrawAbsMin extends TimeDraw { | |
717 | static String _hint = "HH:mm"; //$NON-NLS-1$ | |
718 | ||
719 | @Override | |
720 | public void draw(GC gc, long time, Rectangle rect) { | |
721 | String stime = sminformat.format(new Date(time / 1000000)); | |
722 | Utils.drawText(gc, stime, rect, true); | |
723 | } | |
724 | ||
725 | @Override | |
726 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
727 | String header = sminformatheader.format(new Date(time / 1000000)); | |
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 | } | |
734 | ||
735 | ||
736 | @Override | |
737 | public String hint() { | |
738 | return _hint; | |
739 | } | |
740 | } | |
741 | ||
742 | class TimeDrawAbsSec extends TimeDraw { | |
743 | static String _hint = "HH:mm:ss"; //$NON-NLS-1$ | |
744 | ||
745 | @Override | |
746 | public void draw(GC gc, long time, Rectangle rect) { | |
747 | String stime = stimeformat.format(new Date(time / 1000000)); | |
748 | Utils.drawText(gc, stime, rect, true); | |
749 | } | |
750 | ||
751 | @Override | |
752 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
753 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
754 | int headerwidth = gc.stringExtent(header).x + 4; | |
755 | if (headerwidth <= rect.width) { | |
756 | rect.x += (rect.width - headerwidth); | |
757 | Utils.drawText(gc, header, rect, true); | |
758 | } | |
759 | } | |
760 | ||
761 | @Override | |
762 | public String hint() { | |
763 | return _hint; | |
764 | } | |
765 | } | |
766 | ||
767 | class TimeDrawAbsMillisec extends TimeDraw { | |
768 | static String _hint = "HH:ss:ms"; //$NON-NLS-1$ | |
769 | ||
770 | @Override | |
771 | public void draw(GC gc, long time, Rectangle rect) { | |
772 | String stime = stimeformat.format(new Date(time / 1000000)); | |
773 | String ns = Utils.formatNs(time, Resolution.MILLISEC); | |
774 | ||
775 | Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$ | |
776 | } | |
777 | ||
778 | @Override | |
779 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
780 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
781 | int headerwidth = gc.stringExtent(header).x + 4; | |
782 | if (headerwidth <= rect.width) { | |
783 | rect.x += (rect.width - headerwidth); | |
784 | Utils.drawText(gc, header, rect, true); | |
785 | } | |
786 | } | |
787 | ||
788 | @Override | |
789 | public String hint() { | |
790 | return _hint; | |
791 | } | |
792 | } | |
793 | ||
794 | class TimeDrawAbsMicroSec extends TimeDraw { | |
795 | static String _hint = "HH:ss:ms:mcs"; //$NON-NLS-1$ | |
796 | ||
797 | @Override | |
798 | public void draw(GC gc, long time, Rectangle rect) { | |
799 | String stime = stimeformat.format(new Date(time / 1000000)); | |
800 | String micr = Utils.formatNs(time, Resolution.MICROSEC); | |
801 | Utils.drawText(gc, stime + "." + micr, rect, true); //$NON-NLS-1$ | |
802 | } | |
803 | ||
804 | @Override | |
805 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
806 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
807 | int headerwidth = gc.stringExtent(header).x + 4; | |
808 | if (headerwidth <= rect.width) { | |
809 | rect.x += (rect.width - headerwidth); | |
810 | Utils.drawText(gc, header, rect, true); | |
811 | } | |
812 | } | |
813 | ||
814 | @Override | |
815 | public String hint() { | |
816 | return _hint; | |
817 | } | |
818 | } | |
819 | ||
820 | class TimeDrawAbsNanoSec extends TimeDraw { | |
821 | static String _hint = "HH:ss:ms:mcs:ns"; //$NON-NLS-1$ | |
822 | ||
823 | @Override | |
824 | public void draw(GC gc, long time, Rectangle rect) { | |
825 | String stime = stimeformat.format(new Date(time / 1000000)); | |
826 | String ns = Utils.formatNs(time, Resolution.NANOSEC); | |
827 | Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$ | |
828 | } | |
829 | ||
830 | @Override | |
831 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
832 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
833 | int headerwidth = gc.stringExtent(header).x + 4; | |
834 | if (headerwidth <= rect.width) { | |
835 | rect.x += (rect.width - headerwidth); | |
836 | Utils.drawText(gc, header, rect, true); | |
837 | } | |
838 | } | |
839 | ||
840 | @Override | |
841 | public String hint() { | |
842 | return _hint; | |
843 | } | |
844 | } |