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
.PaintEvent
;
20 import org
.eclipse
.swt
.graphics
.GC
;
21 import org
.eclipse
.swt
.graphics
.Image
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
25 * A histogram widget that displays the event distribution of a whole trace.
27 * It also features a selected range window that can be dragged and zoomed.
30 * @author Francois Chouinard
32 public class FullTraceHistogram
extends Histogram
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final HistogramZoom fZoom
;
40 private long fRangeStartTime
= 0L;
41 private long fRangeDuration
;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
50 * @param view A reference to the parent histogram view
51 * @param parent A reference to the parent composite
53 public FullTraceHistogram(HistogramView view
, Composite parent
) {
55 fZoom
= new HistogramZoom(this, getStartTime(), getTimeLimit());
56 addMouseWheelListener(fZoom
);
60 public void dispose() {
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
73 fZoom
.setFullRange(0L, 0L);
74 fZoom
.setNewRange(0L, 0L);
80 * Sets the time range of the full histogram.
82 * @param startTime A start time
83 * @param endTime A end time
85 public void setFullRange(long startTime
, long endTime
) {
86 fZoom
.setFullRange(startTime
, endTime
);
90 * Sets the selected time range.
92 * @param startTime The histogram start time
93 * @param duration The histogram duration
95 public void setTimeRange(long startTime
, long duration
) {
96 fRangeStartTime
= startTime
;
97 fRangeDuration
= duration
;
98 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
99 fDataModel
.complete();
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
106 private int fStartPosition
;
107 private boolean fMouseMoved
;
110 public void mouseDown(MouseEvent event
) {
111 if (fScaledData
!= null && fDragState
== DRAG_NONE
&& fDataModel
.getNbEvents() != 0) {
112 if (event
.button
== 2 || (event
.button
== 1 && (event
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
113 fDragState
= DRAG_RANGE
;
114 fDragButton
= event
.button
;
115 fStartPosition
= event
.x
;
118 } else if (event
.button
== 3) {
119 fDragState
= DRAG_ZOOM
;
120 fDragButton
= event
.button
;
121 long time
= Math
.min(getTimestamp(event
.x
), getEndTime());
122 if ((event
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
123 if (time
< fRangeStartTime
+ fRangeDuration
/ 2) {
124 fRangeStartTime
= fRangeStartTime
+ fRangeDuration
;
127 fRangeStartTime
= time
;
129 fRangeDuration
= time
- fRangeStartTime
;
134 super.mouseDown(event
);
138 public void mouseUp(MouseEvent event
) {
139 if (fDragState
== DRAG_RANGE
&& event
.button
== fDragButton
) {
140 fDragState
= DRAG_NONE
;
143 // if single click without move, center on the click
144 long startTime
= getTimestamp(event
.x
) - fRangeDuration
/ 2;
145 fRangeStartTime
= Math
.max(getStartTime(), Math
.min(getEndTime() - fRangeDuration
, startTime
));
147 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
149 } else if (fDragState
== DRAG_ZOOM
&& event
.button
== fDragButton
) {
150 fDragState
= DRAG_NONE
;
152 if (fRangeDuration
< 0) {
153 fRangeStartTime
= fRangeStartTime
+ fRangeDuration
;
154 fRangeDuration
= -fRangeDuration
;
156 if (fRangeDuration
> 0) {
157 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
159 fRangeStartTime
= fZoom
.getStartTime();
160 fRangeDuration
= fZoom
.getDuration();
165 super.mouseUp(event
);
168 // ------------------------------------------------------------------------
170 // ------------------------------------------------------------------------
173 public void mouseMove(MouseEvent event
) {
174 if (fDragState
== DRAG_RANGE
) {
175 int nbBuckets
= event
.x
- fStartPosition
;
176 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
177 long newStart
= fZoom
.getStartTime() + delta
;
178 if (newStart
< getStartTime()) {
179 newStart
= getStartTime();
181 long newEnd
= newStart
+ fZoom
.getDuration();
182 if (newEnd
> getEndTime()) {
183 newEnd
= getEndTime();
184 newStart
= newEnd
- fZoom
.getDuration();
186 fRangeStartTime
= newStart
;
190 } else if (fDragState
== DRAG_ZOOM
) {
191 long endTime
= Math
.max(getStartTime(), Math
.min(getEndTime(), getTimestamp(event
.x
)));
192 fRangeDuration
= endTime
- fRangeStartTime
;
196 super.mouseMove(event
);
199 // ------------------------------------------------------------------------
201 // ------------------------------------------------------------------------
204 public void paintControl(PaintEvent event
) {
205 super.paintControl(event
);
207 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
208 assert image
!= null;
210 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
211 GC rangeWindowGC
= new GC(rangeRectangleImage
);
213 if ((fScaledData
!= null) && (fRangeDuration
!= 0 || fDragState
== DRAG_ZOOM
)) {
214 drawTimeRangeWindow(rangeWindowGC
, fRangeStartTime
, fRangeDuration
);
217 // Draws the buffer image onto the canvas.
218 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
220 rangeWindowGC
.dispose();
221 rangeRectangleImage
.dispose();
225 * Get the histogram zoom
226 * @return the histogram zoom
229 public HistogramZoom
getZoom() {