Commit | Line | Data |
---|---|---|
c392540b | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2011, 2013 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) { |
41b5c37f AM |
140 | long realStart = startTime; |
141 | ||
142 | if (realStart < fAbsoluteStartTime) { | |
143 | realStart = fAbsoluteStartTime; | |
20ff3b75 | 144 | } |
c392540b | 145 | |
41b5c37f | 146 | long endTime = realStart + duration; |
c392540b FC |
147 | if (endTime > fAbsoluteEndTime) { |
148 | endTime = fAbsoluteEndTime; | |
20ff3b75 | 149 | if (endTime - duration > fAbsoluteStartTime) { |
41b5c37f | 150 | realStart = endTime - duration; |
20ff3b75 | 151 | } else { |
41b5c37f | 152 | realStart = fAbsoluteStartTime; |
c392540b FC |
153 | } |
154 | } | |
155 | ||
41b5c37f AM |
156 | fRangeStartTime = realStart; |
157 | fRangeDuration = endTime - realStart; | |
c392540b FC |
158 | } |
159 | ||
160 | // ------------------------------------------------------------------------ | |
161 | // MouseWheelListener | |
162 | // ------------------------------------------------------------------------ | |
163 | ||
c392540b FC |
164 | @Override |
165 | public synchronized void mouseScrolled(MouseEvent event) { | |
166 | if (fScrollCounter == null) { | |
167 | fScrollCounter = new MouseScrollCounter(this); | |
168 | fScrollCounter.start(); | |
c392540b FC |
169 | } |
170 | fScrollCounter.incrementMouseScroll(event.count); | |
171 | } | |
172 | ||
173 | private synchronized void zoom(int nbClicks) { | |
174 | // The job is finished | |
175 | fScrollCounter = null; | |
176 | ||
177 | // Compute the new time range | |
178 | long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR)); | |
179 | ||
bd6307ff | 180 | // Distribute delta and adjust for boundaries |
83f4e378 | 181 | long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2); |
c392540b FC |
182 | long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange); |
183 | requestedStart = validateStart(requestedEnd - requestedRange); | |
184 | ||
185 | fHistogram.updateTimeRange(requestedStart, requestedEnd); | |
186 | } | |
187 | ||
188 | private long validateStart(long start) { | |
41b5c37f AM |
189 | long realStart = start; |
190 | ||
191 | if (realStart < fAbsoluteStartTime) { | |
192 | realStart = fAbsoluteStartTime; | |
20ff3b75 | 193 | } |
41b5c37f AM |
194 | if (realStart > fAbsoluteEndTime) { |
195 | realStart = fAbsoluteEndTime - fMinWindowSize; | |
20ff3b75 | 196 | } |
41b5c37f | 197 | return realStart; |
c392540b FC |
198 | } |
199 | ||
200 | private long validateEnd(long start, long end) { | |
41b5c37f AM |
201 | long realEnd = end; |
202 | ||
203 | if (realEnd > fAbsoluteEndTime) { | |
204 | realEnd = fAbsoluteEndTime; | |
20ff3b75 | 205 | } |
41b5c37f AM |
206 | if (realEnd < start + fMinWindowSize) { |
207 | realEnd = start + fMinWindowSize; | |
20ff3b75 | 208 | } |
41b5c37f | 209 | return realEnd; |
c392540b FC |
210 | } |
211 | ||
212 | // ------------------------------------------------------------------------ | |
213 | // DelayedMouseScroll | |
214 | // ------------------------------------------------------------------------ | |
215 | ||
5a5c2fc7 | 216 | private static class MouseScrollCounter extends Thread { |
c392540b FC |
217 | |
218 | // -------------------------------------------------------------------- | |
219 | // Constants | |
220 | // -------------------------------------------------------------------- | |
221 | ||
222 | private final static long QUIET_TIME = 100L; | |
223 | private final static long POLLING_INTERVAL = 10L; | |
224 | ||
225 | // -------------------------------------------------------------------- | |
226 | // Attributes | |
227 | // -------------------------------------------------------------------- | |
228 | ||
229 | private HistogramZoom fZoom = null; | |
230 | ||
231 | private long fLastPoolTime = 0L; | |
232 | private int nbScrollClick = 0; | |
233 | ||
234 | // -------------------------------------------------------------------- | |
e0752744 | 235 | // Constructors |
c392540b FC |
236 | // -------------------------------------------------------------------- |
237 | ||
20ff3b75 | 238 | /** |
b544077e BH |
239 | * Constructor of inner class to handle consecutive scrolls of mouse wheel. |
240 | * @param zoom the histogram zoom reference | |
241 | */ | |
c392540b FC |
242 | public MouseScrollCounter(HistogramZoom zoom) { |
243 | fZoom = zoom; | |
244 | fLastPoolTime = System.currentTimeMillis(); | |
245 | } | |
246 | ||
247 | // -------------------------------------------------------------------- | |
248 | // Operation | |
249 | // -------------------------------------------------------------------- | |
20ff3b75 | 250 | |
b544077e BH |
251 | /** |
252 | * Increments the number of scroll clicks. | |
253 | * @param nbScrolls the number to add to the current value | |
254 | */ | |
c392540b FC |
255 | public void incrementMouseScroll(int nbScrolls) { |
256 | fLastPoolTime = System.currentTimeMillis(); | |
257 | nbScrollClick += nbScrolls; | |
258 | } | |
259 | ||
260 | // -------------------------------------------------------------------- | |
261 | // Thread | |
262 | // -------------------------------------------------------------------- | |
263 | ||
264 | @Override | |
265 | public void run() { | |
266 | while ((System.currentTimeMillis() - fLastPoolTime) < QUIET_TIME) { | |
267 | try { | |
268 | Thread.sleep(POLLING_INTERVAL); | |
269 | } catch (Exception e) { | |
270 | return; | |
271 | } | |
272 | } | |
273 | // Done waiting. Notify the histogram. | |
20ff3b75 | 274 | if (!isInterrupted()) { |
c392540b | 275 | fZoom.zoom(nbScrollClick); |
20ff3b75 | 276 | } |
c392540b FC |
277 | } |
278 | } | |
279 | ||
280 | } |