Commit | Line | Data |
---|---|---|
c392540b FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.ui.views.histogram; | |
14 | ||
15 | import org.eclipse.swt.events.MouseEvent; | |
16 | import org.eclipse.swt.events.MouseWheelListener; | |
17 | import org.eclipse.swt.widgets.Canvas; | |
18 | ||
19 | /** | |
20 | * <b><u>HistogramZoom</u></b> | |
21 | * <p> | |
22 | * TODO: Document me... | |
23 | */ | |
24 | public class HistogramZoom implements MouseWheelListener { | |
25 | ||
26 | // ------------------------------------------------------------------------ | |
27 | // Constants | |
28 | // ------------------------------------------------------------------------ | |
29 | ||
30 | private final static double ZOOM_FACTOR = 0.8; | |
31 | ||
32 | // ------------------------------------------------------------------------ | |
33 | // Attributes | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
36 | private final Histogram fHistogram; | |
37 | private final Canvas fCanvas; | |
38 | ||
39 | private long fAbsoluteStartTime; | |
40 | private long fAbsoluteEndTime; | |
41 | private final long fMinWindowSize; | |
42 | ||
43 | private long fRangeStartTime; | |
44 | private long fRangeDuration; | |
45 | ||
46 | private MouseScrollCounter fScrollCounter; | |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Construction | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
52 | public HistogramZoom(Histogram histogram, Canvas canvas, long start, long end) { | |
53 | fHistogram = histogram; | |
54 | fCanvas = canvas; | |
55 | fAbsoluteStartTime = start; | |
56 | fAbsoluteEndTime = end; | |
57 | fMinWindowSize = fCanvas.getBounds().x; | |
58 | ||
59 | fRangeStartTime = fAbsoluteStartTime; | |
60 | fRangeDuration = fAbsoluteStartTime + fMinWindowSize; | |
61 | ||
62 | canvas.addMouseWheelListener(this); | |
63 | } | |
64 | ||
65 | // ------------------------------------------------------------------------ | |
66 | // Accessors | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | public long getStartTime() { | |
70 | return fRangeStartTime; | |
71 | } | |
72 | ||
73 | public long getEndTime() { | |
74 | return fRangeStartTime + fRangeDuration; | |
75 | } | |
76 | ||
77 | public long getDuration() { | |
78 | return fRangeDuration; | |
79 | } | |
80 | ||
81 | // ------------------------------------------------------------------------ | |
82 | // Operations | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
85 | public synchronized void stop() { | |
86 | if (fScrollCounter != null) { | |
87 | fScrollCounter.interrupt(); | |
88 | fScrollCounter = null; | |
89 | } | |
90 | } | |
91 | ||
8edafa7f | 92 | public synchronized void setFullRange(long startTime, long endTime) { |
c392540b FC |
93 | fAbsoluteStartTime = startTime; |
94 | fAbsoluteEndTime = endTime; | |
95 | } | |
96 | ||
8edafa7f | 97 | public synchronized void setNewRange(long startTime, long duration) { |
c392540b FC |
98 | if (startTime < fAbsoluteStartTime) |
99 | startTime = fAbsoluteStartTime; | |
100 | ||
101 | long endTime = startTime + duration; | |
102 | if (endTime > fAbsoluteEndTime) { | |
103 | endTime = fAbsoluteEndTime; | |
104 | if (endTime - duration > fAbsoluteStartTime) | |
105 | startTime = endTime - duration; | |
106 | else { | |
107 | startTime = fAbsoluteStartTime; | |
108 | } | |
109 | } | |
110 | ||
111 | fRangeStartTime = startTime; | |
112 | fRangeDuration = endTime - startTime; | |
113 | } | |
114 | ||
115 | // ------------------------------------------------------------------------ | |
116 | // MouseWheelListener | |
117 | // ------------------------------------------------------------------------ | |
118 | ||
119 | private long fMouseTimestamp = 0; | |
120 | ||
121 | @Override | |
122 | public synchronized void mouseScrolled(MouseEvent event) { | |
123 | if (fScrollCounter == null) { | |
124 | fScrollCounter = new MouseScrollCounter(this); | |
125 | fScrollCounter.start(); | |
126 | fMouseTimestamp = fHistogram.getTimestamp(event.x); | |
127 | } | |
128 | fScrollCounter.incrementMouseScroll(event.count); | |
129 | } | |
130 | ||
131 | private synchronized void zoom(int nbClicks) { | |
132 | // The job is finished | |
133 | fScrollCounter = null; | |
134 | ||
135 | // Compute the new time range | |
136 | long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR)); | |
137 | ||
138 | // Perform a proportional zoom wrt the mouse position | |
139 | double ratio = ((double) (fMouseTimestamp - fRangeStartTime)) / fRangeDuration; | |
140 | long requestedStart = validateStart(fRangeStartTime + (long) ((fRangeDuration - requestedRange) * ratio)); | |
141 | long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange); | |
142 | requestedStart = validateStart(requestedEnd - requestedRange); | |
143 | ||
144 | fHistogram.updateTimeRange(requestedStart, requestedEnd); | |
145 | } | |
146 | ||
147 | private long validateStart(long start) { | |
148 | if (start < fAbsoluteStartTime) | |
149 | start = fAbsoluteStartTime; | |
150 | if (start > fAbsoluteEndTime) | |
151 | start = fAbsoluteEndTime - fMinWindowSize; | |
152 | return start; | |
153 | } | |
154 | ||
155 | private long validateEnd(long start, long end) { | |
156 | if (end > fAbsoluteEndTime) | |
157 | end = fAbsoluteEndTime; | |
158 | if (end < start + fMinWindowSize) | |
159 | end = start + fMinWindowSize; | |
160 | return end; | |
161 | } | |
162 | ||
163 | // ------------------------------------------------------------------------ | |
164 | // DelayedMouseScroll | |
165 | // ------------------------------------------------------------------------ | |
166 | ||
167 | private class MouseScrollCounter extends Thread { | |
168 | ||
169 | // -------------------------------------------------------------------- | |
170 | // Constants | |
171 | // -------------------------------------------------------------------- | |
172 | ||
173 | private final static long QUIET_TIME = 100L; | |
174 | private final static long POLLING_INTERVAL = 10L; | |
175 | ||
176 | // -------------------------------------------------------------------- | |
177 | // Attributes | |
178 | // -------------------------------------------------------------------- | |
179 | ||
180 | private HistogramZoom fZoom = null; | |
181 | ||
182 | private long fLastPoolTime = 0L; | |
183 | private int nbScrollClick = 0; | |
184 | ||
185 | // -------------------------------------------------------------------- | |
186 | // Construction | |
187 | // -------------------------------------------------------------------- | |
188 | ||
189 | public MouseScrollCounter(HistogramZoom zoom) { | |
190 | fZoom = zoom; | |
191 | fLastPoolTime = System.currentTimeMillis(); | |
192 | } | |
193 | ||
194 | // -------------------------------------------------------------------- | |
195 | // Operation | |
196 | // -------------------------------------------------------------------- | |
197 | ||
198 | public void incrementMouseScroll(int nbScrolls) { | |
199 | fLastPoolTime = System.currentTimeMillis(); | |
200 | nbScrollClick += nbScrolls; | |
201 | } | |
202 | ||
203 | // -------------------------------------------------------------------- | |
204 | // Thread | |
205 | // -------------------------------------------------------------------- | |
206 | ||
207 | @Override | |
208 | public void run() { | |
209 | while ((System.currentTimeMillis() - fLastPoolTime) < QUIET_TIME) { | |
210 | try { | |
211 | Thread.sleep(POLLING_INTERVAL); | |
212 | } catch (Exception e) { | |
213 | return; | |
214 | } | |
215 | } | |
216 | // Done waiting. Notify the histogram. | |
217 | if (!isInterrupted()) | |
218 | fZoom.zoom(nbScrollClick); | |
219 | } | |
220 | } | |
221 | ||
222 | } |