Latency: introduce latency density view
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / internal / analysis / timing / ui / views / segmentstore / density / MouseSelectionProvider.java
1 /**********************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 package org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.density;
11
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;
24
25 /**
26 * Class for providing selection with the left mouse button. It also notifies
27 * the viewer about a change of selection.
28 *
29 * @author Bernd Hufmann
30 * @author Marc-Andre Laperle
31 */
32 public class MouseSelectionProvider extends BaseMouseProvider implements MouseListener, MouseMoveListener, ICustomPaintListener {
33
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;
42
43 /**
44 * Constructor for a mouse selection provider.
45 *
46 * @param densityViewer
47 * The parent density viewer
48 */
49 public MouseSelectionProvider(AbstractSegmentStoreDensityViewer densityViewer) {
50 super(densityViewer);
51 register();
52 }
53
54 @Override
55 public void register() {
56 getChart().getPlotArea().addMouseListener(this);
57 getChart().getPlotArea().addMouseMoveListener(this);
58 ((IPlotArea) getChart().getPlotArea()).addCustomPaintListener(this);
59 }
60
61 @Override
62 public void deregister() {
63 if (!getChart().isDisposed()) {
64 getChart().getPlotArea().removeMouseListener(this);
65 getChart().getPlotArea().removeMouseMoveListener(this);
66 ((IPlotArea) getChart().getPlotArea()).removeCustomPaintListener(this);
67 }
68 }
69
70 @Override
71 public void mouseDoubleClick(@Nullable MouseEvent e) {
72 }
73
74 @Override
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;
82 } else {
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;
91 } else {
92 fStartCoordinate = selectionBegin;
93 fEndCoordinate = time;
94 }
95 }
96 fIsUpdate = true;
97 }
98 }
99
100 @Override
101 public void mouseUp(@Nullable MouseEvent e) {
102 if ((fIsUpdate)) {
103 if (fStartCoordinate > fEndCoordinate) {
104 double tmp = fStartCoordinate;
105 fStartCoordinate = fEndCoordinate;
106 fEndCoordinate = tmp;
107 }
108 if (!isEmptySelection()) {
109 getDensityViewer().select(new Range(Double.MIN_VALUE, Double.MAX_VALUE));
110 } else {
111 getDensityViewer().select(new Range(fStartCoordinate, fEndCoordinate));
112 }
113 fIsUpdate = false;
114 getChart().redraw();
115 }
116 }
117
118 @Override
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);
124 } else {
125 fEndCoordinate = xAxis.getDataCoordinate(e.x);
126 }
127 getChart().redraw();
128 }
129 }
130
131 private boolean isEmptySelection() {
132 IAxis xAxis = getChart().getAxisSet().getXAxis(0);
133 int begin = xAxis.getPixelCoordinate(fStartCoordinate);
134 int end = xAxis.getPixelCoordinate(fEndCoordinate);
135
136 return Math.abs(end - begin) > 2;
137 }
138
139 @Override
140 public void paintControl(@Nullable PaintEvent e) {
141 if (e == null || !isEmptySelection()) {
142 return;
143 }
144
145 Display display = getChart().getDisplay();
146
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);
153
154 int end = xAxis.getPixelCoordinate(fEndCoordinate);
155 e.gc.drawLine(end, 0, end, e.height);
156
157 e.gc.setAlpha(150);
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);
164 } else {
165 e.gc.fillRectangle(endX + 1, 0, beginX - endX - 1, e.height);
166 }
167 }
168 }
169
170 @Override
171 public boolean drawBehindSeries() {
172 return false;
173 }
174 }
This page took 0.036466 seconds and 5 git commands to generate.