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