1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
;
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
;
24 * Class for providing zooming based on mouse drag with right mouse button.
25 * It also notifies the viewer about a change of range.
27 * @author Bernd Hufmann
30 public class TmfMouseDragZoomProvider
extends TmfBaseProvider
implements MouseListener
, MouseMoveListener
, ICustomPaintListener
{
32 // ------------------------------------------------------------------------
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
;
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
48 * @param tmfChartViewer
49 * the chart viewer reference.
51 public TmfMouseDragZoomProvider(ITmfChartTimeProvider tmfChartViewer
) {
52 super(tmfChartViewer
);
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 public void register() {
61 getChart().getPlotArea().addMouseListener(this);
62 getChart().getPlotArea().addMouseMoveListener(this);
63 ((IPlotArea
) getChart().getPlotArea()).addCustomPaintListener(this);
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);
76 public void refresh() {
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 public void mouseDoubleClick(MouseEvent e
) {
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
;
98 public void mouseUp(MouseEvent e
) {
99 if ((fIsUpdate
) && (fStartTime
!= fEndTime
)) {
100 if (fStartTime
> fEndTime
) {
101 long tmp
= fStartTime
;
102 fStartTime
= fEndTime
;
105 ITmfChartTimeProvider viewer
= getChartViewer();
106 viewer
.updateWindow(fStartTime
+ viewer
.getTimeOffset(), fEndTime
+ viewer
.getTimeOffset());
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
119 public void mouseMove(MouseEvent e
) {
121 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
122 fEndTime
= limitXDataCoordinate(xAxis
.getDataCoordinate(e
.x
));
127 // ------------------------------------------------------------------------
128 // ICustomPaintListener
129 // ------------------------------------------------------------------------
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
);
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
);
141 e
.gc
.fillRectangle(endX
, 0, startX
- endX
, e
.height
);
143 e
.gc
.drawLine(startX
, 0, startX
, e
.height
);
144 e
.gc
.drawLine(endX
, 0, endX
, e
.height
);
149 public boolean drawBehindSeries() {