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
.swt
.widgets
.Display
;
19 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.density
.AbstractSegmentStoreDensityViewer
;
20 import org
.swtchart
.IAxis
;
21 import org
.swtchart
.ICustomPaintListener
;
22 import org
.swtchart
.IPlotArea
;
23 import org
.swtchart
.Range
;
26 * Class for providing selection with the left mouse button. It also notifies
27 * the viewer about a change of selection.
29 * @author Bernd Hufmann
30 * @author Marc-Andre Laperle
32 public class MouseSelectionProvider
extends BaseMouseProvider
implements MouseListener
, MouseMoveListener
, ICustomPaintListener
{
34 /** Cached start coordinate */
35 private double fStartCoordinate
;
36 /** Cached end coordinate */
37 private double fEndCoordinate
;
38 /** Flag indicating that an update is ongoing */
39 private boolean fIsUpdate
;
40 /** Flag indicating that the begin marker is dragged */
41 private boolean fDragBeginMarker
;
44 * Constructor for a mouse selection provider.
46 * @param densityViewer
47 * The parent density viewer
49 public MouseSelectionProvider(AbstractSegmentStoreDensityViewer densityViewer
) {
55 public void register() {
56 getChart().getPlotArea().addMouseListener(this);
57 getChart().getPlotArea().addMouseMoveListener(this);
58 ((IPlotArea
) getChart().getPlotArea()).addCustomPaintListener(this);
62 public void deregister() {
63 if (!getChart().isDisposed()) {
64 getChart().getPlotArea().removeMouseListener(this);
65 getChart().getPlotArea().removeMouseMoveListener(this);
66 ((IPlotArea
) getChart().getPlotArea()).removeCustomPaintListener(this);
71 public void mouseDoubleClick(@Nullable MouseEvent e
) {
75 public void mouseDown(@Nullable MouseEvent e
) {
76 if (e
!= null && (e
.button
== 1)) {
77 fDragBeginMarker
= false;
78 if (((e
.stateMask
& SWT
.SHIFT
) != SWT
.SHIFT
) || (fEndCoordinate
== fStartCoordinate
)) {
79 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
80 fStartCoordinate
= xAxis
.getDataCoordinate(e
.x
);
81 fEndCoordinate
= fStartCoordinate
;
83 double selectionBegin
= fStartCoordinate
;
84 double selectionEnd
= fEndCoordinate
;
85 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
86 double time
= xAxis
.getDataCoordinate(e
.x
);
87 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
88 fDragBeginMarker
= true;
89 fStartCoordinate
= time
;
90 fEndCoordinate
= selectionEnd
;
92 fStartCoordinate
= selectionBegin
;
93 fEndCoordinate
= time
;
101 public void mouseUp(@Nullable MouseEvent e
) {
103 if (fStartCoordinate
> fEndCoordinate
) {
104 double tmp
= fStartCoordinate
;
105 fStartCoordinate
= fEndCoordinate
;
106 fEndCoordinate
= tmp
;
108 if (!isEmptySelection()) {
109 getDensityViewer().select(new Range(Double
.MIN_VALUE
, Double
.MAX_VALUE
));
111 getDensityViewer().select(new Range(fStartCoordinate
, fEndCoordinate
));
119 public void mouseMove(@Nullable MouseEvent e
) {
120 if (e
!= null && fIsUpdate
) {
121 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
122 if (fDragBeginMarker
) {
123 fStartCoordinate
= xAxis
.getDataCoordinate(e
.x
);
125 fEndCoordinate
= xAxis
.getDataCoordinate(e
.x
);
131 private boolean isEmptySelection() {
132 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
133 int begin
= xAxis
.getPixelCoordinate(fStartCoordinate
);
134 int end
= xAxis
.getPixelCoordinate(fEndCoordinate
);
136 return Math
.abs(end
- begin
) > 2;
140 public void paintControl(@Nullable PaintEvent e
) {
141 if (e
== null || !isEmptySelection()) {
145 Display display
= getChart().getDisplay();
147 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
148 e
.gc
.setBackground(display
.getSystemColor(SWT
.COLOR_BLUE
));
149 e
.gc
.setForeground(display
.getSystemColor(SWT
.COLOR_BLUE
));
150 e
.gc
.setLineStyle(SWT
.LINE_SOLID
);
151 int begin
= xAxis
.getPixelCoordinate(fStartCoordinate
);
152 e
.gc
.drawLine(begin
, 0, begin
, e
.height
);
154 int end
= xAxis
.getPixelCoordinate(fEndCoordinate
);
155 e
.gc
.drawLine(end
, 0, end
, e
.height
);
158 if (Math
.abs(fEndCoordinate
- fStartCoordinate
) > 1) {
159 e
.gc
.setBackground(display
.getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
));
160 int beginX
= xAxis
.getPixelCoordinate(fStartCoordinate
);
161 int endX
= xAxis
.getPixelCoordinate(fEndCoordinate
);
162 if (fEndCoordinate
> fStartCoordinate
) {
163 e
.gc
.fillRectangle(beginX
+ 1, 0, endX
- beginX
- 1, e
.height
);
165 e
.gc
.fillRectangle(endX
+ 1, 0, beginX
- endX
- 1, e
.height
);
171 public boolean drawBehindSeries() {