Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / histogram / HistogramZoom.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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.tracecompass.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 */
64 public HistogramZoom(Histogram histogram, long start, long end) {
65 fHistogram = histogram;
66 fAbsoluteStartTime = start;
67 fAbsoluteEndTime = end;
68 fMinWindowSize = 0;
69
70 fRangeStartTime = fAbsoluteStartTime;
71 fRangeDuration = fAbsoluteStartTime + fMinWindowSize;
72
73 histogram.addMouseWheelListener(this);
74 histogram.addKeyListener(this);
75 }
76
77 // ------------------------------------------------------------------------
78 // Accessors
79 // ------------------------------------------------------------------------
80
81 /**
82 * Get start time of the zoom window.
83 * @return the start time.
84 */
85 public synchronized long getStartTime() {
86 return fRangeStartTime;
87 }
88
89 /**
90 * Get the end time of the zoom window.
91 * @return the end time
92 */
93 public synchronized long getEndTime() {
94 return fRangeStartTime + fRangeDuration;
95 }
96
97 /**
98 * Get the duration of the zoom window.
99 * @return the duration of the zoom window.
100 */
101 public synchronized long getDuration() {
102 return fRangeDuration;
103 }
104
105 // ------------------------------------------------------------------------
106 // Operations
107 // ------------------------------------------------------------------------
108
109 /**
110 * The the full time range of the histogram
111 *
112 * @param startTime the start time the histogram
113 * @param endTime the end time of the histogram
114 */
115 public synchronized void setFullRange(long startTime, long endTime) {
116 fAbsoluteStartTime = startTime;
117 fAbsoluteEndTime = endTime;
118 }
119
120 /**
121 * Sets the new zoom window
122 * @param startTime the start time
123 * @param duration the duration
124 */
125 public synchronized void setNewRange(long startTime, long duration) {
126 long realStart = startTime;
127
128 if (realStart < fAbsoluteStartTime) {
129 realStart = fAbsoluteStartTime;
130 }
131
132 long endTime = realStart + duration;
133 if (endTime > fAbsoluteEndTime) {
134 endTime = fAbsoluteEndTime;
135 if (endTime - duration > fAbsoluteStartTime) {
136 realStart = endTime - duration;
137 } else {
138 realStart = fAbsoluteStartTime;
139 }
140 }
141
142 fRangeStartTime = realStart;
143 fRangeDuration = endTime - realStart;
144 }
145
146 // ------------------------------------------------------------------------
147 // MouseWheelListener
148 // ------------------------------------------------------------------------
149
150 @Override
151 public void mouseScrolled(MouseEvent event) {
152 zoom(event.count);
153 }
154
155 @Override
156 public void keyPressed(KeyEvent e) {
157 if (e.character == '+') {
158 zoom(1);
159 } else if (e.character == '-') {
160 zoom(-1);
161 }
162 }
163
164 @Override
165 public void keyReleased(KeyEvent e) {
166 }
167
168 private synchronized void zoom(int nbClicks) {
169 // Compute the new time range
170 long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR));
171
172 // Distribute delta and adjust for boundaries
173 long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2);
174 long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange);
175 requestedStart = validateStart(requestedEnd - requestedRange);
176
177 fHistogram.updateTimeRange(requestedStart, requestedEnd);
178 }
179
180 private long validateStart(long start) {
181 long realStart = start;
182
183 if (realStart < fAbsoluteStartTime) {
184 realStart = fAbsoluteStartTime;
185 }
186 if (realStart > fAbsoluteEndTime) {
187 realStart = fAbsoluteEndTime - fMinWindowSize;
188 }
189 return realStart;
190 }
191
192 private long validateEnd(long start, long end) {
193 long realEnd = end;
194
195 if (realEnd > fAbsoluteEndTime) {
196 realEnd = fAbsoluteEndTime;
197 }
198 if (realEnd < start + fMinWindowSize) {
199 realEnd = start + fMinWindowSize;
200 }
201 return realEnd;
202 }
203 }
This page took 0.135083 seconds and 6 git commands to generate.