ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramZoom.java
CommitLineData
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
65cdf787 12 * Patrick Tasse - Update for mouse wheel zoom
c392540b
FC
13 *******************************************************************************/
14
e0752744 15package org.eclipse.linuxtools.tmf.ui.views.histogram;
c392540b 16
ba1f5c20
PT
17import org.eclipse.swt.events.KeyEvent;
18import org.eclipse.swt.events.KeyListener;
c392540b
FC
19import org.eclipse.swt.events.MouseEvent;
20import org.eclipse.swt.events.MouseWheelListener;
c392540b
FC
21
22/**
b544077e 23 * Class to handle zooming within histogram windows..
20ff3b75 24 *
b544077e
BH
25 * @version 1.0
26 * @author Francois Chouinard
c392540b 27 * <p>
c392540b 28 */
ba1f5c20 29public class HistogramZoom implements MouseWheelListener, KeyListener {
c392540b
FC
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;
c392540b
FC
42
43 private long fAbsoluteStartTime;
44 private long fAbsoluteEndTime;
45 private final long fMinWindowSize;
46
47 private long fRangeStartTime;
48 private long fRangeDuration;
49
c392540b 50 // ------------------------------------------------------------------------
e0752744 51 // Constructors
c392540b
FC
52 // ------------------------------------------------------------------------
53
20ff3b75
AM
54 /**
55 * Standard constructor.
56 *
57 * @param histogram
58 * The parent histogram object
20ff3b75
AM
59 * @param start
60 * The start time of the zoom area
61 * @param end
62 * The end time of the zoom area
65cdf787 63 * @since 2.0
20ff3b75 64 */
65cdf787 65 public HistogramZoom(Histogram histogram, long start, long end) {
c392540b 66 fHistogram = histogram;
c392540b
FC
67 fAbsoluteStartTime = start;
68 fAbsoluteEndTime = end;
65cdf787 69 fMinWindowSize = 0;
c392540b
FC
70
71 fRangeStartTime = fAbsoluteStartTime;
72 fRangeDuration = fAbsoluteStartTime + fMinWindowSize;
ba1f5c20
PT
73
74 histogram.addMouseWheelListener(this);
75 histogram.addKeyListener(this);
c392540b
FC
76 }
77
78 // ------------------------------------------------------------------------
79 // Accessors
80 // ------------------------------------------------------------------------
81
b544077e
BH
82 /**
83 * Get start time of the zoom window.
84 * @return the start time.
85 */
5a5c2fc7 86 public synchronized long getStartTime() {
c392540b
FC
87 return fRangeStartTime;
88 }
89
b544077e
BH
90 /**
91 * Get the end time of the zoom window.
92 * @return the end time
93 */
5a5c2fc7 94 public synchronized long getEndTime() {
c392540b
FC
95 return fRangeStartTime + fRangeDuration;
96 }
97
b544077e
BH
98 /**
99 * Get the duration of the zoom window.
100 * @return the duration of the zoom window.
101 */
5a5c2fc7 102 public synchronized long getDuration() {
c392540b
FC
103 return fRangeDuration;
104 }
105
106 // ------------------------------------------------------------------------
107 // Operations
108 // ------------------------------------------------------------------------
109
b544077e
BH
110 /**
111 * The the full time range of the histogram
20ff3b75 112 *
b544077e
BH
113 * @param startTime the start time the histogram
114 * @param endTime the end time of the histogram
115 */
8edafa7f 116 public synchronized void setFullRange(long startTime, long endTime) {
c392540b
FC
117 fAbsoluteStartTime = startTime;
118 fAbsoluteEndTime = endTime;
119 }
120
b544077e
BH
121 /**
122 * Sets the new zoom window
123 * @param startTime the start time
124 * @param duration the duration
125 */
8edafa7f 126 public synchronized void setNewRange(long startTime, long duration) {
41b5c37f
AM
127 long realStart = startTime;
128
129 if (realStart < fAbsoluteStartTime) {
130 realStart = fAbsoluteStartTime;
20ff3b75 131 }
c392540b 132
41b5c37f 133 long endTime = realStart + duration;
c392540b
FC
134 if (endTime > fAbsoluteEndTime) {
135 endTime = fAbsoluteEndTime;
20ff3b75 136 if (endTime - duration > fAbsoluteStartTime) {
41b5c37f 137 realStart = endTime - duration;
20ff3b75 138 } else {
41b5c37f 139 realStart = fAbsoluteStartTime;
c392540b
FC
140 }
141 }
142
41b5c37f
AM
143 fRangeStartTime = realStart;
144 fRangeDuration = endTime - realStart;
c392540b
FC
145 }
146
147 // ------------------------------------------------------------------------
148 // MouseWheelListener
149 // ------------------------------------------------------------------------
150
c392540b 151 @Override
ba1f5c20 152 public void mouseScrolled(MouseEvent event) {
38df2c82 153 zoom(event.count);
c392540b
FC
154 }
155
ba1f5c20
PT
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
c392540b 175 private synchronized void zoom(int nbClicks) {
c392540b
FC
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
bd6307ff 179 // Distribute delta and adjust for boundaries
83f4e378 180 long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2);
c392540b
FC
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) {
41b5c37f
AM
188 long realStart = start;
189
190 if (realStart < fAbsoluteStartTime) {
191 realStart = fAbsoluteStartTime;
20ff3b75 192 }
41b5c37f
AM
193 if (realStart > fAbsoluteEndTime) {
194 realStart = fAbsoluteEndTime - fMinWindowSize;
20ff3b75 195 }
41b5c37f 196 return realStart;
c392540b
FC
197 }
198
199 private long validateEnd(long start, long end) {
41b5c37f
AM
200 long realEnd = end;
201
202 if (realEnd > fAbsoluteEndTime) {
203 realEnd = fAbsoluteEndTime;
20ff3b75 204 }
41b5c37f
AM
205 if (realEnd < start + fMinWindowSize) {
206 realEnd = start + fMinWindowSize;
20ff3b75 207 }
41b5c37f 208 return realEnd;
c392540b 209 }
c392540b 210}
This page took 0.066256 seconds and 5 git commands to generate.