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 | **********************************************************************/ | |
2bdf0193 | 12 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts; |
73920960 BH |
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 | |
73920960 BH |
28 | */ |
29 | public class TmfMouseDragZoomProvider extends TmfBaseProvider implements MouseListener, MouseMoveListener, ICustomPaintListener { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | /** Cached start time */ | |
35 | private long fStartTime; | |
36 | /** Cached end time */ | |
37 | private long fEndTime; | |
38 | /** Flag indicating that an update is ongoing */ | |
39 | private boolean fIsUpdate; | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Constructors | |
43 | // ------------------------------------------------------------------------ | |
44 | /** | |
45 | * Default constructor | |
46 | * | |
47 | * @param tmfChartViewer | |
48 | * the chart viewer reference. | |
49 | */ | |
50 | public TmfMouseDragZoomProvider(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 | ((IPlotArea) getChart().getPlotArea()).addCustomPaintListener(this); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public void deregister() { | |
67 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
68 | getChart().getPlotArea().removeMouseListener(this); | |
69 | getChart().getPlotArea().removeMouseMoveListener(this); | |
70 | ((IPlotArea) getChart().getPlotArea()).removeCustomPaintListener(this); | |
71 | } | |
72 | } | |
73 | ||
74 | @Override | |
75 | public void refresh() { | |
76 | // nothing to do | |
77 | } | |
78 | ||
79 | // ------------------------------------------------------------------------ | |
80 | // MouseListener | |
81 | // ------------------------------------------------------------------------ | |
82 | @Override | |
83 | public void mouseDoubleClick(MouseEvent e) { | |
84 | } | |
85 | ||
86 | @Override | |
87 | public void mouseDown(MouseEvent e) { | |
88 | if ((getChartViewer().getWindowDuration() != 0) && (e.button == 3)) { | |
89 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
90 | fStartTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); | |
91 | fEndTime = fStartTime; | |
92 | fIsUpdate = true; | |
93 | } | |
94 | } | |
95 | ||
96 | @Override | |
97 | public void mouseUp(MouseEvent e) { | |
98 | if ((fIsUpdate) && (fStartTime != fEndTime)) { | |
99 | if (fStartTime > fEndTime) { | |
100 | long tmp = fStartTime; | |
101 | fStartTime = fEndTime; | |
102 | fEndTime = tmp; | |
103 | } | |
104 | ITmfChartTimeProvider viewer = getChartViewer(); | |
105 | viewer.updateWindow(fStartTime + viewer.getTimeOffset(), fEndTime + viewer.getTimeOffset()); | |
106 | } | |
107 | ||
108 | if (fIsUpdate) { | |
109 | getChart().redraw(); | |
110 | } | |
111 | fIsUpdate = false; | |
112 | } | |
113 | ||
114 | // ------------------------------------------------------------------------ | |
115 | // MouseMoveListener | |
116 | // ------------------------------------------------------------------------ | |
117 | @Override | |
118 | public void mouseMove(MouseEvent e) { | |
119 | if (fIsUpdate) { | |
120 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
121 | fEndTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); | |
122 | getChart().redraw(); | |
123 | } | |
124 | } | |
125 | ||
126 | // ------------------------------------------------------------------------ | |
127 | // ICustomPaintListener | |
128 | // ------------------------------------------------------------------------ | |
129 | @Override | |
130 | public void paintControl(PaintEvent e) { | |
131 | if (fIsUpdate && (fStartTime != fEndTime)) { | |
132 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
133 | int startX = xAxis.getPixelCoordinate(fStartTime); | |
134 | int endX = xAxis.getPixelCoordinate(fEndTime); | |
135 | ||
136 | e.gc.setBackground(TmfXYChartViewer.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND)); | |
137 | if (fStartTime < fEndTime) { | |
138 | e.gc.fillRectangle(startX, 0, endX - startX, e.height); | |
139 | } else { | |
140 | e.gc.fillRectangle(endX, 0, startX - endX, e.height); | |
141 | } | |
142 | e.gc.drawLine(startX, 0, startX, e.height); | |
143 | e.gc.drawLine(endX, 0, endX, e.height); | |
144 | } | |
145 | } | |
146 | ||
147 | @Override | |
148 | public boolean drawBehindSeries() { | |
149 | return true; | |
150 | } | |
151 | } |