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