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 * Patrick Tasse - Update for mouse wheel zoom
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
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
;
23 * Class to handle zooming within histogram windows..
26 * @author Francois Chouinard
29 public class HistogramZoom
implements MouseWheelListener
, KeyListener
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 private final static double ZOOM_FACTOR
= 0.8;
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private final Histogram fHistogram
;
43 private long fAbsoluteStartTime
;
44 private long fAbsoluteEndTime
;
45 private final long fMinWindowSize
;
47 private long fRangeStartTime
;
48 private long fRangeDuration
;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
55 * Standard constructor.
58 * The parent histogram object
60 * The start time of the zoom area
62 * The end time of the zoom area
65 public HistogramZoom(Histogram histogram
, long start
, long end
) {
66 fHistogram
= histogram
;
67 fAbsoluteStartTime
= start
;
68 fAbsoluteEndTime
= end
;
71 fRangeStartTime
= fAbsoluteStartTime
;
72 fRangeDuration
= fAbsoluteStartTime
+ fMinWindowSize
;
74 histogram
.addMouseWheelListener(this);
75 histogram
.addKeyListener(this);
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
83 * Get start time of the zoom window.
84 * @return the start time.
86 public synchronized long getStartTime() {
87 return fRangeStartTime
;
91 * Get the end time of the zoom window.
92 * @return the end time
94 public synchronized long getEndTime() {
95 return fRangeStartTime
+ fRangeDuration
;
99 * Get the duration of the zoom window.
100 * @return the duration of the zoom window.
102 public synchronized long getDuration() {
103 return fRangeDuration
;
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 * The the full time range of the histogram
113 * @param startTime the start time the histogram
114 * @param endTime the end time of the histogram
116 public synchronized void setFullRange(long startTime
, long endTime
) {
117 fAbsoluteStartTime
= startTime
;
118 fAbsoluteEndTime
= endTime
;
122 * Sets the new zoom window
123 * @param startTime the start time
124 * @param duration the duration
126 public synchronized void setNewRange(long startTime
, long duration
) {
127 long realStart
= startTime
;
129 if (realStart
< fAbsoluteStartTime
) {
130 realStart
= fAbsoluteStartTime
;
133 long endTime
= realStart
+ duration
;
134 if (endTime
> fAbsoluteEndTime
) {
135 endTime
= fAbsoluteEndTime
;
136 if (endTime
- duration
> fAbsoluteStartTime
) {
137 realStart
= endTime
- duration
;
139 realStart
= fAbsoluteStartTime
;
143 fRangeStartTime
= realStart
;
144 fRangeDuration
= endTime
- realStart
;
147 // ------------------------------------------------------------------------
148 // MouseWheelListener
149 // ------------------------------------------------------------------------
152 public void mouseScrolled(MouseEvent event
) {
160 public void keyPressed(KeyEvent e
) {
161 if (e
.character
== '+') {
163 } else if (e
.character
== '-') {
172 public void keyReleased(KeyEvent e
) {
175 private synchronized void zoom(int nbClicks
) {
176 // Compute the new time range
177 long requestedRange
= (nbClicks
> 0) ? Math
.round(ZOOM_FACTOR
* fRangeDuration
) : (long) Math
.ceil(fRangeDuration
* (1.0 / ZOOM_FACTOR
));
179 // Distribute delta and adjust for boundaries
180 long requestedStart
= validateStart(fRangeStartTime
+ (fRangeDuration
- requestedRange
) / 2);
181 long requestedEnd
= validateEnd(requestedStart
, requestedStart
+ requestedRange
);
182 requestedStart
= validateStart(requestedEnd
- requestedRange
);
184 fHistogram
.updateTimeRange(requestedStart
, requestedEnd
);
187 private long validateStart(long start
) {
188 long realStart
= start
;
190 if (realStart
< fAbsoluteStartTime
) {
191 realStart
= fAbsoluteStartTime
;
193 if (realStart
> fAbsoluteEndTime
) {
194 realStart
= fAbsoluteEndTime
- fMinWindowSize
;
199 private long validateEnd(long start
, long end
) {
202 if (realEnd
> fAbsoluteEndTime
) {
203 realEnd
= fAbsoluteEndTime
;
205 if (realEnd
< start
+ fMinWindowSize
) {
206 realEnd
= start
+ fMinWindowSize
;
This page took 0.035688 seconds and 5 git commands to generate.