Commit | Line | Data |
---|---|---|
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 | 14 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
c392540b FC |
15 | |
16 | import org.eclipse.swt.events.MouseEvent; | |
17 | import org.eclipse.swt.events.MouseWheelListener; | |
18 | import 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 | */ |
27 | public 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 | ||
162 | private long fMouseTimestamp = 0; | |
163 | ||
164 | @Override | |
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); | |
170 | } | |
171 | fScrollCounter.incrementMouseScroll(event.count); | |
172 | } | |
173 | ||
174 | private synchronized void zoom(int nbClicks) { | |
175 | // The job is finished | |
176 | fScrollCounter = null; | |
177 | ||
178 | // Compute the new time range | |
179 | long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR)); | |
180 | ||
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); | |
186 | ||
187 | fHistogram.updateTimeRange(requestedStart, requestedEnd); | |
188 | } | |
189 | ||
190 | private long validateStart(long start) { | |
20ff3b75 | 191 | if (start < fAbsoluteStartTime) { |
c392540b | 192 | start = fAbsoluteStartTime; |
20ff3b75 AM |
193 | } |
194 | if (start > fAbsoluteEndTime) { | |
c392540b | 195 | start = fAbsoluteEndTime - fMinWindowSize; |
20ff3b75 | 196 | } |
c392540b FC |
197 | return start; |
198 | } | |
199 | ||
200 | private long validateEnd(long start, long end) { | |
20ff3b75 | 201 | if (end > fAbsoluteEndTime) { |
c392540b | 202 | end = fAbsoluteEndTime; |
20ff3b75 AM |
203 | } |
204 | if (end < start + fMinWindowSize) { | |
c392540b | 205 | end = start + fMinWindowSize; |
20ff3b75 | 206 | } |
c392540b FC |
207 | return end; |
208 | } | |
209 | ||
210 | // ------------------------------------------------------------------------ | |
211 | // DelayedMouseScroll | |
212 | // ------------------------------------------------------------------------ | |
213 | ||
5a5c2fc7 | 214 | private static class MouseScrollCounter extends Thread { |
c392540b FC |
215 | |
216 | // -------------------------------------------------------------------- | |
217 | // Constants | |
218 | // -------------------------------------------------------------------- | |
219 | ||
220 | private final static long QUIET_TIME = 100L; | |
221 | private final static long POLLING_INTERVAL = 10L; | |
222 | ||
223 | // -------------------------------------------------------------------- | |
224 | // Attributes | |
225 | // -------------------------------------------------------------------- | |
226 | ||
227 | private HistogramZoom fZoom = null; | |
228 | ||
229 | private long fLastPoolTime = 0L; | |
230 | private int nbScrollClick = 0; | |
231 | ||
232 | // -------------------------------------------------------------------- | |
e0752744 | 233 | // Constructors |
c392540b FC |
234 | // -------------------------------------------------------------------- |
235 | ||
20ff3b75 | 236 | /** |
b544077e BH |
237 | * Constructor of inner class to handle consecutive scrolls of mouse wheel. |
238 | * @param zoom the histogram zoom reference | |
239 | */ | |
c392540b FC |
240 | public MouseScrollCounter(HistogramZoom zoom) { |
241 | fZoom = zoom; | |
242 | fLastPoolTime = System.currentTimeMillis(); | |
243 | } | |
244 | ||
245 | // -------------------------------------------------------------------- | |
246 | // Operation | |
247 | // -------------------------------------------------------------------- | |
20ff3b75 | 248 | |
b544077e BH |
249 | /** |
250 | * Increments the number of scroll clicks. | |
251 | * @param nbScrolls the number to add to the current value | |
252 | */ | |
c392540b FC |
253 | public void incrementMouseScroll(int nbScrolls) { |
254 | fLastPoolTime = System.currentTimeMillis(); | |
255 | nbScrollClick += nbScrolls; | |
256 | } | |
257 | ||
258 | // -------------------------------------------------------------------- | |
259 | // Thread | |
260 | // -------------------------------------------------------------------- | |
261 | ||
262 | @Override | |
263 | public void run() { | |
264 | while ((System.currentTimeMillis() - fLastPoolTime) < QUIET_TIME) { | |
265 | try { | |
266 | Thread.sleep(POLLING_INTERVAL); | |
267 | } catch (Exception e) { | |
268 | return; | |
269 | } | |
270 | } | |
271 | // Done waiting. Notify the histogram. | |
20ff3b75 | 272 | if (!isInterrupted()) { |
c392540b | 273 | fZoom.zoom(nbScrollClick); |
20ff3b75 | 274 | } |
c392540b FC |
275 | } |
276 | } | |
277 | ||
278 | } |