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 selection of ranges with the left mouse button.
25 * It also notifies the viewer about a change of selection.
27 * @author Bernd Hufmann
30 public class TmfMouseSelectionProvider
extends TmfBaseProvider
implements MouseListener
, MouseMoveListener
, ICustomPaintListener
{
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
35 /** Cached start time */
36 private long fBeginTime
;
37 /** Cached end time */
38 private long fEndTime
;
39 /** Flag indicating that an update is ongoing */
40 private boolean fIsInternalUpdate
;
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
48 * @param tmfChartViewer
49 * The chart viewer reference.
51 public TmfMouseSelectionProvider(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);
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
79 public void mouseDoubleClick(MouseEvent e
) {
83 public void mouseDown(MouseEvent e
) {
84 if ((getChartViewer().getWindowDuration() != 0) && (e
.button
== 1)) {
85 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
86 fBeginTime
= limitXDataCoordinate(xAxis
.getDataCoordinate(e
.x
));
87 fEndTime
= fBeginTime
;
88 fIsInternalUpdate
= true;
93 public void mouseUp(MouseEvent e
) {
94 if (fIsInternalUpdate
) {
95 if (fBeginTime
> fEndTime
) {
97 long tmp
= fBeginTime
;
98 fBeginTime
= fEndTime
;
101 ITmfChartTimeProvider viewer
= getChartViewer();
102 viewer
.updateSelectionRange(fBeginTime
+ viewer
.getTimeOffset(), fEndTime
+ viewer
.getTimeOffset());
103 fIsInternalUpdate
= false;
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
112 public void mouseMove(MouseEvent e
) {
113 if (fIsInternalUpdate
) {
114 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
115 fEndTime
= limitXDataCoordinate(xAxis
.getDataCoordinate(e
.x
));
120 // ------------------------------------------------------------------------
121 // ICustomPaintListener
122 // ------------------------------------------------------------------------
124 public void paintControl(PaintEvent e
) {
125 ITmfChartTimeProvider viewer
= getChartViewer();
127 if (!fIsInternalUpdate
) {
128 fBeginTime
= viewer
.getSelectionBeginTime() - viewer
.getTimeOffset();
129 fEndTime
= viewer
.getSelectionEndTime() - viewer
.getTimeOffset();
131 long windowStartTime
= viewer
.getWindowStartTime() - viewer
.getTimeOffset();
132 long windowEndTime
= viewer
.getWindowEndTime() - viewer
.getTimeOffset();
134 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
135 e
.gc
.setBackground(TmfXYChartViewer
.getDisplay().getSystemColor(SWT
.COLOR_BLUE
));
136 e
.gc
.setForeground(TmfXYChartViewer
.getDisplay().getSystemColor(SWT
.COLOR_BLUE
));
137 e
.gc
.setLineStyle(SWT
.LINE_SOLID
);
138 if ((fBeginTime
>= windowStartTime
) && (fBeginTime
<= windowEndTime
)) {
139 int beginX
= xAxis
.getPixelCoordinate(fBeginTime
);
140 e
.gc
.drawLine(beginX
, 0, beginX
, e
.height
);
143 if ((fEndTime
>= windowStartTime
) && (fEndTime
<= windowEndTime
) && (fBeginTime
!= fEndTime
)) {
144 int endX
= xAxis
.getPixelCoordinate(fEndTime
);
145 e
.gc
.drawLine(endX
, 0, endX
, e
.height
);
148 if (Math
.abs(fEndTime
- fBeginTime
) > 1) {
149 e
.gc
.setBackground(TmfXYChartViewer
.getDisplay().getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
));
150 int beginX
= xAxis
.getPixelCoordinate(fBeginTime
);
151 int endX
= xAxis
.getPixelCoordinate(fEndTime
);
152 if (fEndTime
> fBeginTime
) {
153 e
.gc
.fillRectangle(beginX
+ 1, 0, endX
- beginX
- 1, e
.height
);
155 e
.gc
.fillRectangle(endX
+ 1, 0, beginX
- endX
- 1, e
.height
);
161 public boolean drawBehindSeries() {