tmf: Add support for time range selection
[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
FC
16
17import org.eclipse.swt.events.MouseEvent;
18import org.eclipse.swt.events.MouseWheelListener;
c392540b
FC
19
20/**
b544077e 21 * Class to handle zooming within histogram windows..
20ff3b75 22 *
b544077e
BH
23 * @version 1.0
24 * @author Francois Chouinard
c392540b 25 * <p>
c392540b
FC
26 */
27public 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;
c392540b
FC
40
41 private long fAbsoluteStartTime;
42 private long fAbsoluteEndTime;
43 private final long fMinWindowSize;
44
45 private long fRangeStartTime;
46 private long fRangeDuration;
47
c392540b 48 // ------------------------------------------------------------------------
e0752744 49 // Constructors
c392540b
FC
50 // ------------------------------------------------------------------------
51
20ff3b75
AM
52 /**
53 * Standard constructor.
54 *
55 * @param histogram
56 * The parent histogram object
20ff3b75
AM
57 * @param start
58 * The start time of the zoom area
59 * @param end
60 * The end time of the zoom area
65cdf787 61 * @since 2.0
20ff3b75 62 */
65cdf787 63 public HistogramZoom(Histogram histogram, long start, long end) {
c392540b 64 fHistogram = histogram;
c392540b
FC
65 fAbsoluteStartTime = start;
66 fAbsoluteEndTime = end;
65cdf787 67 fMinWindowSize = 0;
c392540b
FC
68
69 fRangeStartTime = fAbsoluteStartTime;
70 fRangeDuration = fAbsoluteStartTime + fMinWindowSize;
c392540b
FC
71 }
72
73 // ------------------------------------------------------------------------
74 // Accessors
75 // ------------------------------------------------------------------------
76
b544077e
BH
77 /**
78 * Get start time of the zoom window.
79 * @return the start time.
80 */
5a5c2fc7 81 public synchronized long getStartTime() {
c392540b
FC
82 return fRangeStartTime;
83 }
84
b544077e
BH
85 /**
86 * Get the end time of the zoom window.
87 * @return the end time
88 */
5a5c2fc7 89 public synchronized long getEndTime() {
c392540b
FC
90 return fRangeStartTime + fRangeDuration;
91 }
92
b544077e
BH
93 /**
94 * Get the duration of the zoom window.
95 * @return the duration of the zoom window.
96 */
5a5c2fc7 97 public synchronized long getDuration() {
c392540b
FC
98 return fRangeDuration;
99 }
100
101 // ------------------------------------------------------------------------
102 // Operations
103 // ------------------------------------------------------------------------
104
b544077e
BH
105 /**
106 * The the full time range of the histogram
20ff3b75 107 *
b544077e
BH
108 * @param startTime the start time the histogram
109 * @param endTime the end time of the histogram
110 */
8edafa7f 111 public synchronized void setFullRange(long startTime, long endTime) {
c392540b
FC
112 fAbsoluteStartTime = startTime;
113 fAbsoluteEndTime = endTime;
114 }
115
b544077e
BH
116 /**
117 * Sets the new zoom window
118 * @param startTime the start time
119 * @param duration the duration
120 */
8edafa7f 121 public synchronized void setNewRange(long startTime, long duration) {
41b5c37f
AM
122 long realStart = startTime;
123
124 if (realStart < fAbsoluteStartTime) {
125 realStart = fAbsoluteStartTime;
20ff3b75 126 }
c392540b 127
41b5c37f 128 long endTime = realStart + duration;
c392540b
FC
129 if (endTime > fAbsoluteEndTime) {
130 endTime = fAbsoluteEndTime;
20ff3b75 131 if (endTime - duration > fAbsoluteStartTime) {
41b5c37f 132 realStart = endTime - duration;
20ff3b75 133 } else {
41b5c37f 134 realStart = fAbsoluteStartTime;
c392540b
FC
135 }
136 }
137
41b5c37f
AM
138 fRangeStartTime = realStart;
139 fRangeDuration = endTime - realStart;
c392540b
FC
140 }
141
142 // ------------------------------------------------------------------------
143 // MouseWheelListener
144 // ------------------------------------------------------------------------
145
c392540b
FC
146 @Override
147 public synchronized void mouseScrolled(MouseEvent event) {
38df2c82 148 zoom(event.count);
c392540b
FC
149 }
150
151 private synchronized void zoom(int nbClicks) {
c392540b
FC
152 // Compute the new time range
153 long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR));
154
bd6307ff 155 // Distribute delta and adjust for boundaries
83f4e378 156 long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2);
c392540b
FC
157 long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange);
158 requestedStart = validateStart(requestedEnd - requestedRange);
159
160 fHistogram.updateTimeRange(requestedStart, requestedEnd);
161 }
162
163 private long validateStart(long start) {
41b5c37f
AM
164 long realStart = start;
165
166 if (realStart < fAbsoluteStartTime) {
167 realStart = fAbsoluteStartTime;
20ff3b75 168 }
41b5c37f
AM
169 if (realStart > fAbsoluteEndTime) {
170 realStart = fAbsoluteEndTime - fMinWindowSize;
20ff3b75 171 }
41b5c37f 172 return realStart;
c392540b
FC
173 }
174
175 private long validateEnd(long start, long end) {
41b5c37f
AM
176 long realEnd = end;
177
178 if (realEnd > fAbsoluteEndTime) {
179 realEnd = fAbsoluteEndTime;
20ff3b75 180 }
41b5c37f
AM
181 if (realEnd < start + fMinWindowSize) {
182 realEnd = start + fMinWindowSize;
20ff3b75 183 }
41b5c37f 184 return realEnd;
c392540b 185 }
c392540b 186}
This page took 0.048491 seconds and 5 git commands to generate.