Commit | Line | Data |
---|---|---|
73920960 | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
73920960 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 | package org.eclipse.linuxtools.tmf.ui.viewers.xycharts; | |
13 | ||
14 | import org.eclipse.swt.SWT; | |
15 | import org.eclipse.swt.events.MouseEvent; | |
16 | import org.eclipse.swt.events.MouseListener; | |
17 | import org.eclipse.swt.events.MouseMoveListener; | |
18 | import org.eclipse.swt.events.PaintEvent; | |
19 | import org.swtchart.IAxis; | |
20 | import org.swtchart.ICustomPaintListener; | |
21 | import org.swtchart.IPlotArea; | |
22 | ||
23 | /** | |
24 | * Class for providing zooming based on mouse drag with right mouse button. | |
25 | * It also notifies the viewer about a change of range. | |
26 | * | |
27 | * @author Bernd Hufmann | |
28 | * @since 3.0 | |
29 | */ | |
30 | public class TmfMouseDragZoomProvider extends TmfBaseProvider implements MouseListener, MouseMoveListener, ICustomPaintListener { | |
31 | ||
32 | // ------------------------------------------------------------------------ | |
33 | // Attributes | |
34 | // ------------------------------------------------------------------------ | |
35 | /** Cached start time */ | |
36 | private long fStartTime; | |
37 | /** Cached end time */ | |
38 | private long fEndTime; | |
39 | /** Flag indicating that an update is ongoing */ | |
40 | private boolean fIsUpdate; | |
41 | ||
42 | // ------------------------------------------------------------------------ | |
43 | // Constructors | |
44 | // ------------------------------------------------------------------------ | |
45 | /** | |
46 | * Default constructor | |
47 | * | |
48 | * @param tmfChartViewer | |
49 | * the chart viewer reference. | |
50 | */ | |
51 | public TmfMouseDragZoomProvider(ITmfChartTimeProvider tmfChartViewer) { | |
52 | super(tmfChartViewer); | |
53 | register(); | |
54 | } | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // TmfBaseProvider | |
58 | // ------------------------------------------------------------------------ | |
59 | @Override | |
60 | public void register() { | |
61 | getChart().getPlotArea().addMouseListener(this); | |
62 | getChart().getPlotArea().addMouseMoveListener(this); | |
63 | ((IPlotArea) getChart().getPlotArea()).addCustomPaintListener(this); | |
64 | } | |
65 | ||
66 | @Override | |
67 | public void deregister() { | |
68 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
69 | getChart().getPlotArea().removeMouseListener(this); | |
70 | getChart().getPlotArea().removeMouseMoveListener(this); | |
71 | ((IPlotArea) getChart().getPlotArea()).removeCustomPaintListener(this); | |
72 | } | |
73 | } | |
74 | ||
75 | @Override | |
76 | public void refresh() { | |
77 | // nothing to do | |
78 | } | |
79 | ||
80 | // ------------------------------------------------------------------------ | |
81 | // MouseListener | |
82 | // ------------------------------------------------------------------------ | |
83 | @Override | |
84 | public void mouseDoubleClick(MouseEvent e) { | |
85 | } | |
86 | ||
87 | @Override | |
88 | public void mouseDown(MouseEvent e) { | |
89 | if ((getChartViewer().getWindowDuration() != 0) && (e.button == 3)) { | |
90 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
91 | fStartTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); | |
92 | fEndTime = fStartTime; | |
93 | fIsUpdate = true; | |
94 | } | |
95 | } | |
96 | ||
97 | @Override | |
98 | public void mouseUp(MouseEvent e) { | |
99 | if ((fIsUpdate) && (fStartTime != fEndTime)) { | |
100 | if (fStartTime > fEndTime) { | |
101 | long tmp = fStartTime; | |
102 | fStartTime = fEndTime; | |
103 | fEndTime = tmp; | |
104 | } | |
105 | ITmfChartTimeProvider viewer = getChartViewer(); | |
106 | viewer.updateWindow(fStartTime + viewer.getTimeOffset(), fEndTime + viewer.getTimeOffset()); | |
107 | } | |
108 | ||
109 | if (fIsUpdate) { | |
110 | getChart().redraw(); | |
111 | } | |
112 | fIsUpdate = false; | |
113 | } | |
114 | ||
115 | // ------------------------------------------------------------------------ | |
116 | // MouseMoveListener | |
117 | // ------------------------------------------------------------------------ | |
118 | @Override | |
119 | public void mouseMove(MouseEvent e) { | |
120 | if (fIsUpdate) { | |
121 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
122 | fEndTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); | |
123 | getChart().redraw(); | |
124 | } | |
125 | } | |
126 | ||
127 | // ------------------------------------------------------------------------ | |
128 | // ICustomPaintListener | |
129 | // ------------------------------------------------------------------------ | |
130 | @Override | |
131 | public void paintControl(PaintEvent e) { | |
132 | if (fIsUpdate && (fStartTime != fEndTime)) { | |
133 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
134 | int startX = xAxis.getPixelCoordinate(fStartTime); | |
135 | int endX = xAxis.getPixelCoordinate(fEndTime); | |
136 | ||
137 | e.gc.setBackground(TmfXYChartViewer.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND)); | |
138 | if (fStartTime < fEndTime) { | |
139 | e.gc.fillRectangle(startX, 0, endX - startX, e.height); | |
140 | } else { | |
141 | e.gc.fillRectangle(endX, 0, startX - endX, e.height); | |
142 | } | |
143 | e.gc.drawLine(startX, 0, startX, e.height); | |
144 | e.gc.drawLine(endX, 0, endX, e.height); | |
145 | } | |
146 | } | |
147 | ||
148 | @Override | |
149 | public boolean drawBehindSeries() { | |
150 | return true; | |
151 | } | |
152 | } |