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
.linuxtools
.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
26 public class TmfMouseWheelZoomProvider
extends TmfBaseProvider
implements MouseWheelListener
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
31 private final static double ZOOM_FACTOR
= 0.8;
32 private final static long MIN_WINDOW_SIZE
= 1;
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 * Standard constructor.
40 * @param tmfChartViewer
41 * The parent histogram object
43 public TmfMouseWheelZoomProvider(ITmfChartTimeProvider tmfChartViewer
) {
44 super(tmfChartViewer
);
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 public void register() {
53 getChart().getPlotArea().addMouseWheelListener(this);
57 public void deregister() {
58 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
59 getChart().getPlotArea().removeMouseWheelListener(this);
64 public void refresh() {
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 public synchronized void mouseScrolled(MouseEvent event
) {
73 ITmfChartTimeProvider viewer
= getChartViewer();
75 long oldDuration
= viewer
.getWindowDuration();
77 if (oldDuration
== 0) {
81 // Compute the new time range
82 long newDuration
= oldDuration
;
84 if (event
.count
> 0) {
86 newDuration
= Math
.round(ZOOM_FACTOR
* oldDuration
);
88 ratio
= 1.0 / ZOOM_FACTOR
;
89 newDuration
= (long) Math
.ceil(oldDuration
* ratio
);
91 newDuration
= Math
.max(MIN_WINDOW_SIZE
, newDuration
);
93 // Center the zoom on mouse position, distribute new duration and adjust for boundaries.
94 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
95 long timeAtXPos
= limitXDataCoordinate(xAxis
.getDataCoordinate(event
.x
)) + viewer
.getTimeOffset();
96 // Note: ratio = newDuration/oldDuration
97 long newWindowStartTime
= timeAtXPos
- Math
.round(ratio
* (timeAtXPos
- viewer
.getWindowStartTime()));
98 long newWindowEndTime
= validateWindowEndTime(newWindowStartTime
, newWindowStartTime
+ newDuration
);
99 newWindowStartTime
= validateWindowStartTime(newWindowStartTime
);
100 viewer
.updateWindow(newWindowStartTime
, newWindowEndTime
);
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
106 private long validateWindowStartTime(long start
) {
107 ITmfChartTimeProvider viewer
= getChartViewer();
108 long realStart
= start
;
110 long startTime
= viewer
.getStartTime();
111 long endTime
= viewer
.getEndTime();
113 if (realStart
< startTime
) {
114 realStart
= startTime
;
116 if (realStart
> endTime
) {
122 private long validateWindowEndTime(long start
, long end
) {
123 ITmfChartTimeProvider viewer
= getChartViewer();
125 long endTime
= viewer
.getEndTime();
127 if (realEnd
> endTime
) {
130 if (realEnd
< start
+ MIN_WINDOW_SIZE
) {
131 realEnd
= start
+ MIN_WINDOW_SIZE
;