Commit | Line | Data |
---|---|---|
b23cbbfc MAL |
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.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; | |
23 | ||
24 | /** | |
25 | * Class for providing zooming based on mouse drag with right mouse button. | |
26 | * It also notifies the viewer about a change of range. | |
27 | * | |
28 | * @author Bernd Hufmann | |
29 | * @author Marc-Andre Laperle | |
30 | */ | |
31 | public class MouseDragZoomProvider extends BaseMouseProvider implements MouseListener, MouseMoveListener, ICustomPaintListener { | |
32 | ||
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; | |
39 | ||
40 | /** | |
41 | * Default constructor | |
42 | * | |
43 | * @param densityViewer | |
44 | * the density viewer reference. | |
45 | */ | |
46 | public MouseDragZoomProvider(AbstractSegmentStoreDensityViewer densityViewer) { | |
47 | super(densityViewer); | |
48 | register(); | |
49 | } | |
50 | ||
51 | @Override | |
a7ebfdff | 52 | public final void register() { |
b23cbbfc MAL |
53 | getChart().getPlotArea().addMouseListener(this); |
54 | getChart().getPlotArea().addMouseMoveListener(this); | |
55 | ((IPlotArea) getChart().getPlotArea()).addCustomPaintListener(this); | |
56 | } | |
57 | ||
58 | @Override | |
a7ebfdff | 59 | public final void deregister() { |
b23cbbfc MAL |
60 | if (!getChart().isDisposed()) { |
61 | getChart().getPlotArea().removeMouseListener(this); | |
62 | getChart().getPlotArea().removeMouseMoveListener(this); | |
63 | ((IPlotArea) getChart().getPlotArea()).removeCustomPaintListener(this); | |
64 | } | |
65 | } | |
66 | ||
67 | @Override | |
68 | public void mouseDoubleClick(@Nullable MouseEvent e) { | |
69 | } | |
70 | ||
71 | @Override | |
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; | |
77 | fIsUpdate = true; | |
78 | } | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void mouseUp(@Nullable MouseEvent e) { | |
83 | if ((fIsUpdate) && (fStartCoordinate != fEndCoordinate)) { | |
84 | if (fStartCoordinate > fEndCoordinate) { | |
85 | double tmp = fStartCoordinate; | |
86 | fStartCoordinate = fEndCoordinate; | |
87 | fEndCoordinate = tmp; | |
88 | } | |
89 | getDensityViewer().zoom(new Range(fStartCoordinate, fEndCoordinate)); | |
90 | } | |
91 | ||
92 | if (fIsUpdate) { | |
93 | getChart().redraw(); | |
94 | } | |
95 | fIsUpdate = false; | |
96 | } | |
97 | ||
98 | @Override | |
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); | |
103 | getChart().redraw(); | |
104 | } | |
105 | } | |
106 | ||
107 | @Override | |
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); | |
113 | ||
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); | |
117 | } else { | |
118 | e.gc.fillRectangle(endX, 0, startX - endX, e.height); | |
119 | } | |
120 | e.gc.drawLine(startX, 0, startX, e.height); | |
121 | e.gc.drawLine(endX, 0, endX, e.height); | |
122 | } | |
123 | } | |
124 | ||
125 | @Override | |
126 | public boolean drawBehindSeries() { | |
127 | return true; | |
128 | } | |
129 | } |