Commit | Line | Data |
---|---|---|
4bcc4ed6 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
4bcc4ed6 BH |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts; |
4bcc4ed6 | 14 | |
a9ad9bdc | 15 | import org.eclipse.swt.SWT; |
4bcc4ed6 BH |
16 | import org.eclipse.swt.events.MouseEvent; |
17 | import org.eclipse.swt.events.MouseWheelListener; | |
18 | import org.swtchart.IAxis; | |
a9ad9bdc | 19 | import org.swtchart.Range; |
4bcc4ed6 BH |
20 | |
21 | /** | |
22 | * Class for providing zooming based on mouse wheel. It centers the zoom on | |
23 | * mouse position. It also notifies the viewer about a change of range. | |
24 | * | |
25 | * @author Bernd Hufmann | |
4bcc4ed6 BH |
26 | */ |
27 | public class TmfMouseWheelZoomProvider extends TmfBaseProvider implements MouseWheelListener { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Constants | |
31 | // ------------------------------------------------------------------------ | |
574f43ad MK |
32 | private static final double ZOOM_FACTOR = 0.8; |
33 | private static final long MIN_WINDOW_SIZE = 1; | |
4bcc4ed6 BH |
34 | |
35 | // ------------------------------------------------------------------------ | |
36 | // Constructors | |
37 | // ------------------------------------------------------------------------ | |
38 | /** | |
39 | * Standard constructor. | |
40 | * | |
41 | * @param tmfChartViewer | |
42 | * The parent histogram object | |
43 | */ | |
44 | public TmfMouseWheelZoomProvider(ITmfChartTimeProvider tmfChartViewer) { | |
45 | super(tmfChartViewer); | |
46 | register(); | |
47 | } | |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // TmfBaseProvider | |
51 | // ------------------------------------------------------------------------ | |
52 | @Override | |
53 | public void register() { | |
54 | getChart().getPlotArea().addMouseWheelListener(this); | |
55 | } | |
56 | ||
57 | @Override | |
58 | public void deregister() { | |
59 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
60 | getChart().getPlotArea().removeMouseWheelListener(this); | |
61 | } | |
62 | } | |
63 | ||
64 | @Override | |
65 | public void refresh() { | |
66 | // nothing to do | |
67 | } | |
68 | ||
69 | // ------------------------------------------------------------------------ | |
70 | // MouseWheelListener | |
71 | // ------------------------------------------------------------------------ | |
72 | @Override | |
73 | public synchronized void mouseScrolled(MouseEvent event) { | |
74 | ITmfChartTimeProvider viewer = getChartViewer(); | |
75 | ||
a9ad9bdc MK |
76 | final int count = event.count; |
77 | if (count != 0 && (event.stateMask & SWT.CTRL) != 0) { | |
78 | final int x = event.x; | |
79 | zoom(viewer, count, x); | |
4bcc4ed6 | 80 | } |
a9ad9bdc | 81 | } |
4bcc4ed6 | 82 | |
a9ad9bdc | 83 | private void zoom(ITmfChartTimeProvider viewer, final int count, final int x) { |
4bcc4ed6 | 84 | // Compute the new time range |
a9ad9bdc MK |
85 | long newDuration = viewer.getWindowDuration(); |
86 | if (newDuration == 0 || count == 0) { | |
87 | return; | |
88 | } | |
4bcc4ed6 | 89 | double ratio = 1.0; |
a9ad9bdc | 90 | if (count > 0) { |
4bcc4ed6 | 91 | ratio = ZOOM_FACTOR; |
a9ad9bdc | 92 | newDuration = Math.round(ZOOM_FACTOR * newDuration); |
4bcc4ed6 BH |
93 | } else { |
94 | ratio = 1.0 / ZOOM_FACTOR; | |
a9ad9bdc | 95 | newDuration = (long) Math.ceil(newDuration * ratio); |
4bcc4ed6 BH |
96 | } |
97 | newDuration = Math.max(MIN_WINDOW_SIZE, newDuration); | |
98 | ||
99 | // Center the zoom on mouse position, distribute new duration and adjust for boundaries. | |
100 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
a9ad9bdc | 101 | long timeAtXPos = limitXDataCoordinate(xAxis.getDataCoordinate(x)) + viewer.getTimeOffset(); |
4bcc4ed6 BH |
102 | // Note: ratio = newDuration/oldDuration |
103 | long newWindowStartTime = timeAtXPos - Math.round(ratio * (timeAtXPos - viewer.getWindowStartTime())); | |
104 | long newWindowEndTime = validateWindowEndTime(newWindowStartTime, newWindowStartTime + newDuration); | |
105 | newWindowStartTime = validateWindowStartTime(newWindowStartTime); | |
106 | viewer.updateWindow(newWindowStartTime, newWindowEndTime); | |
a9ad9bdc MK |
107 | xAxis.setRange(new Range(newWindowStartTime - viewer.getTimeOffset(), |
108 | newWindowEndTime - viewer.getTimeOffset())); | |
4bcc4ed6 BH |
109 | } |
110 | ||
111 | // ------------------------------------------------------------------------ | |
112 | // Helper methods | |
113 | // ------------------------------------------------------------------------ | |
114 | private long validateWindowStartTime(long start) { | |
115 | ITmfChartTimeProvider viewer = getChartViewer(); | |
116 | long realStart = start; | |
117 | ||
118 | long startTime = viewer.getStartTime(); | |
119 | long endTime = viewer.getEndTime(); | |
120 | ||
121 | if (realStart < startTime) { | |
122 | realStart = startTime; | |
123 | } | |
124 | if (realStart > endTime) { | |
125 | realStart = endTime; | |
126 | } | |
127 | return realStart; | |
128 | } | |
129 | ||
130 | private long validateWindowEndTime(long start, long end) { | |
131 | ITmfChartTimeProvider viewer = getChartViewer(); | |
132 | long realEnd = end; | |
133 | long endTime = viewer.getEndTime(); | |
134 | ||
135 | if (realEnd > endTime) { | |
136 | realEnd = endTime; | |
137 | } | |
138 | if (realEnd < start + MIN_WINDOW_SIZE) { | |
139 | realEnd = start + MIN_WINDOW_SIZE; | |
140 | } | |
141 | return realEnd; | |
142 | } | |
143 | } |