1 /**********************************************************************
2 * Copyright (c) 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
.events
.MouseEvent
;
15 import org
.eclipse
.swt
.events
.MouseListener
;
16 import org
.eclipse
.swt
.events
.MouseMoveListener
;
17 import org
.swtchart
.IAxis
;
18 import org
.swtchart
.Range
;
21 * Class for updating time ranges based on middle mouse button drag.
22 * It also notifies the viewer about a change of range.
24 * @author Bernd Hufmann
27 public class TmfMouseDragProvider
extends TmfBaseProvider
implements MouseListener
, MouseMoveListener
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
32 /** Cached start time */
33 private long fStartTime
;
34 /** Cached end time */
35 private long fEndTime
;
36 /** Flag indicating that an update is ongoing */
37 private boolean fIsUpdate
;
38 /** Cached position when mouse button was pressed */
39 private int fStartPosition
;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
47 * @param tmfChartViewer
48 * the chart viewer reference.
50 public TmfMouseDragProvider(ITmfChartTimeProvider tmfChartViewer
) {
51 super(tmfChartViewer
);
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 public void register() {
60 getChart().getPlotArea().addMouseListener(this);
61 getChart().getPlotArea().addMouseMoveListener(this);
65 public void deregister() {
66 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
67 getChart().getPlotArea().removeMouseListener(this);
68 getChart().getPlotArea().removeMouseMoveListener(this);
73 public void refresh() {
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 public void mouseDoubleClick(MouseEvent e
) {
85 public void mouseDown(MouseEvent e
) {
86 if ((getChartViewer().getWindowDuration() != 0) && (e
.button
== 2)) {
93 public void mouseUp(MouseEvent e
) {
94 if ((fIsUpdate
) && (fStartTime
!= fEndTime
)) {
95 ITmfChartTimeProvider viewer
= getChartViewer();
96 viewer
.updateWindow(fStartTime
, fEndTime
);
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
105 public void mouseMove(MouseEvent e
) {
107 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
109 ITmfChartTimeProvider viewer
= getChartViewer();
111 fStartTime
= viewer
.getWindowStartTime();
112 fEndTime
= viewer
.getWindowEndTime();
114 long startTime
= viewer
.getStartTime();
115 long endTime
= viewer
.getEndTime();
118 if (fStartPosition
> e
.x
) {
119 delta
= (long) (xAxis
.getDataCoordinate(fStartPosition
) - xAxis
.getDataCoordinate(e
.x
));
120 long max
= endTime
- fEndTime
;
121 delta
= Math
.max(0, Math
.min(delta
, max
));
122 fStartTime
= fStartTime
+ delta
;
123 fEndTime
= fEndTime
+ delta
;
124 } else if (fStartPosition
< e
.x
) {
125 delta
= (long) (xAxis
.getDataCoordinate(e
.x
) - xAxis
.getDataCoordinate(fStartPosition
));
126 long max
= fStartTime
- startTime
;
127 delta
= Math
.max(0, Math
.min(delta
, max
));
128 fStartTime
= fStartTime
- delta
;
129 fEndTime
= fEndTime
- delta
;
132 xAxis
.setRange(new Range(fStartTime
- viewer
.getTimeOffset(), fEndTime
- viewer
.getTimeOffset()));