1 /*******************************************************************************
2 * Copyright (c) 2013, 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
;
15 import org
.eclipse
.swt
.events
.MouseEvent
;
16 import org
.eclipse
.swt
.events
.MouseWheelListener
;
17 import org
.swtchart
.IAxis
;
20 * Class for providing zooming based on mouse wheel. It centers the zoom on
21 * mouse position. It also notifies the viewer about a change of range.
23 * @author Bernd Hufmann
25 public class TmfMouseWheelZoomProvider
extends TmfBaseProvider
implements MouseWheelListener
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
30 private static final double ZOOM_FACTOR
= 0.8;
31 private static final long MIN_WINDOW_SIZE
= 1;
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
37 * Standard constructor.
39 * @param tmfChartViewer
40 * The parent histogram object
42 public TmfMouseWheelZoomProvider(ITmfChartTimeProvider tmfChartViewer
) {
43 super(tmfChartViewer
);
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 public void register() {
52 getChart().getPlotArea().addMouseWheelListener(this);
56 public void deregister() {
57 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
58 getChart().getPlotArea().removeMouseWheelListener(this);
63 public void refresh() {
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 public synchronized void mouseScrolled(MouseEvent event
) {
72 ITmfChartTimeProvider viewer
= getChartViewer();
74 long oldDuration
= viewer
.getWindowDuration();
76 if (oldDuration
== 0) {
80 // Compute the new time range
81 long newDuration
= oldDuration
;
83 if (event
.count
> 0) {
85 newDuration
= Math
.round(ZOOM_FACTOR
* oldDuration
);
87 ratio
= 1.0 / ZOOM_FACTOR
;
88 newDuration
= (long) Math
.ceil(oldDuration
* ratio
);
90 newDuration
= Math
.max(MIN_WINDOW_SIZE
, newDuration
);
92 // Center the zoom on mouse position, distribute new duration and adjust for boundaries.
93 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
94 long timeAtXPos
= limitXDataCoordinate(xAxis
.getDataCoordinate(event
.x
)) + viewer
.getTimeOffset();
95 // Note: ratio = newDuration/oldDuration
96 long newWindowStartTime
= timeAtXPos
- Math
.round(ratio
* (timeAtXPos
- viewer
.getWindowStartTime()));
97 long newWindowEndTime
= validateWindowEndTime(newWindowStartTime
, newWindowStartTime
+ newDuration
);
98 newWindowStartTime
= validateWindowStartTime(newWindowStartTime
);
99 viewer
.updateWindow(newWindowStartTime
, newWindowEndTime
);
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
105 private long validateWindowStartTime(long start
) {
106 ITmfChartTimeProvider viewer
= getChartViewer();
107 long realStart
= start
;
109 long startTime
= viewer
.getStartTime();
110 long endTime
= viewer
.getEndTime();
112 if (realStart
< startTime
) {
113 realStart
= startTime
;
115 if (realStart
> endTime
) {
121 private long validateWindowEndTime(long start
, long end
) {
122 ITmfChartTimeProvider viewer
= getChartViewer();
124 long endTime
= viewer
.getEndTime();
126 if (realEnd
> endTime
) {
129 if (realEnd
< start
+ MIN_WINDOW_SIZE
) {
130 realEnd
= start
+ MIN_WINDOW_SIZE
;