tmf: Add a trace-getting method in TmfView
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramZoom.java
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 // ------------------------------------------------------------------------
50 // Constructors
51 // ------------------------------------------------------------------------
52
53 /**
54 * Standard constructor.
55 *
56 * @param histogram
57 * The parent histogram object
58 * @param canvas
59 * The canvas
60 * @param start
61 * The start time of the zoom area
62 * @param end
63 * The end time of the zoom area
64 */
65 public HistogramZoom(Histogram histogram, Canvas canvas, long start,
66 long end) {
67 fHistogram = histogram;
68 fCanvas = canvas;
69 fAbsoluteStartTime = start;
70 fAbsoluteEndTime = end;
71 fMinWindowSize = fCanvas.getBounds().x;
72
73 fRangeStartTime = fAbsoluteStartTime;
74 fRangeDuration = fAbsoluteStartTime + fMinWindowSize;
75
76 canvas.addMouseWheelListener(this);
77 }
78
79 // ------------------------------------------------------------------------
80 // Accessors
81 // ------------------------------------------------------------------------
82
83 /**
84 * Get start time of the zoom window.
85 * @return the start time.
86 */
87 public synchronized long getStartTime() {
88 return fRangeStartTime;
89 }
90
91 /**
92 * Get the end time of the zoom window.
93 * @return the end time
94 */
95 public synchronized long getEndTime() {
96 return fRangeStartTime + fRangeDuration;
97 }
98
99 /**
100 * Get the duration of the zoom window.
101 * @return the duration of the zoom window.
102 */
103 public synchronized long getDuration() {
104 return fRangeDuration;
105 }
106
107 // ------------------------------------------------------------------------
108 // Operations
109 // ------------------------------------------------------------------------
110
111 /**
112 * The the full time range of the histogram
113 *
114 * @param startTime the start time the histogram
115 * @param endTime the end time of the histogram
116 */
117 public synchronized void setFullRange(long startTime, long endTime) {
118 fAbsoluteStartTime = startTime;
119 fAbsoluteEndTime = endTime;
120 }
121
122 /**
123 * Sets the new zoom window
124 * @param startTime the start time
125 * @param duration the duration
126 */
127 public synchronized void setNewRange(long startTime, long duration) {
128 long realStart = startTime;
129
130 if (realStart < fAbsoluteStartTime) {
131 realStart = fAbsoluteStartTime;
132 }
133
134 long endTime = realStart + duration;
135 if (endTime > fAbsoluteEndTime) {
136 endTime = fAbsoluteEndTime;
137 if (endTime - duration > fAbsoluteStartTime) {
138 realStart = endTime - duration;
139 } else {
140 realStart = fAbsoluteStartTime;
141 }
142 }
143
144 fRangeStartTime = realStart;
145 fRangeDuration = endTime - realStart;
146 }
147
148 // ------------------------------------------------------------------------
149 // MouseWheelListener
150 // ------------------------------------------------------------------------
151
152 @Override
153 public synchronized void mouseScrolled(MouseEvent event) {
154 zoom(event.count);
155 }
156
157 private synchronized void zoom(int nbClicks) {
158 // Compute the new time range
159 long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR));
160
161 // Distribute delta and adjust for boundaries
162 long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2);
163 long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange);
164 requestedStart = validateStart(requestedEnd - requestedRange);
165
166 fHistogram.updateTimeRange(requestedStart, requestedEnd);
167 }
168
169 private long validateStart(long start) {
170 long realStart = start;
171
172 if (realStart < fAbsoluteStartTime) {
173 realStart = fAbsoluteStartTime;
174 }
175 if (realStart > fAbsoluteEndTime) {
176 realStart = fAbsoluteEndTime - fMinWindowSize;
177 }
178 return realStart;
179 }
180
181 private long validateEnd(long start, long end) {
182 long realEnd = end;
183
184 if (realEnd > fAbsoluteEndTime) {
185 realEnd = fAbsoluteEndTime;
186 }
187 if (realEnd < start + fMinWindowSize) {
188 realEnd = start + fMinWindowSize;
189 }
190 return realEnd;
191 }
192 }
This page took 0.035378 seconds and 5 git commands to generate.