1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Moved from LTTng to TMF
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
16 import org
.eclipse
.swt
.events
.MouseEvent
;
17 import org
.eclipse
.swt
.events
.MouseWheelListener
;
18 import org
.eclipse
.swt
.widgets
.Canvas
;
21 * Class to handle zooming within histogram windows..
24 * @author Francois Chouinard
27 public class HistogramZoom
implements MouseWheelListener
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 private final static double ZOOM_FACTOR
= 0.8;
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final Histogram fHistogram
;
40 private final Canvas fCanvas
;
42 private long fAbsoluteStartTime
;
43 private long fAbsoluteEndTime
;
44 private final long fMinWindowSize
;
46 private long fRangeStartTime
;
47 private long fRangeDuration
;
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
54 * Standard constructor.
57 * The parent histogram object
61 * The start time of the zoom area
63 * The end time of the zoom area
65 public HistogramZoom(Histogram histogram
, Canvas canvas
, long start
,
67 fHistogram
= histogram
;
69 fAbsoluteStartTime
= start
;
70 fAbsoluteEndTime
= end
;
71 fMinWindowSize
= fCanvas
.getBounds().x
;
73 fRangeStartTime
= fAbsoluteStartTime
;
74 fRangeDuration
= fAbsoluteStartTime
+ fMinWindowSize
;
76 canvas
.addMouseWheelListener(this);
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Get start time of the zoom window.
85 * @return the start time.
87 public synchronized long getStartTime() {
88 return fRangeStartTime
;
92 * Get the end time of the zoom window.
93 * @return the end time
95 public synchronized long getEndTime() {
96 return fRangeStartTime
+ fRangeDuration
;
100 * Get the duration of the zoom window.
101 * @return the duration of the zoom window.
103 public synchronized long getDuration() {
104 return fRangeDuration
;
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
112 * The the full time range of the histogram
114 * @param startTime the start time the histogram
115 * @param endTime the end time of the histogram
117 public synchronized void setFullRange(long startTime
, long endTime
) {
118 fAbsoluteStartTime
= startTime
;
119 fAbsoluteEndTime
= endTime
;
123 * Sets the new zoom window
124 * @param startTime the start time
125 * @param duration the duration
127 public synchronized void setNewRange(long startTime
, long duration
) {
128 long realStart
= startTime
;
130 if (realStart
< fAbsoluteStartTime
) {
131 realStart
= fAbsoluteStartTime
;
134 long endTime
= realStart
+ duration
;
135 if (endTime
> fAbsoluteEndTime
) {
136 endTime
= fAbsoluteEndTime
;
137 if (endTime
- duration
> fAbsoluteStartTime
) {
138 realStart
= endTime
- duration
;
140 realStart
= fAbsoluteStartTime
;
144 fRangeStartTime
= realStart
;
145 fRangeDuration
= endTime
- realStart
;
148 // ------------------------------------------------------------------------
149 // MouseWheelListener
150 // ------------------------------------------------------------------------
153 public synchronized void mouseScrolled(MouseEvent event
) {
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
));
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
);
166 fHistogram
.updateTimeRange(requestedStart
, requestedEnd
);
169 private long validateStart(long start
) {
170 long realStart
= start
;
172 if (realStart
< fAbsoluteStartTime
) {
173 realStart
= fAbsoluteStartTime
;
175 if (realStart
> fAbsoluteEndTime
) {
176 realStart
= fAbsoluteEndTime
- fMinWindowSize
;
181 private long validateEnd(long start
, long end
) {
184 if (realEnd
> fAbsoluteEndTime
) {
185 realEnd
= fAbsoluteEndTime
;
187 if (realEnd
< start
+ fMinWindowSize
) {
188 realEnd
= start
+ fMinWindowSize
;
This page took 0.035378 seconds and 5 git commands to generate.