| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 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 | * Francois Chouinard - Initial API and implementation |
| 11 | * Bernd Hufmann - Changed to updated histogram data model |
| 12 | * Patrick Tasse - Update for mouse wheel zoom |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.tracecompass.tmf.ui.views.histogram; |
| 16 | |
| 17 | import org.eclipse.swt.SWT; |
| 18 | import org.eclipse.swt.events.MouseEvent; |
| 19 | import org.eclipse.swt.events.PaintEvent; |
| 20 | import org.eclipse.swt.graphics.GC; |
| 21 | import org.eclipse.swt.graphics.Image; |
| 22 | import org.eclipse.swt.widgets.Composite; |
| 23 | |
| 24 | /** |
| 25 | * A histogram widget that displays the event distribution of a whole trace. |
| 26 | * <p> |
| 27 | * It also features a selected range window that can be dragged and zoomed. |
| 28 | * |
| 29 | * @version 1.1 |
| 30 | * @author Francois Chouinard |
| 31 | */ |
| 32 | public class FullTraceHistogram extends Histogram { |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | // Attributes |
| 36 | // ------------------------------------------------------------------------ |
| 37 | |
| 38 | private final HistogramZoom fZoom; |
| 39 | |
| 40 | private long fRangeStartTime = 0L; |
| 41 | private long fRangeDuration; |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Construction |
| 45 | // ------------------------------------------------------------------------ |
| 46 | |
| 47 | /** |
| 48 | * Full Constructor |
| 49 | * |
| 50 | * @param view A reference to the parent histogram view |
| 51 | * @param parent A reference to the parent composite |
| 52 | */ |
| 53 | public FullTraceHistogram(HistogramView view, Composite parent) { |
| 54 | super(view, parent); |
| 55 | fZoom = new HistogramZoom(this, getStartTime(), getTimeLimit()); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void dispose() { |
| 60 | super.dispose(); |
| 61 | } |
| 62 | |
| 63 | // ------------------------------------------------------------------------ |
| 64 | // Operations |
| 65 | // ------------------------------------------------------------------------ |
| 66 | |
| 67 | @Override |
| 68 | public void clear() { |
| 69 | fRangeStartTime = 0L; |
| 70 | fRangeDuration = 0L; |
| 71 | if (fZoom != null) { |
| 72 | fZoom.setFullRange(0L, 0L); |
| 73 | fZoom.setNewRange(0L, 0L); |
| 74 | } |
| 75 | super.clear(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets the time range of the full histogram. |
| 80 | * |
| 81 | * @param startTime A start time |
| 82 | * @param endTime A end time |
| 83 | */ |
| 84 | public void setFullRange(long startTime, long endTime) { |
| 85 | fZoom.setFullRange(startTime, endTime); |
| 86 | fZoom.setNewRange(fRangeStartTime, fRangeDuration); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the selected time range. |
| 91 | * |
| 92 | * @param startTime The histogram start time |
| 93 | * @param duration The histogram duration |
| 94 | */ |
| 95 | public void setTimeRange(long startTime, long duration) { |
| 96 | fRangeStartTime = startTime; |
| 97 | fRangeDuration = duration; |
| 98 | fZoom.setNewRange(fRangeStartTime, fRangeDuration); |
| 99 | fDataModel.complete(); |
| 100 | } |
| 101 | |
| 102 | // ------------------------------------------------------------------------ |
| 103 | // MouseListener |
| 104 | // ------------------------------------------------------------------------ |
| 105 | |
| 106 | private int fStartDelta; |
| 107 | private boolean fMouseMoved; |
| 108 | |
| 109 | @Override |
| 110 | public void mouseDown(MouseEvent event) { |
| 111 | if (fScaledData != null && fDragState == DRAG_NONE && fDataModel.getStartTime() < fDataModel.getEndTime()) { |
| 112 | if (event.button == 2 || (event.button == 1 && (event.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) { |
| 113 | fDragState = DRAG_RANGE; |
| 114 | fDragButton = event.button; |
| 115 | int center = (int) (((fRangeStartTime + fRangeDuration / 2) - fScaledData.fFirstBucketTime) / fScaledData.fBucketDuration); |
| 116 | fStartDelta = center - event.x; |
| 117 | fMouseMoved = false; |
| 118 | return; |
| 119 | } else if (event.button == 3) { |
| 120 | fDragState = DRAG_ZOOM; |
| 121 | fDragButton = event.button; |
| 122 | long time = Math.min(getTimestamp(event.x), getEndTime()); |
| 123 | if ((event.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { |
| 124 | if (time < fRangeStartTime + fRangeDuration / 2) { |
| 125 | fRangeStartTime = fRangeStartTime + fRangeDuration; |
| 126 | } |
| 127 | } else { |
| 128 | fRangeStartTime = time; |
| 129 | } |
| 130 | fRangeDuration = time - fRangeStartTime; |
| 131 | fCanvas.redraw(); |
| 132 | return; |
| 133 | } |
| 134 | } |
| 135 | super.mouseDown(event); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void mouseUp(MouseEvent event) { |
| 140 | if (fDragState == DRAG_RANGE && event.button == fDragButton) { |
| 141 | fDragState = DRAG_NONE; |
| 142 | fDragButton = 0; |
| 143 | if (!fMouseMoved) { |
| 144 | // if single click without move, center on the click |
| 145 | long startTime = getTimestamp(event.x) - fRangeDuration / 2; |
| 146 | fRangeStartTime = Math.max(getStartTime(), Math.min(getEndTime() - fRangeDuration, startTime)); |
| 147 | } |
| 148 | ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fRangeDuration); |
| 149 | return; |
| 150 | } else if (fDragState == DRAG_ZOOM && event.button == fDragButton) { |
| 151 | fDragState = DRAG_NONE; |
| 152 | fDragButton = 0; |
| 153 | if (fRangeDuration < 0) { |
| 154 | fRangeStartTime = fRangeStartTime + fRangeDuration; |
| 155 | fRangeDuration = -fRangeDuration; |
| 156 | } |
| 157 | if (fRangeDuration > 0) { |
| 158 | ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fRangeDuration); |
| 159 | } else { |
| 160 | fRangeStartTime = fZoom.getStartTime(); |
| 161 | fRangeDuration = fZoom.getDuration(); |
| 162 | fCanvas.redraw(); |
| 163 | } |
| 164 | return; |
| 165 | } |
| 166 | super.mouseUp(event); |
| 167 | } |
| 168 | |
| 169 | // ------------------------------------------------------------------------ |
| 170 | // MouseMoveListener |
| 171 | // ------------------------------------------------------------------------ |
| 172 | |
| 173 | @Override |
| 174 | public void mouseMove(MouseEvent event) { |
| 175 | if (fDragState == DRAG_RANGE) { |
| 176 | int center = event.x + fStartDelta; |
| 177 | long newStart = getTimestamp(center) - fRangeDuration / 2; |
| 178 | fRangeStartTime = Math.max(getStartTime(), Math.min(getEndTime() - fRangeDuration, newStart)); |
| 179 | fCanvas.redraw(); |
| 180 | fMouseMoved = true; |
| 181 | return; |
| 182 | } else if (fDragState == DRAG_ZOOM) { |
| 183 | long endTime = Math.max(getStartTime(), Math.min(getEndTime(), getTimestamp(event.x))); |
| 184 | fRangeDuration = endTime - fRangeStartTime; |
| 185 | fCanvas.redraw(); |
| 186 | return; |
| 187 | } |
| 188 | super.mouseMove(event); |
| 189 | } |
| 190 | |
| 191 | // ------------------------------------------------------------------------ |
| 192 | // PaintListener |
| 193 | // ------------------------------------------------------------------------ |
| 194 | |
| 195 | @Override |
| 196 | public void paintControl(PaintEvent event) { |
| 197 | super.paintControl(event); |
| 198 | |
| 199 | Image image = (Image) fCanvas.getData(IMAGE_KEY); |
| 200 | assert image != null; |
| 201 | |
| 202 | Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY); |
| 203 | GC rangeWindowGC = new GC(rangeRectangleImage); |
| 204 | |
| 205 | if ((fScaledData != null) && (fRangeDuration != 0 || fDragState == DRAG_ZOOM)) { |
| 206 | drawTimeRangeWindow(rangeWindowGC, fRangeStartTime, fRangeDuration); |
| 207 | } |
| 208 | |
| 209 | // Draws the buffer image onto the canvas. |
| 210 | event.gc.drawImage(rangeRectangleImage, 0, 0); |
| 211 | |
| 212 | rangeWindowGC.dispose(); |
| 213 | rangeRectangleImage.dispose(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get the histogram zoom |
| 218 | * @return the histogram zoom |
| 219 | */ |
| 220 | public HistogramZoom getZoom() { |
| 221 | return fZoom; |
| 222 | } |
| 223 | } |