| 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 | * Patrick Tasse - Update for mouse wheel zoom |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
| 16 | |
| 17 | import org.eclipse.swt.events.KeyEvent; |
| 18 | import org.eclipse.swt.events.KeyListener; |
| 19 | import org.eclipse.swt.events.MouseEvent; |
| 20 | import org.eclipse.swt.events.MouseWheelListener; |
| 21 | |
| 22 | /** |
| 23 | * Class to handle zooming within histogram windows.. |
| 24 | * |
| 25 | * @version 1.0 |
| 26 | * @author Francois Chouinard |
| 27 | * <p> |
| 28 | */ |
| 29 | public class HistogramZoom implements MouseWheelListener, KeyListener { |
| 30 | |
| 31 | // ------------------------------------------------------------------------ |
| 32 | // Constants |
| 33 | // ------------------------------------------------------------------------ |
| 34 | |
| 35 | private final static double ZOOM_FACTOR = 0.8; |
| 36 | |
| 37 | // ------------------------------------------------------------------------ |
| 38 | // Attributes |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
| 41 | private final Histogram fHistogram; |
| 42 | |
| 43 | private long fAbsoluteStartTime; |
| 44 | private long fAbsoluteEndTime; |
| 45 | private final long fMinWindowSize; |
| 46 | |
| 47 | private long fRangeStartTime; |
| 48 | private long fRangeDuration; |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Constructors |
| 52 | // ------------------------------------------------------------------------ |
| 53 | |
| 54 | /** |
| 55 | * Standard constructor. |
| 56 | * |
| 57 | * @param histogram |
| 58 | * The parent histogram object |
| 59 | * @param start |
| 60 | * The start time of the zoom area |
| 61 | * @param end |
| 62 | * The end time of the zoom area |
| 63 | * @since 2.0 |
| 64 | */ |
| 65 | public HistogramZoom(Histogram histogram, long start, long end) { |
| 66 | fHistogram = histogram; |
| 67 | fAbsoluteStartTime = start; |
| 68 | fAbsoluteEndTime = end; |
| 69 | fMinWindowSize = 0; |
| 70 | |
| 71 | fRangeStartTime = fAbsoluteStartTime; |
| 72 | fRangeDuration = fAbsoluteStartTime + fMinWindowSize; |
| 73 | |
| 74 | histogram.addMouseWheelListener(this); |
| 75 | histogram.addKeyListener(this); |
| 76 | } |
| 77 | |
| 78 | // ------------------------------------------------------------------------ |
| 79 | // Accessors |
| 80 | // ------------------------------------------------------------------------ |
| 81 | |
| 82 | /** |
| 83 | * Get start time of the zoom window. |
| 84 | * @return the start time. |
| 85 | */ |
| 86 | public synchronized long getStartTime() { |
| 87 | return fRangeStartTime; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the end time of the zoom window. |
| 92 | * @return the end time |
| 93 | */ |
| 94 | public synchronized long getEndTime() { |
| 95 | return fRangeStartTime + fRangeDuration; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the duration of the zoom window. |
| 100 | * @return the duration of the zoom window. |
| 101 | */ |
| 102 | public synchronized long getDuration() { |
| 103 | return fRangeDuration; |
| 104 | } |
| 105 | |
| 106 | // ------------------------------------------------------------------------ |
| 107 | // Operations |
| 108 | // ------------------------------------------------------------------------ |
| 109 | |
| 110 | /** |
| 111 | * The the full time range of the histogram |
| 112 | * |
| 113 | * @param startTime the start time the histogram |
| 114 | * @param endTime the end time of the histogram |
| 115 | */ |
| 116 | public synchronized void setFullRange(long startTime, long endTime) { |
| 117 | fAbsoluteStartTime = startTime; |
| 118 | fAbsoluteEndTime = endTime; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the new zoom window |
| 123 | * @param startTime the start time |
| 124 | * @param duration the duration |
| 125 | */ |
| 126 | public synchronized void setNewRange(long startTime, long duration) { |
| 127 | long realStart = startTime; |
| 128 | |
| 129 | if (realStart < fAbsoluteStartTime) { |
| 130 | realStart = fAbsoluteStartTime; |
| 131 | } |
| 132 | |
| 133 | long endTime = realStart + duration; |
| 134 | if (endTime > fAbsoluteEndTime) { |
| 135 | endTime = fAbsoluteEndTime; |
| 136 | if (endTime - duration > fAbsoluteStartTime) { |
| 137 | realStart = endTime - duration; |
| 138 | } else { |
| 139 | realStart = fAbsoluteStartTime; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fRangeStartTime = realStart; |
| 144 | fRangeDuration = endTime - realStart; |
| 145 | } |
| 146 | |
| 147 | // ------------------------------------------------------------------------ |
| 148 | // MouseWheelListener |
| 149 | // ------------------------------------------------------------------------ |
| 150 | |
| 151 | @Override |
| 152 | public void mouseScrolled(MouseEvent event) { |
| 153 | zoom(event.count); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @since 3.1 |
| 158 | */ |
| 159 | @Override |
| 160 | public void keyPressed(KeyEvent e) { |
| 161 | if (e.character == '+') { |
| 162 | zoom(1); |
| 163 | } else if (e.character == '-') { |
| 164 | zoom(-1); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @since 3.1 |
| 170 | */ |
| 171 | @Override |
| 172 | public void keyReleased(KeyEvent e) { |
| 173 | } |
| 174 | |
| 175 | private synchronized void zoom(int nbClicks) { |
| 176 | // Compute the new time range |
| 177 | long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR)); |
| 178 | |
| 179 | // Distribute delta and adjust for boundaries |
| 180 | long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2); |
| 181 | long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange); |
| 182 | requestedStart = validateStart(requestedEnd - requestedRange); |
| 183 | |
| 184 | fHistogram.updateTimeRange(requestedStart, requestedEnd); |
| 185 | } |
| 186 | |
| 187 | private long validateStart(long start) { |
| 188 | long realStart = start; |
| 189 | |
| 190 | if (realStart < fAbsoluteStartTime) { |
| 191 | realStart = fAbsoluteStartTime; |
| 192 | } |
| 193 | if (realStart > fAbsoluteEndTime) { |
| 194 | realStart = fAbsoluteEndTime - fMinWindowSize; |
| 195 | } |
| 196 | return realStart; |
| 197 | } |
| 198 | |
| 199 | private long validateEnd(long start, long end) { |
| 200 | long realEnd = end; |
| 201 | |
| 202 | if (realEnd > fAbsoluteEndTime) { |
| 203 | realEnd = fAbsoluteEndTime; |
| 204 | } |
| 205 | if (realEnd < start + fMinWindowSize) { |
| 206 | realEnd = start + fMinWindowSize; |
| 207 | } |
| 208 | return realEnd; |
| 209 | } |
| 210 | } |