| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 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 | * Francois Chouinard - Moved from LTTng to TMF |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
| 15 | |
| 16 | import org.eclipse.swt.events.MouseEvent; |
| 17 | import org.eclipse.swt.events.MouseWheelListener; |
| 18 | import org.eclipse.swt.widgets.Canvas; |
| 19 | |
| 20 | /** |
| 21 | * Class to handle zooming within histogram windows.. |
| 22 | * |
| 23 | * @version 1.0 |
| 24 | * @author Francois Chouinard |
| 25 | * <p> |
| 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 | // ------------------------------------------------------------------------ |
| 52 | // Constructors |
| 53 | // ------------------------------------------------------------------------ |
| 54 | |
| 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) { |
| 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 | |
| 85 | /** |
| 86 | * Get start time of the zoom window. |
| 87 | * @return the start time. |
| 88 | */ |
| 89 | public synchronized long getStartTime() { |
| 90 | return fRangeStartTime; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the end time of the zoom window. |
| 95 | * @return the end time |
| 96 | */ |
| 97 | public synchronized long getEndTime() { |
| 98 | return fRangeStartTime + fRangeDuration; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the duration of the zoom window. |
| 103 | * @return the duration of the zoom window. |
| 104 | */ |
| 105 | public synchronized long getDuration() { |
| 106 | return fRangeDuration; |
| 107 | } |
| 108 | |
| 109 | // ------------------------------------------------------------------------ |
| 110 | // Operations |
| 111 | // ------------------------------------------------------------------------ |
| 112 | |
| 113 | /** |
| 114 | * Stops the zooming (multiple consecutive execution) |
| 115 | */ |
| 116 | public synchronized void stop() { |
| 117 | if (fScrollCounter != null) { |
| 118 | fScrollCounter.interrupt(); |
| 119 | fScrollCounter = null; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * The the full time range of the histogram |
| 125 | * |
| 126 | * @param startTime the start time the histogram |
| 127 | * @param endTime the end time of the histogram |
| 128 | */ |
| 129 | public synchronized void setFullRange(long startTime, long endTime) { |
| 130 | fAbsoluteStartTime = startTime; |
| 131 | fAbsoluteEndTime = endTime; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sets the new zoom window |
| 136 | * @param startTime the start time |
| 137 | * @param duration the duration |
| 138 | */ |
| 139 | public synchronized void setNewRange(long startTime, long duration) { |
| 140 | long realStart = startTime; |
| 141 | |
| 142 | if (realStart < fAbsoluteStartTime) { |
| 143 | realStart = fAbsoluteStartTime; |
| 144 | } |
| 145 | |
| 146 | long endTime = realStart + duration; |
| 147 | if (endTime > fAbsoluteEndTime) { |
| 148 | endTime = fAbsoluteEndTime; |
| 149 | if (endTime - duration > fAbsoluteStartTime) { |
| 150 | realStart = endTime - duration; |
| 151 | } else { |
| 152 | realStart = fAbsoluteStartTime; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | fRangeStartTime = realStart; |
| 157 | fRangeDuration = endTime - realStart; |
| 158 | } |
| 159 | |
| 160 | // ------------------------------------------------------------------------ |
| 161 | // MouseWheelListener |
| 162 | // ------------------------------------------------------------------------ |
| 163 | |
| 164 | @Override |
| 165 | public synchronized void mouseScrolled(MouseEvent event) { |
| 166 | if (fScrollCounter == null) { |
| 167 | fScrollCounter = new MouseScrollCounter(this); |
| 168 | fScrollCounter.start(); |
| 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 | |
| 180 | // Distribute delta and adjust for boundaries |
| 181 | long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2); |
| 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) { |
| 189 | long realStart = start; |
| 190 | |
| 191 | if (realStart < fAbsoluteStartTime) { |
| 192 | realStart = fAbsoluteStartTime; |
| 193 | } |
| 194 | if (realStart > fAbsoluteEndTime) { |
| 195 | realStart = fAbsoluteEndTime - fMinWindowSize; |
| 196 | } |
| 197 | return realStart; |
| 198 | } |
| 199 | |
| 200 | private long validateEnd(long start, long end) { |
| 201 | long realEnd = end; |
| 202 | |
| 203 | if (realEnd > fAbsoluteEndTime) { |
| 204 | realEnd = fAbsoluteEndTime; |
| 205 | } |
| 206 | if (realEnd < start + fMinWindowSize) { |
| 207 | realEnd = start + fMinWindowSize; |
| 208 | } |
| 209 | return realEnd; |
| 210 | } |
| 211 | |
| 212 | // ------------------------------------------------------------------------ |
| 213 | // DelayedMouseScroll |
| 214 | // ------------------------------------------------------------------------ |
| 215 | |
| 216 | private static class MouseScrollCounter extends Thread { |
| 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 | // -------------------------------------------------------------------- |
| 235 | // Constructors |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Constructor of inner class to handle consecutive scrolls of mouse wheel. |
| 240 | * @param zoom the histogram zoom reference |
| 241 | */ |
| 242 | public MouseScrollCounter(HistogramZoom zoom) { |
| 243 | fZoom = zoom; |
| 244 | fLastPoolTime = System.currentTimeMillis(); |
| 245 | } |
| 246 | |
| 247 | // -------------------------------------------------------------------- |
| 248 | // Operation |
| 249 | // -------------------------------------------------------------------- |
| 250 | |
| 251 | /** |
| 252 | * Increments the number of scroll clicks. |
| 253 | * @param nbScrolls the number to add to the current value |
| 254 | */ |
| 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. |
| 274 | if (!isInterrupted()) { |
| 275 | fZoom.zoom(nbScrollClick); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | } |