1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Moved from LTTng to TMF
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
16 import org
.eclipse
.swt
.events
.MouseEvent
;
17 import org
.eclipse
.swt
.events
.MouseWheelListener
;
18 import org
.eclipse
.swt
.widgets
.Canvas
;
21 * Class to handle zooming within histogram windows..
24 * @author Francois Chouinard
27 public class HistogramZoom
implements MouseWheelListener
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 private final static double ZOOM_FACTOR
= 0.8;
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final Histogram fHistogram
;
40 private final Canvas fCanvas
;
42 private long fAbsoluteStartTime
;
43 private long fAbsoluteEndTime
;
44 private final long fMinWindowSize
;
46 private long fRangeStartTime
;
47 private long fRangeDuration
;
49 private MouseScrollCounter fScrollCounter
;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Standard constructor.
59 * The parent histogram object
63 * The start time of the zoom area
65 * The end time of the zoom area
67 public HistogramZoom(Histogram histogram
, Canvas canvas
, long start
,
69 fHistogram
= histogram
;
71 fAbsoluteStartTime
= start
;
72 fAbsoluteEndTime
= end
;
73 fMinWindowSize
= fCanvas
.getBounds().x
;
75 fRangeStartTime
= fAbsoluteStartTime
;
76 fRangeDuration
= fAbsoluteStartTime
+ fMinWindowSize
;
78 canvas
.addMouseWheelListener(this);
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 * Get start time of the zoom window.
87 * @return the start time.
89 public synchronized long getStartTime() {
90 return fRangeStartTime
;
94 * Get the end time of the zoom window.
95 * @return the end time
97 public synchronized long getEndTime() {
98 return fRangeStartTime
+ fRangeDuration
;
102 * Get the duration of the zoom window.
103 * @return the duration of the zoom window.
105 public synchronized long getDuration() {
106 return fRangeDuration
;
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
114 * Stops the zooming (multiple consecutive execution)
116 public synchronized void stop() {
117 if (fScrollCounter
!= null) {
118 fScrollCounter
.interrupt();
119 fScrollCounter
= null;
124 * The the full time range of the histogram
126 * @param startTime the start time the histogram
127 * @param endTime the end time of the histogram
129 public synchronized void setFullRange(long startTime
, long endTime
) {
130 fAbsoluteStartTime
= startTime
;
131 fAbsoluteEndTime
= endTime
;
135 * Sets the new zoom window
136 * @param startTime the start time
137 * @param duration the duration
139 public synchronized void setNewRange(long startTime
, long duration
) {
140 if (startTime
< fAbsoluteStartTime
) {
141 startTime
= fAbsoluteStartTime
;
144 long endTime
= startTime
+ duration
;
145 if (endTime
> fAbsoluteEndTime
) {
146 endTime
= fAbsoluteEndTime
;
147 if (endTime
- duration
> fAbsoluteStartTime
) {
148 startTime
= endTime
- duration
;
150 startTime
= fAbsoluteStartTime
;
154 fRangeStartTime
= startTime
;
155 fRangeDuration
= endTime
- startTime
;
158 // ------------------------------------------------------------------------
159 // MouseWheelListener
160 // ------------------------------------------------------------------------
162 private long fMouseTimestamp
= 0;
165 public synchronized void mouseScrolled(MouseEvent event
) {
166 if (fScrollCounter
== null) {
167 fScrollCounter
= new MouseScrollCounter(this);
168 fScrollCounter
.start();
169 fMouseTimestamp
= fHistogram
.getTimestamp(event
.x
);
171 fScrollCounter
.incrementMouseScroll(event
.count
);
174 private synchronized void zoom(int nbClicks
) {
175 // The job is finished
176 fScrollCounter
= null;
178 // Compute the new time range
179 long requestedRange
= (nbClicks
> 0) ? Math
.round(ZOOM_FACTOR
* fRangeDuration
) : (long) Math
.ceil(fRangeDuration
* (1.0 / ZOOM_FACTOR
));
181 // Perform a proportional zoom wrt the mouse position
182 double ratio
= ((double) (fMouseTimestamp
- fRangeStartTime
)) / fRangeDuration
;
183 long requestedStart
= validateStart(fRangeStartTime
+ (long) ((fRangeDuration
- requestedRange
) * ratio
));
184 long requestedEnd
= validateEnd(requestedStart
, requestedStart
+ requestedRange
);
185 requestedStart
= validateStart(requestedEnd
- requestedRange
);
187 fHistogram
.updateTimeRange(requestedStart
, requestedEnd
);
190 private long validateStart(long start
) {
191 if (start
< fAbsoluteStartTime
) {
192 start
= fAbsoluteStartTime
;
194 if (start
> fAbsoluteEndTime
) {
195 start
= fAbsoluteEndTime
- fMinWindowSize
;
200 private long validateEnd(long start
, long end
) {
201 if (end
> fAbsoluteEndTime
) {
202 end
= fAbsoluteEndTime
;
204 if (end
< start
+ fMinWindowSize
) {
205 end
= start
+ fMinWindowSize
;
210 // ------------------------------------------------------------------------
211 // DelayedMouseScroll
212 // ------------------------------------------------------------------------
214 private static class MouseScrollCounter
extends Thread
{
216 // --------------------------------------------------------------------
218 // --------------------------------------------------------------------
220 private final static long QUIET_TIME
= 100L;
221 private final static long POLLING_INTERVAL
= 10L;
223 // --------------------------------------------------------------------
225 // --------------------------------------------------------------------
227 private HistogramZoom fZoom
= null;
229 private long fLastPoolTime
= 0L;
230 private int nbScrollClick
= 0;
232 // --------------------------------------------------------------------
234 // --------------------------------------------------------------------
237 * Constructor of inner class to handle consecutive scrolls of mouse wheel.
238 * @param zoom the histogram zoom reference
240 public MouseScrollCounter(HistogramZoom zoom
) {
242 fLastPoolTime
= System
.currentTimeMillis();
245 // --------------------------------------------------------------------
247 // --------------------------------------------------------------------
250 * Increments the number of scroll clicks.
251 * @param nbScrolls the number to add to the current value
253 public void incrementMouseScroll(int nbScrolls
) {
254 fLastPoolTime
= System
.currentTimeMillis();
255 nbScrollClick
+= nbScrolls
;
258 // --------------------------------------------------------------------
260 // --------------------------------------------------------------------
264 while ((System
.currentTimeMillis() - fLastPoolTime
) < QUIET_TIME
) {
266 Thread
.sleep(POLLING_INTERVAL
);
267 } catch (Exception e
) {
271 // Done waiting. Notify the histogram.
272 if (!isInterrupted()) {
273 fZoom
.zoom(nbScrollClick
);