| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2012 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 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
| 15 | |
| 16 | import org.eclipse.swt.SWT; |
| 17 | import org.eclipse.swt.events.MouseEvent; |
| 18 | import org.eclipse.swt.events.MouseMoveListener; |
| 19 | import org.eclipse.swt.events.PaintEvent; |
| 20 | import org.eclipse.swt.graphics.Color; |
| 21 | import org.eclipse.swt.graphics.GC; |
| 22 | import org.eclipse.swt.graphics.Image; |
| 23 | import org.eclipse.swt.widgets.Composite; |
| 24 | import org.eclipse.swt.widgets.Display; |
| 25 | |
| 26 | /** |
| 27 | * A histogram widget that displays the event distribution of a whole trace. |
| 28 | * <p> |
| 29 | * It also features a selected range window that can be dragged and zoomed. |
| 30 | * |
| 31 | * @version 1.0 |
| 32 | * @author Francois Chouinard |
| 33 | */ |
| 34 | public class FullTraceHistogram extends Histogram implements MouseMoveListener { |
| 35 | |
| 36 | // ------------------------------------------------------------------------ |
| 37 | // Constants |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | // Histogram colors |
| 41 | private final Color fTimeRangeColor = new Color(Display.getCurrent(), 255, 128, 0); |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Attributes |
| 45 | // ------------------------------------------------------------------------ |
| 46 | |
| 47 | private final HistogramZoom fZoom; |
| 48 | |
| 49 | private long fRangeStartTime; |
| 50 | private long fRangeDuration; |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | // Construction |
| 54 | // ------------------------------------------------------------------------ |
| 55 | |
| 56 | /** |
| 57 | * Standard Constructor. |
| 58 | * |
| 59 | * @param view A reference to the parent histogram view |
| 60 | * @param parent A reference to the parent composite |
| 61 | */ |
| 62 | public FullTraceHistogram(HistogramView view, Composite parent) { |
| 63 | super(view, parent); |
| 64 | fZoom = new HistogramZoom(this, fCanvas, getStartTime(), getTimeLimit()); |
| 65 | fCanvas.addMouseMoveListener(this); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void dispose() { |
| 70 | fTimeRangeColor.dispose(); |
| 71 | super.dispose(); |
| 72 | } |
| 73 | |
| 74 | // ------------------------------------------------------------------------ |
| 75 | // Operations |
| 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 | } |
| 87 | |
| 88 | /** |
| 89 | * Sets the selected time range. |
| 90 | * |
| 91 | * @param startTime A start time |
| 92 | * @param duration A window duration |
| 93 | */ |
| 94 | public void setTimeRange(long startTime, long duration) { |
| 95 | fRangeStartTime = startTime; |
| 96 | fRangeDuration = duration; |
| 97 | fZoom.setNewRange(fRangeStartTime, fRangeDuration); |
| 98 | fDataModel.complete(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void updateTimeRange(long startTime, long endTime) { |
| 103 | ((HistogramView) fParentView).updateTimeRange(startTime, endTime); |
| 104 | } |
| 105 | |
| 106 | // ------------------------------------------------------------------------ |
| 107 | // MouseListener |
| 108 | // ------------------------------------------------------------------------ |
| 109 | |
| 110 | private boolean fMouseDown; |
| 111 | private int fStartPosition; |
| 112 | |
| 113 | @Override |
| 114 | public void mouseDown(MouseEvent event) { |
| 115 | fMouseDown = true; |
| 116 | fStartPosition = event.x; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void mouseUp(MouseEvent event) { |
| 121 | if (fMouseDown) { |
| 122 | fMouseDown = false; |
| 123 | // Check if mouse click without move; if so, just set the current event time |
| 124 | if (event.x == fStartPosition) { |
| 125 | super.mouseDown(event); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fZoom.getDuration()); |
| 130 | |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // ------------------------------------------------------------------------ |
| 136 | // MouseMoveListener |
| 137 | // ------------------------------------------------------------------------ |
| 138 | |
| 139 | @Override |
| 140 | public void mouseMove(MouseEvent event) { |
| 141 | |
| 142 | if (fMouseDown) { |
| 143 | int nbBuckets = event.x - fStartPosition; |
| 144 | long delta = nbBuckets * fScaledData.fBucketDuration; |
| 145 | long newStart = fZoom.getStartTime() + delta; |
| 146 | if (newStart < getStartTime()) { |
| 147 | newStart = getStartTime(); |
| 148 | } |
| 149 | long newEnd = newStart + fZoom.getDuration(); |
| 150 | if (newEnd > getEndTime()) { |
| 151 | newEnd = getEndTime(); |
| 152 | newStart = newEnd - fZoom.getDuration(); |
| 153 | } |
| 154 | fRangeStartTime = newStart; |
| 155 | fDataModel.complete(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // ------------------------------------------------------------------------ |
| 160 | // PaintListener |
| 161 | // ------------------------------------------------------------------------ |
| 162 | |
| 163 | @Override |
| 164 | public void paintControl(PaintEvent event) { |
| 165 | super.paintControl(event); |
| 166 | |
| 167 | Image image = (Image) fCanvas.getData(IMAGE_KEY); |
| 168 | assert image != null; |
| 169 | |
| 170 | Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY); |
| 171 | GC rangeWindowGC = new GC(rangeRectangleImage); |
| 172 | |
| 173 | if ((fScaledData != null) && (fRangeStartTime != 0)) { |
| 174 | drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage); |
| 175 | } |
| 176 | |
| 177 | // Draws the buffer image onto the canvas. |
| 178 | event.gc.drawImage(rangeRectangleImage, 0, 0); |
| 179 | |
| 180 | rangeWindowGC.dispose(); |
| 181 | rangeRectangleImage.dispose(); |
| 182 | } |
| 183 | |
| 184 | private void drawTimeRangeWindow(GC imageGC, Image image) { |
| 185 | |
| 186 | // Map times to histogram coordinates |
| 187 | long bucketSpan = Math.max(fScaledData.fBucketDuration,1); |
| 188 | int rangeWidth = (int) (fRangeDuration / bucketSpan); |
| 189 | |
| 190 | int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan); |
| 191 | int right = left + rangeWidth; |
| 192 | int center = (left + right) / 2; |
| 193 | int height = fCanvas.getSize().y - 2; |
| 194 | |
| 195 | // Draw the selection window |
| 196 | imageGC.setForeground(fTimeRangeColor); |
| 197 | imageGC.setLineWidth(1); |
| 198 | imageGC.setLineStyle(SWT.LINE_SOLID); |
| 199 | imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15); |
| 200 | |
| 201 | // Fill the selection window |
| 202 | imageGC.setBackground(fTimeRangeColor); |
| 203 | imageGC.setAlpha(35); |
| 204 | imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15); |
| 205 | imageGC.setAlpha(255); |
| 206 | |
| 207 | // Draw the cross hair |
| 208 | imageGC.setForeground(fTimeRangeColor); |
| 209 | imageGC.setLineWidth(1); |
| 210 | imageGC.setLineStyle(SWT.LINE_SOLID); |
| 211 | |
| 212 | int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2; |
| 213 | imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2); |
| 214 | imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth); |
| 215 | } |
| 216 | |
| 217 | } |