Commit | Line | Data |
---|---|---|
eee2beeb BH |
1 | /********************************************************************** |
2 | * Copyright (c) 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 | package org.eclipse.linuxtools.tmf.ui.viewers.xycharts; | |
13 | ||
14 | import org.eclipse.swt.events.MouseEvent; | |
15 | import org.eclipse.swt.events.MouseListener; | |
16 | import org.eclipse.swt.events.MouseMoveListener; | |
17 | import org.swtchart.IAxis; | |
18 | import org.swtchart.Range; | |
19 | ||
20 | /** | |
21 | * Class for updating time ranges based on middle mouse button drag. | |
22 | * It also notifies the viewer about a change of range. | |
23 | * | |
24 | * @author Bernd Hufmann | |
25 | * @since 3.0 | |
26 | */ | |
27 | public class TmfMouseDragProvider extends TmfBaseProvider implements MouseListener, MouseMoveListener { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Attributes | |
31 | // ------------------------------------------------------------------------ | |
32 | /** Cached start time */ | |
33 | private long fStartTime; | |
34 | /** Cached end time */ | |
35 | private long fEndTime; | |
36 | /** Flag indicating that an update is ongoing */ | |
37 | private boolean fIsUpdate; | |
38 | /** Cached position when mouse button was pressed */ | |
39 | private int fStartPosition; | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Constructors | |
43 | // ------------------------------------------------------------------------ | |
44 | /** | |
45 | * Default constructor | |
46 | * | |
47 | * @param tmfChartViewer | |
48 | * the chart viewer reference. | |
49 | */ | |
50 | public TmfMouseDragProvider(ITmfChartTimeProvider tmfChartViewer) { | |
51 | super(tmfChartViewer); | |
52 | register(); | |
53 | } | |
54 | ||
55 | // ------------------------------------------------------------------------ | |
56 | // TmfBaseProvider | |
57 | // ------------------------------------------------------------------------ | |
58 | @Override | |
59 | public void register() { | |
60 | getChart().getPlotArea().addMouseListener(this); | |
61 | getChart().getPlotArea().addMouseMoveListener(this); | |
62 | } | |
63 | ||
64 | @Override | |
65 | public void deregister() { | |
66 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
67 | getChart().getPlotArea().removeMouseListener(this); | |
68 | getChart().getPlotArea().removeMouseMoveListener(this); | |
69 | } | |
70 | } | |
71 | ||
72 | @Override | |
73 | public void refresh() { | |
74 | // nothing to do | |
75 | } | |
76 | ||
77 | // ------------------------------------------------------------------------ | |
78 | // MouseListener | |
79 | // ------------------------------------------------------------------------ | |
80 | @Override | |
81 | public void mouseDoubleClick(MouseEvent e) { | |
82 | } | |
83 | ||
84 | @Override | |
85 | public void mouseDown(MouseEvent e) { | |
86 | if ((getChartViewer().getWindowDuration() != 0) && (e.button == 2)) { | |
87 | fStartPosition = e.x; | |
88 | fIsUpdate = true; | |
89 | } | |
90 | } | |
91 | ||
92 | @Override | |
93 | public void mouseUp(MouseEvent e) { | |
94 | if ((fIsUpdate) && (fStartTime != fEndTime)) { | |
95 | ITmfChartTimeProvider viewer = getChartViewer(); | |
96 | viewer.updateWindow(fStartTime, fEndTime); | |
97 | } | |
98 | fIsUpdate = false; | |
99 | } | |
100 | ||
101 | // ------------------------------------------------------------------------ | |
102 | // MouseMoveListener | |
103 | // ------------------------------------------------------------------------ | |
104 | @Override | |
105 | public void mouseMove(MouseEvent e) { | |
106 | if (fIsUpdate) { | |
107 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
108 | ||
109 | ITmfChartTimeProvider viewer = getChartViewer(); | |
110 | ||
111 | fStartTime = viewer.getWindowStartTime(); | |
112 | fEndTime = viewer.getWindowEndTime(); | |
113 | ||
114 | long startTime = viewer.getStartTime(); | |
115 | long endTime = viewer.getEndTime(); | |
116 | ||
117 | long delta = 0; | |
118 | if (fStartPosition > e.x) { | |
119 | delta = (long) (xAxis.getDataCoordinate(fStartPosition) - xAxis.getDataCoordinate(e.x)); | |
120 | long max = endTime - fEndTime; | |
121 | delta = Math.max(0, Math.min(delta, max)); | |
122 | fStartTime = fStartTime + delta; | |
123 | fEndTime = fEndTime + delta; | |
124 | } else if (fStartPosition < e.x) { | |
125 | delta = (long) (xAxis.getDataCoordinate(e.x) - xAxis.getDataCoordinate(fStartPosition)); | |
126 | long max = fStartTime - startTime; | |
127 | delta = Math.max(0, Math.min(delta, max)); | |
128 | fStartTime = fStartTime - delta; | |
129 | fEndTime = fEndTime - delta; | |
130 | } | |
131 | ||
132 | xAxis.setRange(new Range(fStartTime - viewer.getTimeOffset(), fEndTime - viewer.getTimeOffset())); | |
133 | } | |
134 | } | |
135 | } |