071aee5783372c202c206857a3ac2839eaf1ecd0
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / xycharts / TmfMouseWheelZoomProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.viewers.xycharts;
14
15 import org.eclipse.swt.events.MouseEvent;
16 import org.eclipse.swt.events.MouseWheelListener;
17 import org.swtchart.IAxis;
18
19 /**
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.
22 *
23 * @author Bernd Hufmann
24 */
25 public class TmfMouseWheelZoomProvider extends TmfBaseProvider implements MouseWheelListener {
26
27 // ------------------------------------------------------------------------
28 // Constants
29 // ------------------------------------------------------------------------
30 private static final double ZOOM_FACTOR = 0.8;
31 private static final long MIN_WINDOW_SIZE = 1;
32
33 // ------------------------------------------------------------------------
34 // Constructors
35 // ------------------------------------------------------------------------
36 /**
37 * Standard constructor.
38 *
39 * @param tmfChartViewer
40 * The parent histogram object
41 */
42 public TmfMouseWheelZoomProvider(ITmfChartTimeProvider tmfChartViewer) {
43 super(tmfChartViewer);
44 register();
45 }
46
47 // ------------------------------------------------------------------------
48 // TmfBaseProvider
49 // ------------------------------------------------------------------------
50 @Override
51 public void register() {
52 getChart().getPlotArea().addMouseWheelListener(this);
53 }
54
55 @Override
56 public void deregister() {
57 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
58 getChart().getPlotArea().removeMouseWheelListener(this);
59 }
60 }
61
62 @Override
63 public void refresh() {
64 // nothing to do
65 }
66
67 // ------------------------------------------------------------------------
68 // MouseWheelListener
69 // ------------------------------------------------------------------------
70 @Override
71 public synchronized void mouseScrolled(MouseEvent event) {
72 ITmfChartTimeProvider viewer = getChartViewer();
73
74 long oldDuration = viewer.getWindowDuration();
75
76 if (oldDuration == 0) {
77 return;
78 }
79
80 // Compute the new time range
81 long newDuration = oldDuration;
82 double ratio = 1.0;
83 if (event.count > 0) {
84 ratio = ZOOM_FACTOR;
85 newDuration = Math.round(ZOOM_FACTOR * oldDuration);
86 } else {
87 ratio = 1.0 / ZOOM_FACTOR;
88 newDuration = (long) Math.ceil(oldDuration * ratio);
89 }
90 newDuration = Math.max(MIN_WINDOW_SIZE, newDuration);
91
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);
100 }
101
102 // ------------------------------------------------------------------------
103 // Helper methods
104 // ------------------------------------------------------------------------
105 private long validateWindowStartTime(long start) {
106 ITmfChartTimeProvider viewer = getChartViewer();
107 long realStart = start;
108
109 long startTime = viewer.getStartTime();
110 long endTime = viewer.getEndTime();
111
112 if (realStart < startTime) {
113 realStart = startTime;
114 }
115 if (realStart > endTime) {
116 realStart = endTime;
117 }
118 return realStart;
119 }
120
121 private long validateWindowEndTime(long start, long end) {
122 ITmfChartTimeProvider viewer = getChartViewer();
123 long realEnd = end;
124 long endTime = viewer.getEndTime();
125
126 if (realEnd > endTime) {
127 realEnd = endTime;
128 }
129 if (realEnd < start + MIN_WINDOW_SIZE) {
130 realEnd = start + MIN_WINDOW_SIZE;
131 }
132 return realEnd;
133 }
134 }
This page took 0.033913 seconds and 4 git commands to generate.