| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2013, 2014 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 | * Contributors: |
| 10 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts; |
| 13 | |
| 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; |
| 22 | |
| 23 | /** |
| 24 | * Class for providing selection of ranges with the left mouse button. It also |
| 25 | * notifies the viewer about a change of selection. |
| 26 | * |
| 27 | * @author Bernd Hufmann |
| 28 | */ |
| 29 | public class TmfMouseSelectionProvider extends TmfBaseProvider implements MouseListener, MouseMoveListener, ICustomPaintListener { |
| 30 | |
| 31 | // ------------------------------------------------------------------------ |
| 32 | // Attributes |
| 33 | // ------------------------------------------------------------------------ |
| 34 | /** Cached start time */ |
| 35 | private long fBeginTime; |
| 36 | /** Cached end time */ |
| 37 | private long fEndTime; |
| 38 | /** Flag indicating that an update is ongoing */ |
| 39 | private boolean fIsInternalUpdate; |
| 40 | /** Flag indicating that the begin marker is dragged */ |
| 41 | private boolean fDragBeginMarker; |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Constructors |
| 45 | // ------------------------------------------------------------------------ |
| 46 | /** |
| 47 | * Default constructor |
| 48 | * |
| 49 | * @param tmfChartViewer |
| 50 | * The chart viewer reference. |
| 51 | */ |
| 52 | public TmfMouseSelectionProvider(ITmfChartTimeProvider tmfChartViewer) { |
| 53 | super(tmfChartViewer); |
| 54 | register(); |
| 55 | } |
| 56 | |
| 57 | // ------------------------------------------------------------------------ |
| 58 | // TmfBaseProvider |
| 59 | // ------------------------------------------------------------------------ |
| 60 | @Override |
| 61 | public void register() { |
| 62 | getChart().getPlotArea().addMouseListener(this); |
| 63 | getChart().getPlotArea().addMouseMoveListener(this); |
| 64 | ((IPlotArea) getChart().getPlotArea()).addCustomPaintListener(this); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void deregister() { |
| 69 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { |
| 70 | getChart().getPlotArea().removeMouseListener(this); |
| 71 | getChart().getPlotArea().removeMouseMoveListener(this); |
| 72 | ((IPlotArea) getChart().getPlotArea()).removeCustomPaintListener(this); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // ------------------------------------------------------------------------ |
| 77 | // MouseListener |
| 78 | // ------------------------------------------------------------------------ |
| 79 | @Override |
| 80 | public void mouseDoubleClick(MouseEvent e) { |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void mouseDown(MouseEvent e) { |
| 85 | if ((getChartViewer().getWindowDuration() != 0) && (e.button == 1)) { |
| 86 | if ((e.stateMask & SWT.CTRL) != 0) { |
| 87 | return; |
| 88 | } |
| 89 | fDragBeginMarker = false; |
| 90 | if ((e.stateMask & SWT.SHIFT) != SWT.SHIFT) { |
| 91 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); |
| 92 | fBeginTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); |
| 93 | fEndTime = fBeginTime; |
| 94 | } else { |
| 95 | long selectionBegin = fBeginTime; |
| 96 | long selectionEnd = fEndTime; |
| 97 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); |
| 98 | long time = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); |
| 99 | if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) { |
| 100 | fDragBeginMarker = true; |
| 101 | fBeginTime = time; |
| 102 | fEndTime = selectionEnd; |
| 103 | } else { |
| 104 | fBeginTime = selectionBegin; |
| 105 | fEndTime = time; |
| 106 | } |
| 107 | } |
| 108 | fIsInternalUpdate = true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void mouseUp(MouseEvent e) { |
| 114 | if (fIsInternalUpdate) { |
| 115 | if (fBeginTime > fEndTime) { |
| 116 | // Swap time |
| 117 | long tmp = fBeginTime; |
| 118 | fBeginTime = fEndTime; |
| 119 | fEndTime = tmp; |
| 120 | } |
| 121 | ITmfChartTimeProvider viewer = getChartViewer(); |
| 122 | viewer.updateSelectionRange(fBeginTime + viewer.getTimeOffset(), fEndTime + viewer.getTimeOffset()); |
| 123 | fIsInternalUpdate = false; |
| 124 | getChart().redraw(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // ------------------------------------------------------------------------ |
| 129 | // MouseMoveListener |
| 130 | // ------------------------------------------------------------------------ |
| 131 | @Override |
| 132 | public void mouseMove(MouseEvent e) { |
| 133 | if (fIsInternalUpdate) { |
| 134 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); |
| 135 | if (fDragBeginMarker) { |
| 136 | fBeginTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); |
| 137 | } else { |
| 138 | fEndTime = limitXDataCoordinate(xAxis.getDataCoordinate(e.x)); |
| 139 | } |
| 140 | getChart().redraw(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // ------------------------------------------------------------------------ |
| 145 | // ICustomPaintListener |
| 146 | // ------------------------------------------------------------------------ |
| 147 | @Override |
| 148 | public void paintControl(PaintEvent e) { |
| 149 | ITmfChartTimeProvider viewer = getChartViewer(); |
| 150 | |
| 151 | if (!fIsInternalUpdate) { |
| 152 | fBeginTime = viewer.getSelectionBeginTime() - viewer.getTimeOffset(); |
| 153 | fEndTime = viewer.getSelectionEndTime() - viewer.getTimeOffset(); |
| 154 | } |
| 155 | long windowStartTime = viewer.getWindowStartTime() - viewer.getTimeOffset(); |
| 156 | long windowEndTime = viewer.getWindowEndTime() - viewer.getTimeOffset(); |
| 157 | |
| 158 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); |
| 159 | e.gc.setBackground(TmfXYChartViewer.getDisplay().getSystemColor(SWT.COLOR_BLUE)); |
| 160 | e.gc.setForeground(TmfXYChartViewer.getDisplay().getSystemColor(SWT.COLOR_BLUE)); |
| 161 | e.gc.setLineStyle(SWT.LINE_SOLID); |
| 162 | if ((fBeginTime >= windowStartTime) && (fBeginTime <= windowEndTime)) { |
| 163 | int beginX = xAxis.getPixelCoordinate(fBeginTime); |
| 164 | e.gc.drawLine(beginX, 0, beginX, e.height); |
| 165 | } |
| 166 | |
| 167 | if ((fEndTime >= windowStartTime) && (fEndTime <= windowEndTime) && (fBeginTime != fEndTime)) { |
| 168 | int endX = xAxis.getPixelCoordinate(fEndTime); |
| 169 | e.gc.drawLine(endX, 0, endX, e.height); |
| 170 | } |
| 171 | e.gc.setAlpha(150); |
| 172 | if (Math.abs(fEndTime - fBeginTime) > 1) { |
| 173 | e.gc.setBackground(TmfXYChartViewer.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); |
| 174 | int beginX = xAxis.getPixelCoordinate(fBeginTime); |
| 175 | int endX = xAxis.getPixelCoordinate(fEndTime); |
| 176 | if (fEndTime > fBeginTime) { |
| 177 | e.gc.fillRectangle(beginX + 1, 0, endX - beginX - 1, e.height); |
| 178 | } else { |
| 179 | e.gc.fillRectangle(endX + 1, 0, beginX - endX - 1, e.height); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean drawBehindSeries() { |
| 186 | return false; |
| 187 | } |
| 188 | } |