1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.tracecompass
.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
64 public HistogramZoom(Histogram histogram
, long start
, long end
) {
65 fHistogram
= histogram
;
66 fAbsoluteStartTime
= start
;
67 fAbsoluteEndTime
= end
;
70 fRangeStartTime
= fAbsoluteStartTime
;
71 fRangeDuration
= fAbsoluteStartTime
+ fMinWindowSize
;
73 histogram
.addMouseWheelListener(this);
74 histogram
.addKeyListener(this);
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
82 * Get start time of the zoom window.
83 * @return the start time.
85 public synchronized long getStartTime() {
86 return fRangeStartTime
;
90 * Get the end time of the zoom window.
91 * @return the end time
93 public synchronized long getEndTime() {
94 return fRangeStartTime
+ fRangeDuration
;
98 * Get the duration of the zoom window.
99 * @return the duration of the zoom window.
101 public synchronized long getDuration() {
102 return fRangeDuration
;
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
110 * The the full time range of the histogram
112 * @param startTime the start time the histogram
113 * @param endTime the end time of the histogram
115 public synchronized void setFullRange(long startTime
, long endTime
) {
116 fAbsoluteStartTime
= startTime
;
117 fAbsoluteEndTime
= endTime
;
121 * Sets the new zoom window
122 * @param startTime the start time
123 * @param duration the duration
125 public synchronized void setNewRange(long startTime
, long duration
) {
126 long realStart
= startTime
;
128 if (realStart
< fAbsoluteStartTime
) {
129 realStart
= fAbsoluteStartTime
;
132 long endTime
= realStart
+ duration
;
133 if (endTime
> fAbsoluteEndTime
) {
134 endTime
= fAbsoluteEndTime
;
135 if (endTime
- duration
> fAbsoluteStartTime
) {
136 realStart
= endTime
- duration
;
138 realStart
= fAbsoluteStartTime
;
142 fRangeStartTime
= realStart
;
143 fRangeDuration
= endTime
- realStart
;
146 // ------------------------------------------------------------------------
147 // MouseWheelListener
148 // ------------------------------------------------------------------------
151 public void mouseScrolled(MouseEvent event
) {
156 public void keyPressed(KeyEvent e
) {
157 if (e
.character
== '+') {
159 } else if (e
.character
== '-') {
165 public void keyReleased(KeyEvent e
) {
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
));
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
);
177 fHistogram
.updateTimeRange(requestedStart
, requestedEnd
);
180 private long validateStart(long start
) {
181 long realStart
= start
;
183 if (realStart
< fAbsoluteStartTime
) {
184 realStart
= fAbsoluteStartTime
;
186 if (realStart
> fAbsoluteEndTime
) {
187 realStart
= fAbsoluteEndTime
- fMinWindowSize
;
192 private long validateEnd(long start
, long end
) {
195 if (realEnd
> fAbsoluteEndTime
) {
196 realEnd
= fAbsoluteEndTime
;
198 if (realEnd
< start
+ fMinWindowSize
) {
199 realEnd
= start
+ fMinWindowSize
;
This page took 0.135083 seconds and 6 git commands to generate.