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 // ------------------------------------------------------------------------
163 public synchronized void mouseScrolled(MouseEvent event
) {
164 if (fScrollCounter
== null) {
165 fScrollCounter
= new MouseScrollCounter(this);
166 fScrollCounter
.start();
168 fScrollCounter
.incrementMouseScroll(event
.count
);
171 private synchronized void zoom(int nbClicks
) {
172 // The job is finished
173 fScrollCounter
= null;
175 // Compute the new time range
176 long requestedRange
= (nbClicks
> 0) ? Math
.round(ZOOM_FACTOR
* fRangeDuration
) : (long) Math
.ceil(fRangeDuration
* (1.0 / ZOOM_FACTOR
));
178 // Distribute delta and adjust for boundaries
179 long requestedStart
= validateStart(fRangeStartTime
+ (fRangeDuration
- requestedRange
) / 2);
180 long requestedEnd
= validateEnd(requestedStart
, requestedStart
+ requestedRange
);
181 requestedStart
= validateStart(requestedEnd
- requestedRange
);
183 fHistogram
.updateTimeRange(requestedStart
, requestedEnd
);
186 private long validateStart(long start
) {
187 if (start
< fAbsoluteStartTime
) {
188 start
= fAbsoluteStartTime
;
190 if (start
> fAbsoluteEndTime
) {
191 start
= fAbsoluteEndTime
- fMinWindowSize
;
196 private long validateEnd(long start
, long end
) {
197 if (end
> fAbsoluteEndTime
) {
198 end
= fAbsoluteEndTime
;
200 if (end
< start
+ fMinWindowSize
) {
201 end
= start
+ fMinWindowSize
;
206 // ------------------------------------------------------------------------
207 // DelayedMouseScroll
208 // ------------------------------------------------------------------------
210 private static class MouseScrollCounter
extends Thread
{
212 // --------------------------------------------------------------------
214 // --------------------------------------------------------------------
216 private final static long QUIET_TIME
= 100L;
217 private final static long POLLING_INTERVAL
= 10L;
219 // --------------------------------------------------------------------
221 // --------------------------------------------------------------------
223 private HistogramZoom fZoom
= null;
225 private long fLastPoolTime
= 0L;
226 private int nbScrollClick
= 0;
228 // --------------------------------------------------------------------
230 // --------------------------------------------------------------------
233 * Constructor of inner class to handle consecutive scrolls of mouse wheel.
234 * @param zoom the histogram zoom reference
236 public MouseScrollCounter(HistogramZoom zoom
) {
238 fLastPoolTime
= System
.currentTimeMillis();
241 // --------------------------------------------------------------------
243 // --------------------------------------------------------------------
246 * Increments the number of scroll clicks.
247 * @param nbScrolls the number to add to the current value
249 public void incrementMouseScroll(int nbScrolls
) {
250 fLastPoolTime
= System
.currentTimeMillis();
251 nbScrollClick
+= nbScrolls
;
254 // --------------------------------------------------------------------
256 // --------------------------------------------------------------------
260 while ((System
.currentTimeMillis() - fLastPoolTime
) < QUIET_TIME
) {
262 Thread
.sleep(POLLING_INTERVAL
);
263 } catch (Exception e
) {
267 // Done waiting. Notify the histogram.
268 if (!isInterrupted()) {
269 fZoom
.zoom(nbScrollClick
);