1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Changed to updated histogram data model
12 * Patrick Tasse - Update for mouse wheel zoom
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.MouseEvent
;
19 import org
.eclipse
.swt
.events
.MouseMoveListener
;
20 import org
.eclipse
.swt
.events
.PaintEvent
;
21 import org
.eclipse
.swt
.graphics
.Color
;
22 import org
.eclipse
.swt
.graphics
.GC
;
23 import org
.eclipse
.swt
.graphics
.Image
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Display
;
28 * A histogram widget that displays the event distribution of a whole trace.
30 * It also features a selected range window that can be dragged and zoomed.
33 * @author Francois Chouinard
35 public class FullTraceHistogram
extends Histogram
implements MouseMoveListener
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 private final Color fTimeRangeColor
= new Color(Display
.getCurrent(), 255, 128, 0);
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private final HistogramZoom fZoom
;
50 private long fRangeStartTime
= 0L;
51 private long fRangeDuration
;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
60 * @param view A reference to the parent histogram view
61 * @param parent A reference to the parent composite
63 public FullTraceHistogram(HistogramView view
, Composite parent
) {
65 fZoom
= new HistogramZoom(this, getStartTime(), getTimeLimit());
66 addMouseWheelListener(fZoom
);
67 fCanvas
.addMouseMoveListener(this);
71 public void dispose() {
72 fTimeRangeColor
.dispose();
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
85 fZoom
.setFullRange(0L, 0L);
86 fZoom
.setNewRange(0L, 0L);
92 * Sets the time range of the full histogram.
94 * @param startTime A start time
95 * @param endTime A end time
97 public void setFullRange(long startTime
, long endTime
) {
98 fZoom
.setFullRange(startTime
, endTime
);
102 * Sets the selected time range.
104 * @param startTime The histogram start time
105 * @param duration The histogram duration
107 public void setTimeRange(long startTime
, long duration
) {
108 fRangeStartTime
= startTime
;
109 fRangeDuration
= duration
;
110 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
111 fDataModel
.complete();
115 public void updateTimeRange(long startTime
, long endTime
) {
116 ((HistogramView
) fParentView
).updateTimeRange(startTime
, endTime
);
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
123 private boolean fMouseDown
;
124 private int fStartPosition
;
127 public void mouseDown(MouseEvent event
) {
129 fStartPosition
= event
.x
;
133 public void mouseUp(MouseEvent event
) {
136 // Check if mouse click without move; if so, just set the current event time
137 if (event
.x
== fStartPosition
) {
138 super.mouseDown(event
);
142 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 public void mouseMove(MouseEvent event
) {
156 int nbBuckets
= event
.x
- fStartPosition
;
157 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
158 long newStart
= fZoom
.getStartTime() + delta
;
159 if (newStart
< getStartTime()) {
160 newStart
= getStartTime();
162 long newEnd
= newStart
+ fZoom
.getDuration();
163 if (newEnd
> getEndTime()) {
164 newEnd
= getEndTime();
165 newStart
= newEnd
- fZoom
.getDuration();
167 fRangeStartTime
= newStart
;
168 fDataModel
.complete();
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
177 public void paintControl(PaintEvent event
) {
178 super.paintControl(event
);
180 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
181 assert image
!= null;
183 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
184 GC rangeWindowGC
= new GC(rangeRectangleImage
);
186 if ((fScaledData
!= null) && (fRangeStartTime
!= 0)) {
187 drawTimeRangeWindow(rangeWindowGC
);
190 // Draws the buffer image onto the canvas.
191 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
193 rangeWindowGC
.dispose();
194 rangeRectangleImage
.dispose();
197 private void drawTimeRangeWindow(GC imageGC
) {
199 // Map times to histogram coordinates
200 long bucketSpan
= Math
.max(fScaledData
.fBucketDuration
, 1);
201 int rangeWidth
= (int) (fRangeDuration
/ bucketSpan
);
203 int left
= (int) ((fRangeStartTime
- fDataModel
.getFirstBucketTime()) / bucketSpan
);
204 int right
= left
+ rangeWidth
;
205 int center
= (left
+ right
) / 2;
206 int height
= fCanvas
.getSize().y
;
208 // Draw the selection window
209 imageGC
.setForeground(fTimeRangeColor
);
210 imageGC
.setLineWidth(1);
211 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
212 imageGC
.drawRoundRectangle(left
, 0, rangeWidth
, height
- 1, 15, 15);
214 // Fill the selection window
215 imageGC
.setBackground(fTimeRangeColor
);
216 imageGC
.setAlpha(35);
217 imageGC
.fillRoundRectangle(left
+ 1, 1, rangeWidth
- 1, height
- 2, 15, 15);
218 imageGC
.setAlpha(255);
220 // Draw the cross hair
221 imageGC
.setForeground(fTimeRangeColor
);
222 imageGC
.setLineWidth(1);
223 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
225 int chHalfWidth
= ((rangeWidth
< 60) ?
(rangeWidth
* 2) / 3 : 40) / 2;
226 imageGC
.drawLine(center
- chHalfWidth
, height
/ 2, center
+ chHalfWidth
, height
/ 2);
227 imageGC
.drawLine(center
, (height
/ 2) - chHalfWidth
, center
, (height
/ 2) + chHalfWidth
);
231 * Get the histogram zoom
232 * @return the histogram zoom
235 public HistogramZoom
getZoom() {