1 /**********************************************************************
2 * Copyright (c) 2015 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
8 **********************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.views
.segmentstore
.density
;
12 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.MouseEvent
;
15 import org
.eclipse
.swt
.events
.MouseListener
;
16 import org
.eclipse
.swt
.events
.MouseMoveListener
;
17 import org
.eclipse
.swt
.events
.PaintEvent
;
18 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.density
.AbstractSegmentStoreDensityViewer
;
19 import org
.swtchart
.IAxis
;
20 import org
.swtchart
.ICustomPaintListener
;
21 import org
.swtchart
.IPlotArea
;
22 import org
.swtchart
.Range
;
25 * Class for providing zooming based on mouse drag with right mouse button.
26 * It also notifies the viewer about a change of range.
28 * @author Bernd Hufmann
29 * @author Marc-Andre Laperle
31 public class MouseDragZoomProvider
extends BaseMouseProvider
implements MouseListener
, MouseMoveListener
, ICustomPaintListener
{
33 /** Cached start coordinate */
34 private double fStartCoordinate
;
35 /** Cached end coordinate */
36 private double fEndCoordinate
;
37 /** Flag indicating that an update is ongoing */
38 private boolean fIsUpdate
;
43 * @param densityViewer
44 * the density viewer reference.
46 public MouseDragZoomProvider(AbstractSegmentStoreDensityViewer densityViewer
) {
52 public final void register() {
53 getChart().getPlotArea().addMouseListener(this);
54 getChart().getPlotArea().addMouseMoveListener(this);
55 ((IPlotArea
) getChart().getPlotArea()).addCustomPaintListener(this);
59 public final void deregister() {
60 if (!getChart().isDisposed()) {
61 getChart().getPlotArea().removeMouseListener(this);
62 getChart().getPlotArea().removeMouseMoveListener(this);
63 ((IPlotArea
) getChart().getPlotArea()).removeCustomPaintListener(this);
68 public void mouseDoubleClick(@Nullable MouseEvent e
) {
72 public void mouseDown(@Nullable MouseEvent e
) {
73 if (e
!= null && e
.button
== 3) {
74 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
75 fStartCoordinate
= xAxis
.getDataCoordinate(e
.x
);
76 fEndCoordinate
= fStartCoordinate
;
82 public void mouseUp(@Nullable MouseEvent e
) {
83 if ((fIsUpdate
) && (fStartCoordinate
!= fEndCoordinate
)) {
84 if (fStartCoordinate
> fEndCoordinate
) {
85 double tmp
= fStartCoordinate
;
86 fStartCoordinate
= fEndCoordinate
;
89 getDensityViewer().zoom(new Range(fStartCoordinate
, fEndCoordinate
));
99 public void mouseMove(@Nullable MouseEvent e
) {
100 if (e
!= null && fIsUpdate
) {
101 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
102 fEndCoordinate
= xAxis
.getDataCoordinate(e
.x
);
108 public void paintControl(@Nullable PaintEvent e
) {
109 if (e
!= null && fIsUpdate
&& (fStartCoordinate
!= fEndCoordinate
)) {
110 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
111 int startX
= xAxis
.getPixelCoordinate(fStartCoordinate
);
112 int endX
= xAxis
.getPixelCoordinate(fEndCoordinate
);
114 e
.gc
.setBackground(getChart().getDisplay().getSystemColor(SWT
.COLOR_TITLE_INACTIVE_BACKGROUND
));
115 if (fStartCoordinate
< fEndCoordinate
) {
116 e
.gc
.fillRectangle(startX
, 0, endX
- startX
, e
.height
);
118 e
.gc
.fillRectangle(endX
, 0, startX
- endX
, e
.height
);
120 e
.gc
.drawLine(startX
, 0, startX
, e
.height
);
121 e
.gc
.drawLine(endX
, 0, endX
, e
.height
);
126 public boolean drawBehindSeries() {