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; | |
115 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
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; | |
302 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
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 | ||
313 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
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 | } | |
332 | if (_timeProvider != null && _timeProvider.isCalendarFormat()) { | |
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) { | |
361 | if (_timeDelta >= YEAR_IN_NS) { | |
362 | GREGORIAN_CALENDAR.setTime(new Date(time / 1000000)); | |
363 | int year = GREGORIAN_CALENDAR.get(Calendar.YEAR); | |
364 | int yearDelta = (int) (timeDelta / YEAR_IN_NS); | |
365 | year = (year / yearDelta) * yearDelta; | |
366 | GREGORIAN_CALENDAR.set(Calendar.YEAR, year); | |
367 | GREGORIAN_CALENDAR.set(Calendar.MONTH, 0); // January 1st of year | |
368 | GREGORIAN_CALENDAR.set(Calendar.DAY_OF_MONTH, 1); | |
369 | GREGORIAN_CALENDAR.set(Calendar.HOUR_OF_DAY, 0); | |
370 | GREGORIAN_CALENDAR.set(Calendar.MINUTE, 0); | |
371 | GREGORIAN_CALENDAR.set(Calendar.SECOND, 0); | |
372 | GREGORIAN_CALENDAR.set(Calendar.MILLISECOND, 0); | |
373 | time = GREGORIAN_CALENDAR.getTimeInMillis() * 1000000; | |
374 | } else if (_timeDelta >= MONTH_IN_NS) { | |
375 | GREGORIAN_CALENDAR.setTime(new Date(time / 1000000)); | |
376 | int month = GREGORIAN_CALENDAR.get(Calendar.MONTH); | |
377 | int monthDelta = (int) (timeDelta / MONTH_IN_NS); | |
378 | month = (month / monthDelta) * monthDelta; | |
379 | GREGORIAN_CALENDAR.set(Calendar.MONTH, month); | |
380 | GREGORIAN_CALENDAR.set(Calendar.DAY_OF_MONTH, 1); // 1st of month | |
381 | GREGORIAN_CALENDAR.set(Calendar.HOUR_OF_DAY, 0); | |
382 | GREGORIAN_CALENDAR.set(Calendar.MINUTE, 0); | |
383 | GREGORIAN_CALENDAR.set(Calendar.SECOND, 0); | |
384 | GREGORIAN_CALENDAR.set(Calendar.MILLISECOND, 0); | |
385 | time = GREGORIAN_CALENDAR.getTimeInMillis() * 1000000; | |
386 | } else { | |
387 | long offset = GREGORIAN_CALENDAR.getTimeZone().getOffset(time / 1000000L) * 1000000L; | |
388 | time += offset; | |
389 | time = (time / timeDelta) * timeDelta; | |
390 | time -= offset; | |
391 | } | |
392 | return time; | |
393 | } | |
394 | ||
395 | private int calculateDigits(long time0, long time1) { | |
396 | int numDigits = 5; | |
397 | long timeRange = time1 - time0; | |
398 | ||
399 | if (_timeProvider.isCalendarFormat()) { | |
400 | // Calculate the number of digits to represent the minutes provided | |
401 | // 11:222 | |
402 | // HH:mm:ss | |
403 | numDigits += 8; | |
404 | if (timeRange < 10000) { | |
405 | // HH:11:222:333:444__ | |
406 | numDigits += 10; | |
407 | } else if (timeRange < 10000000) { | |
408 | // HH:11:222:333__ | |
409 | numDigits += 6; | |
410 | } | |
411 | } else { | |
412 | long sec = time1 / 1000000000; | |
413 | numDigits = Long.toString(sec).length(); | |
414 | int thousandGroups = (numDigits - 1) / 3; | |
415 | numDigits += thousandGroups; | |
416 | numDigits += 12; // .000 000 000 | |
417 | } | |
418 | ||
419 | return numDigits; | |
420 | } | |
421 | ||
422 | @Override | |
423 | public void mouseDown(MouseEvent e) { | |
424 | getParent().setFocus(); | |
425 | if (_dragState == 0 && null != _timeProvider) { | |
426 | int x = e.x - _timeProvider.getNameSpace(); | |
427 | if (1 == e.button && x > 0) { | |
428 | setCapture(true); | |
429 | _dragState = 1; | |
430 | } else if (3 == e.button) { | |
431 | _dragState = 3; | |
432 | } | |
433 | if (x < 0) { | |
434 | x = 0; | |
435 | } else if (x > getSize().x - _timeProvider.getNameSpace()) { | |
436 | x = getSize().x - _timeProvider.getNameSpace(); | |
437 | } | |
438 | _dragX = _dragX0 = x; | |
439 | _time0bak = _timeProvider.getTime0(); | |
440 | _time1bak = _timeProvider.getTime1(); | |
441 | } | |
442 | } | |
443 | ||
444 | @Override | |
445 | public void mouseUp(MouseEvent e) { | |
446 | if (e.button == 1 && _dragState == 1) { | |
447 | setCapture(false); | |
448 | _dragState = 0; | |
449 | ||
450 | // Notify time provider to check the need for listener notification | |
451 | if (_dragX != _dragX0 && _timeProvider.getTime0() != _timeProvider.getTime1()) { | |
452 | _timeProvider.setStartFinishTimeNotify(_timeProvider.getTime0(), _timeProvider.getTime1()); | |
453 | } | |
454 | } else if (e.button == 3 && _dragState == 3 && null != _timeProvider) { | |
455 | _dragState = 0; | |
456 | if (_dragX0 == _dragX || _timeProvider.getTime0() == _timeProvider.getTime1()) { | |
5b2b9bd7 | 457 | redraw(); |
8e1f81f1 PT |
458 | return; |
459 | } | |
460 | int timeSpace = _timeProvider.getTimeSpace(); | |
461 | int leftSpace = _timeProvider.getNameSpace(); | |
462 | int x = Math.max(0, e.x - leftSpace); | |
463 | if (timeSpace > 0) { | |
464 | _dragX = x; | |
465 | if (_dragX0 > _dragX) { // drag right to left | |
466 | _dragX = _dragX0; | |
467 | _dragX0 = x; | |
468 | } | |
469 | long time0 = _time0bak + (long) ((_time1bak - _time0bak) * ((double) _dragX0 / timeSpace)); | |
470 | long time1 = _time0bak + (long) ((_time1bak - _time0bak) * ((double) _dragX / timeSpace)); | |
471 | ||
472 | _timeProvider.setStartFinishTimeNotify(time0, time1); | |
473 | _time0bak = _timeProvider.getTime0(); | |
474 | _time1bak = _timeProvider.getTime1(); | |
475 | } | |
476 | } | |
477 | } | |
478 | ||
479 | @Override | |
480 | public void mouseMove(MouseEvent e) { | |
481 | if (_dragX0 < 0 || _dragState == 0 || _timeProvider == null) { | |
482 | return; | |
483 | } | |
484 | Point size = getSize(); | |
485 | int leftSpace = _timeProvider.getNameSpace(); | |
486 | int timeSpace = _timeProvider.getTimeSpace(); | |
487 | int x = e.x - leftSpace; | |
488 | if (1 == _dragState) { | |
489 | if (x > 0 && size.x > leftSpace && _dragX != x) { | |
490 | _dragX = x; | |
491 | if (_timeProvider.getTime0() == _timeProvider.getTime1()) { | |
492 | return; | |
493 | } | |
494 | long interval = (long) ((_time1bak - _time0bak) * ((double) _dragX0 / _dragX)); | |
495 | if (interval == Long.MAX_VALUE) { | |
496 | _timeProvider.setStartFinishTime(_time0bak, Long.MAX_VALUE); | |
497 | } else { | |
498 | long time1 = _time0bak + (long) ((_time1bak - _time0bak) * ((double) _dragX0 / _dragX)); | |
499 | _timeProvider.setStartFinishTime(_time0bak, time1); | |
500 | } | |
501 | } | |
502 | } else if (3 == _dragState) { | |
503 | if (x < 0) { | |
504 | _dragX = 0; | |
505 | } else if (x > timeSpace) { | |
506 | _dragX = timeSpace; | |
507 | } else { | |
508 | _dragX = x; | |
509 | } | |
510 | redraw(); | |
511 | } | |
512 | } | |
513 | ||
514 | @Override | |
515 | public void mouseDoubleClick(MouseEvent e) { | |
5b2b9bd7 | 516 | if (e.button == 1 && null != _timeProvider && _timeProvider.getTime0() != _timeProvider.getTime1() && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
8e1f81f1 | 517 | _timeProvider.resetStartFinishTime(); |
13ccc36b | 518 | _timeProvider.notifyStartFinishTime(); |
8e1f81f1 PT |
519 | _time0bak = _timeProvider.getTime0(); |
520 | _time1bak = _timeProvider.getTime1(); | |
521 | } | |
522 | } | |
523 | } | |
524 | ||
525 | abstract class TimeDraw { | |
526 | static String S = "" ; //$NON-NLS-1$ | |
527 | static String S0 = "0" ; //$NON-NLS-1$ | |
528 | static String S00 = "00"; //$NON-NLS-1$ | |
529 | protected static final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$ | |
530 | protected static final SimpleDateFormat stimeformatheader = new SimpleDateFormat("yyyy MMM dd"); //$NON-NLS-1$ | |
531 | protected static final SimpleDateFormat sminformat = new SimpleDateFormat("HH:mm"); //$NON-NLS-1$ | |
532 | protected static final SimpleDateFormat sminformatheader = new SimpleDateFormat("yyyy MMM dd"); //$NON-NLS-1$ | |
533 | protected static final SimpleDateFormat shrsformat = new SimpleDateFormat("MMM dd HH:mm"); //$NON-NLS-1$ | |
534 | protected static final SimpleDateFormat shrsformatheader = new SimpleDateFormat("yyyy"); //$NON-NLS-1$ | |
535 | protected static final SimpleDateFormat sdayformat = new SimpleDateFormat("MMM dd"); //$NON-NLS-1$ | |
536 | protected static final SimpleDateFormat sdayformatheader = new SimpleDateFormat("yyyy"); //$NON-NLS-1$ | |
537 | protected static final SimpleDateFormat smonthformat = new SimpleDateFormat("yyyy MMM"); //$NON-NLS-1$ | |
538 | protected static final SimpleDateFormat syearformat = new SimpleDateFormat("yyyy"); //$NON-NLS-1$ | |
539 | ||
540 | static String sep(long n) { | |
541 | StringBuilder retVal = new StringBuilder(); | |
542 | String s = Long.toString(n); | |
543 | for (int i = 0; i < s.length(); i++) { | |
544 | int pos = s.length() - i - 1; | |
545 | retVal.append(s.charAt(i)); | |
546 | if (pos % 3 == 0 && pos != 0) { | |
547 | retVal.append(' '); | |
548 | } | |
549 | } | |
550 | return retVal.toString(); | |
551 | } | |
552 | ||
553 | static String pad(long n) { | |
554 | String s; | |
555 | if (n < 10) { | |
556 | s = S00; | |
557 | } else if (n < 100) { | |
558 | s = S0; | |
559 | } else { | |
560 | s = S; | |
561 | } | |
562 | return s + n; | |
563 | } | |
564 | ||
565 | public abstract void draw(GC gc, long time, Rectangle rect); | |
566 | ||
a0a88f65 AM |
567 | /** |
568 | * Override to draw absolute time header This is for the time information | |
569 | * not shown in the draw of each tick | |
570 | * | |
571 | * @param gc | |
572 | * Graphics context | |
573 | * @param time | |
574 | * Timestamp | |
575 | * @param absHeaderRect | |
576 | * Header rectangle | |
577 | */ | |
8e1f81f1 | 578 | public void drawAbsHeader(GC gc, long time, Rectangle absHeaderRect) { |
8e1f81f1 PT |
579 | } |
580 | ||
581 | public abstract String hint(); | |
582 | } | |
583 | ||
584 | class TimeDrawSec extends TimeDraw { | |
585 | static String _hint = "sec"; //$NON-NLS-1$ | |
586 | ||
587 | @Override | |
588 | public void draw(GC gc, long time, Rectangle rect) { | |
589 | time /= 1000000000; | |
83f4e378 | 590 | Utils.drawText(gc, sep(time), rect, true); |
8e1f81f1 PT |
591 | } |
592 | ||
593 | @Override | |
594 | public String hint() { | |
595 | return _hint; | |
596 | } | |
597 | } | |
598 | ||
599 | class TimeDrawMillisec extends TimeDraw { | |
600 | static String _hint = "0.000"; //$NON-NLS-1$ | |
601 | ||
602 | @Override | |
603 | public void draw(GC gc, long time, Rectangle rect) { | |
604 | time /= 1000000; | |
605 | long ms = time % 1000; | |
606 | time /= 1000; | |
607 | Utils.drawText(gc, sep(time) + "." + pad(ms), rect, true); //$NON-NLS-1$ | |
608 | } | |
609 | ||
610 | @Override | |
611 | public String hint() { | |
612 | return _hint; | |
613 | } | |
614 | } | |
615 | ||
616 | class TimeDrawMicrosec extends TimeDraw { | |
617 | static String _hint = "0.000 000"; //$NON-NLS-1$ | |
618 | ||
619 | @Override | |
620 | public void draw(GC gc, long time, Rectangle rect) { | |
621 | time /= 1000; | |
622 | long mcs = time % 1000; | |
623 | time /= 1000; | |
624 | long ms = time % 1000; | |
625 | time /= 1000; | |
626 | Utils.drawText(gc, sep(time) + "." + pad(ms) + " " + pad(mcs), rect, true); //$NON-NLS-1$ //$NON-NLS-2$ | |
627 | } | |
628 | ||
629 | @Override | |
630 | public String hint() { | |
631 | return _hint; | |
632 | } | |
633 | } | |
634 | ||
635 | class TimeDrawNanosec extends TimeDraw { | |
636 | static String _hint = "0.000 000 000"; //$NON-NLS-1$ | |
637 | ||
638 | @Override | |
639 | public void draw(GC gc, long time, Rectangle rect) { | |
640 | long ns = time % 1000; | |
641 | time /= 1000; | |
642 | long mcs = time % 1000; | |
643 | time /= 1000; | |
644 | long ms = time % 1000; | |
645 | time /= 1000; | |
646 | Utils.drawText(gc, sep(time) + "." + pad(ms) + " " + pad(mcs) + " " + pad(ns), rect, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
647 | } | |
648 | ||
649 | @Override | |
650 | public String hint() { | |
651 | return _hint; | |
652 | } | |
653 | } | |
654 | ||
655 | class TimeDrawAbsYear extends TimeDraw { | |
656 | static String _hint = "YYYY"; //$NON-NLS-1$ | |
657 | ||
658 | @Override | |
659 | public void draw(GC gc, long time, Rectangle rect) { | |
660 | String stime = syearformat.format(new Date(time / 1000000)); | |
661 | Utils.drawText(gc, stime, rect, true); | |
662 | } | |
663 | ||
664 | @Override | |
665 | public String hint() { | |
666 | return _hint; | |
667 | } | |
668 | } | |
669 | ||
670 | class TimeDrawAbsMonth extends TimeDraw { | |
671 | static String _hint = "YYYY Mmm"; //$NON-NLS-1$ | |
672 | ||
673 | @Override | |
674 | public void draw(GC gc, long time, Rectangle rect) { | |
675 | String stime = smonthformat.format(new Date(time / 1000000)); | |
676 | Utils.drawText(gc, stime, rect, true); | |
677 | } | |
678 | ||
679 | @Override | |
680 | public String hint() { | |
681 | return _hint; | |
682 | } | |
683 | } | |
684 | ||
685 | class TimeDrawAbsDay extends TimeDraw { | |
686 | static String _hint = "Mmm dd"; //$NON-NLS-1$ | |
687 | ||
688 | @Override | |
689 | public void draw(GC gc, long time, Rectangle rect) { | |
690 | String stime = sdayformat.format(new Date(time / 1000000)); | |
691 | Utils.drawText(gc, stime, rect, true); | |
692 | } | |
693 | ||
694 | @Override | |
695 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
696 | String header = sdayformatheader.format(new Date(time / 1000000)); | |
697 | int headerwidth = gc.stringExtent(header).x + 4; | |
698 | if (headerwidth <= rect.width) { | |
699 | rect.x += (rect.width - headerwidth); | |
700 | Utils.drawText(gc, header, rect, true); | |
701 | } | |
702 | } | |
703 | ||
704 | @Override | |
705 | public String hint() { | |
706 | return _hint; | |
707 | } | |
708 | } | |
709 | ||
710 | class TimeDrawAbsHrs extends TimeDraw { | |
711 | static String _hint = "Mmm dd HH:mm"; //$NON-NLS-1$ | |
712 | ||
713 | @Override | |
714 | public void draw(GC gc, long time, Rectangle rect) { | |
715 | String stime = shrsformat.format(new Date(time / 1000000)); | |
716 | Utils.drawText(gc, stime, rect, true); | |
717 | } | |
718 | ||
719 | @Override | |
720 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
721 | String header = shrsformatheader.format(new Date(time / 1000000)); | |
722 | int headerwidth = gc.stringExtent(header).x + 4; | |
723 | if (headerwidth <= rect.width) { | |
724 | rect.x += (rect.width - headerwidth); | |
725 | Utils.drawText(gc, header, rect, true); | |
726 | } | |
727 | } | |
728 | ||
729 | @Override | |
730 | public String hint() { | |
731 | return _hint; | |
732 | } | |
733 | } | |
734 | ||
735 | class TimeDrawAbsMin extends TimeDraw { | |
736 | static String _hint = "HH:mm"; //$NON-NLS-1$ | |
737 | ||
738 | @Override | |
739 | public void draw(GC gc, long time, Rectangle rect) { | |
740 | String stime = sminformat.format(new Date(time / 1000000)); | |
741 | Utils.drawText(gc, stime, rect, true); | |
742 | } | |
743 | ||
744 | @Override | |
745 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
746 | String header = sminformatheader.format(new Date(time / 1000000)); | |
747 | int headerwidth = gc.stringExtent(header).x + 4; | |
748 | if (headerwidth <= rect.width) { | |
749 | rect.x += (rect.width - headerwidth); | |
750 | Utils.drawText(gc, header, rect, true); | |
751 | } | |
752 | } | |
753 | ||
754 | ||
755 | @Override | |
756 | public String hint() { | |
757 | return _hint; | |
758 | } | |
759 | } | |
760 | ||
761 | class TimeDrawAbsSec extends TimeDraw { | |
762 | static String _hint = "HH:mm:ss"; //$NON-NLS-1$ | |
763 | ||
764 | @Override | |
765 | public void draw(GC gc, long time, Rectangle rect) { | |
766 | String stime = stimeformat.format(new Date(time / 1000000)); | |
767 | Utils.drawText(gc, stime, rect, true); | |
768 | } | |
769 | ||
770 | @Override | |
771 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
772 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
773 | int headerwidth = gc.stringExtent(header).x + 4; | |
774 | if (headerwidth <= rect.width) { | |
775 | rect.x += (rect.width - headerwidth); | |
776 | Utils.drawText(gc, header, rect, true); | |
777 | } | |
778 | } | |
779 | ||
780 | @Override | |
781 | public String hint() { | |
782 | return _hint; | |
783 | } | |
784 | } | |
785 | ||
786 | class TimeDrawAbsMillisec extends TimeDraw { | |
787 | static String _hint = "HH:ss:ms"; //$NON-NLS-1$ | |
788 | ||
789 | @Override | |
790 | public void draw(GC gc, long time, Rectangle rect) { | |
791 | String stime = stimeformat.format(new Date(time / 1000000)); | |
792 | String ns = Utils.formatNs(time, Resolution.MILLISEC); | |
793 | ||
794 | Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$ | |
795 | } | |
796 | ||
797 | @Override | |
798 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
799 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
800 | int headerwidth = gc.stringExtent(header).x + 4; | |
801 | if (headerwidth <= rect.width) { | |
802 | rect.x += (rect.width - headerwidth); | |
803 | Utils.drawText(gc, header, rect, true); | |
804 | } | |
805 | } | |
806 | ||
807 | @Override | |
808 | public String hint() { | |
809 | return _hint; | |
810 | } | |
811 | } | |
812 | ||
813 | class TimeDrawAbsMicroSec extends TimeDraw { | |
814 | static String _hint = "HH:ss:ms:mcs"; //$NON-NLS-1$ | |
815 | ||
816 | @Override | |
817 | public void draw(GC gc, long time, Rectangle rect) { | |
818 | String stime = stimeformat.format(new Date(time / 1000000)); | |
819 | String micr = Utils.formatNs(time, Resolution.MICROSEC); | |
820 | Utils.drawText(gc, stime + "." + micr, rect, true); //$NON-NLS-1$ | |
821 | } | |
822 | ||
823 | @Override | |
824 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
825 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
826 | int headerwidth = gc.stringExtent(header).x + 4; | |
827 | if (headerwidth <= rect.width) { | |
828 | rect.x += (rect.width - headerwidth); | |
829 | Utils.drawText(gc, header, rect, true); | |
830 | } | |
831 | } | |
832 | ||
833 | @Override | |
834 | public String hint() { | |
835 | return _hint; | |
836 | } | |
837 | } | |
838 | ||
839 | class TimeDrawAbsNanoSec extends TimeDraw { | |
840 | static String _hint = "HH:ss:ms:mcs:ns"; //$NON-NLS-1$ | |
841 | ||
842 | @Override | |
843 | public void draw(GC gc, long time, Rectangle rect) { | |
844 | String stime = stimeformat.format(new Date(time / 1000000)); | |
845 | String ns = Utils.formatNs(time, Resolution.NANOSEC); | |
846 | Utils.drawText(gc, stime + "." + ns, rect, true); //$NON-NLS-1$ | |
847 | } | |
848 | ||
849 | @Override | |
850 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
851 | String header = stimeformatheader.format(new Date(time / 1000000)); | |
852 | int headerwidth = gc.stringExtent(header).x + 4; | |
853 | if (headerwidth <= rect.width) { | |
854 | rect.x += (rect.width - headerwidth); | |
855 | Utils.drawText(gc, header, rect, true); | |
856 | } | |
857 | } | |
858 | ||
859 | @Override | |
860 | public String hint() { | |
861 | return _hint; | |
862 | } | |
863 | } | |
026664b7 XR |
864 | |
865 | ||
866 | class TimeDrawNumber extends TimeDraw { | |
867 | ||
868 | @Override | |
869 | public void draw(GC gc, long time, Rectangle rect) { | |
870 | String stime = NumberFormat.getInstance().format(time); | |
871 | Utils.drawText(gc, stime, rect, true); | |
872 | } | |
873 | ||
874 | @Override | |
875 | public void drawAbsHeader(GC gc, long time, Rectangle rect) { | |
876 | String header = NumberFormat.getInstance().format(time); | |
877 | int headerwidth = gc.stringExtent(header).x + 4; | |
878 | if (headerwidth <= rect.width) { | |
879 | rect.x += (rect.width - headerwidth); | |
880 | Utils.drawText(gc, header, rect, true); | |
881 | } | |
882 | } | |
883 | ||
884 | @Override | |
885 | public String hint() { | |
886 | return "cycle"; //$NON-NLS-1$ | |
887 | } | |
888 | } |