Merge branch 'lttng-kepler'
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramZoom.java
CommitLineData
c392540b 1/*******************************************************************************
e0752744 2 * Copyright (c) 2011, 2012 Ericsson
20ff3b75 3 *
c392540b
FC
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
20ff3b75 8 *
c392540b
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
e0752744 11 * Francois Chouinard - Moved from LTTng to TMF
c392540b
FC
12 *******************************************************************************/
13
e0752744 14package org.eclipse.linuxtools.tmf.ui.views.histogram;
c392540b
FC
15
16import org.eclipse.swt.events.MouseEvent;
17import org.eclipse.swt.events.MouseWheelListener;
18import org.eclipse.swt.widgets.Canvas;
19
20/**
b544077e 21 * Class to handle zooming within histogram windows..
20ff3b75 22 *
b544077e
BH
23 * @version 1.0
24 * @author Francois Chouinard
c392540b 25 * <p>
c392540b
FC
26 */
27public class HistogramZoom implements MouseWheelListener {
28
29 // ------------------------------------------------------------------------
30 // Constants
31 // ------------------------------------------------------------------------
32
33 private final static double ZOOM_FACTOR = 0.8;
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 private final Histogram fHistogram;
40 private final Canvas fCanvas;
41
42 private long fAbsoluteStartTime;
43 private long fAbsoluteEndTime;
44 private final long fMinWindowSize;
45
46 private long fRangeStartTime;
47 private long fRangeDuration;
48
49 private MouseScrollCounter fScrollCounter;
50
51 // ------------------------------------------------------------------------
e0752744 52 // Constructors
c392540b
FC
53 // ------------------------------------------------------------------------
54
20ff3b75
AM
55 /**
56 * Standard constructor.
57 *
58 * @param histogram
59 * The parent histogram object
60 * @param canvas
61 * The canvas
62 * @param start
63 * The start time of the zoom area
64 * @param end
65 * The end time of the zoom area
66 */
67 public HistogramZoom(Histogram histogram, Canvas canvas, long start,
68 long end) {
c392540b
FC
69 fHistogram = histogram;
70 fCanvas = canvas;
71 fAbsoluteStartTime = start;
72 fAbsoluteEndTime = end;
73 fMinWindowSize = fCanvas.getBounds().x;
74
75 fRangeStartTime = fAbsoluteStartTime;
76 fRangeDuration = fAbsoluteStartTime + fMinWindowSize;
77
78 canvas.addMouseWheelListener(this);
79 }
80
81 // ------------------------------------------------------------------------
82 // Accessors
83 // ------------------------------------------------------------------------
84
b544077e
BH
85 /**
86 * Get start time of the zoom window.
87 * @return the start time.
88 */
5a5c2fc7 89 public synchronized long getStartTime() {
c392540b
FC
90 return fRangeStartTime;
91 }
92
b544077e
BH
93 /**
94 * Get the end time of the zoom window.
95 * @return the end time
96 */
5a5c2fc7 97 public synchronized long getEndTime() {
c392540b
FC
98 return fRangeStartTime + fRangeDuration;
99 }
100
b544077e
BH
101 /**
102 * Get the duration of the zoom window.
103 * @return the duration of the zoom window.
104 */
5a5c2fc7 105 public synchronized long getDuration() {
c392540b
FC
106 return fRangeDuration;
107 }
108
109 // ------------------------------------------------------------------------
110 // Operations
111 // ------------------------------------------------------------------------
112
b544077e
BH
113 /**
114 * Stops the zooming (multiple consecutive execution)
115 */
c392540b
FC
116 public synchronized void stop() {
117 if (fScrollCounter != null) {
118 fScrollCounter.interrupt();
119 fScrollCounter = null;
120 }
121 }
122
b544077e
BH
123 /**
124 * The the full time range of the histogram
20ff3b75 125 *
b544077e
BH
126 * @param startTime the start time the histogram
127 * @param endTime the end time of the histogram
128 */
8edafa7f 129 public synchronized void setFullRange(long startTime, long endTime) {
c392540b
FC
130 fAbsoluteStartTime = startTime;
131 fAbsoluteEndTime = endTime;
132 }
133
b544077e
BH
134 /**
135 * Sets the new zoom window
136 * @param startTime the start time
137 * @param duration the duration
138 */
8edafa7f 139 public synchronized void setNewRange(long startTime, long duration) {
20ff3b75 140 if (startTime < fAbsoluteStartTime) {
c392540b 141 startTime = fAbsoluteStartTime;
20ff3b75 142 }
c392540b
FC
143
144 long endTime = startTime + duration;
145 if (endTime > fAbsoluteEndTime) {
146 endTime = fAbsoluteEndTime;
20ff3b75 147 if (endTime - duration > fAbsoluteStartTime) {
c392540b 148 startTime = endTime - duration;
20ff3b75 149 } else {
c392540b
FC
150 startTime = fAbsoluteStartTime;
151 }
152 }
153
154 fRangeStartTime = startTime;
155 fRangeDuration = endTime - startTime;
156 }
157
158 // ------------------------------------------------------------------------
159 // MouseWheelListener
160 // ------------------------------------------------------------------------
161
c392540b
FC
162 @Override
163 public synchronized void mouseScrolled(MouseEvent event) {
164 if (fScrollCounter == null) {
165 fScrollCounter = new MouseScrollCounter(this);
166 fScrollCounter.start();
c392540b
FC
167 }
168 fScrollCounter.incrementMouseScroll(event.count);
169 }
170
171 private synchronized void zoom(int nbClicks) {
172 // The job is finished
173 fScrollCounter = null;
174
175 // Compute the new time range
176 long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR));
177
bd6307ff 178 // Distribute delta and adjust for boundaries
83f4e378 179 long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2);
c392540b
FC
180 long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange);
181 requestedStart = validateStart(requestedEnd - requestedRange);
182
183 fHistogram.updateTimeRange(requestedStart, requestedEnd);
184 }
185
186 private long validateStart(long start) {
20ff3b75 187 if (start < fAbsoluteStartTime) {
c392540b 188 start = fAbsoluteStartTime;
20ff3b75
AM
189 }
190 if (start > fAbsoluteEndTime) {
c392540b 191 start = fAbsoluteEndTime - fMinWindowSize;
20ff3b75 192 }
c392540b
FC
193 return start;
194 }
195
196 private long validateEnd(long start, long end) {
20ff3b75 197 if (end > fAbsoluteEndTime) {
c392540b 198 end = fAbsoluteEndTime;
20ff3b75
AM
199 }
200 if (end < start + fMinWindowSize) {
c392540b 201 end = start + fMinWindowSize;
20ff3b75 202 }
c392540b
FC
203 return end;
204 }
205
206 // ------------------------------------------------------------------------
207 // DelayedMouseScroll
208 // ------------------------------------------------------------------------
209
5a5c2fc7 210 private static class MouseScrollCounter extends Thread {
c392540b
FC
211
212 // --------------------------------------------------------------------
213 // Constants
214 // --------------------------------------------------------------------
215
216 private final static long QUIET_TIME = 100L;
217 private final static long POLLING_INTERVAL = 10L;
218
219 // --------------------------------------------------------------------
220 // Attributes
221 // --------------------------------------------------------------------
222
223 private HistogramZoom fZoom = null;
224
225 private long fLastPoolTime = 0L;
226 private int nbScrollClick = 0;
227
228 // --------------------------------------------------------------------
e0752744 229 // Constructors
c392540b
FC
230 // --------------------------------------------------------------------
231
20ff3b75 232 /**
b544077e
BH
233 * Constructor of inner class to handle consecutive scrolls of mouse wheel.
234 * @param zoom the histogram zoom reference
235 */
c392540b
FC
236 public MouseScrollCounter(HistogramZoom zoom) {
237 fZoom = zoom;
238 fLastPoolTime = System.currentTimeMillis();
239 }
240
241 // --------------------------------------------------------------------
242 // Operation
243 // --------------------------------------------------------------------
20ff3b75 244
b544077e
BH
245 /**
246 * Increments the number of scroll clicks.
247 * @param nbScrolls the number to add to the current value
248 */
c392540b
FC
249 public void incrementMouseScroll(int nbScrolls) {
250 fLastPoolTime = System.currentTimeMillis();
251 nbScrollClick += nbScrolls;
252 }
253
254 // --------------------------------------------------------------------
255 // Thread
256 // --------------------------------------------------------------------
257
258 @Override
259 public void run() {
260 while ((System.currentTimeMillis() - fLastPoolTime) < QUIET_TIME) {
261 try {
262 Thread.sleep(POLLING_INTERVAL);
263 } catch (Exception e) {
264 return;
265 }
266 }
267 // Done waiting. Notify the histogram.
20ff3b75 268 if (!isInterrupted()) {
c392540b 269 fZoom.zoom(nbScrollClick);
20ff3b75 270 }
c392540b
FC
271 }
272 }
273
274}
This page took 0.0494019999999999 seconds and 5 git commands to generate.