1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.tracecompass
.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());
59 public void dispose() {
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
72 fZoom
.setFullRange(0L, 0L);
73 fZoom
.setNewRange(0L, 0L);
79 * Sets the time range of the full histogram.
81 * @param startTime A start time
82 * @param endTime A end time
84 public void setFullRange(long startTime
, long endTime
) {
85 fZoom
.setFullRange(startTime
, endTime
);
86 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
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 fStartDelta
;
107 private boolean fMouseMoved
;
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
;
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
;
128 fRangeStartTime
= time
;
130 fRangeDuration
= time
- fRangeStartTime
;
135 super.mouseDown(event
);
139 public void mouseUp(MouseEvent event
) {
140 if (fDragState
== DRAG_RANGE
&& event
.button
== fDragButton
) {
141 fDragState
= DRAG_NONE
;
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
));
148 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
150 } else if (fDragState
== DRAG_ZOOM
&& event
.button
== fDragButton
) {
151 fDragState
= DRAG_NONE
;
153 if (fRangeDuration
< 0) {
154 fRangeStartTime
= fRangeStartTime
+ fRangeDuration
;
155 fRangeDuration
= -fRangeDuration
;
157 if (fRangeDuration
> 0) {
158 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
160 fRangeStartTime
= fZoom
.getStartTime();
161 fRangeDuration
= fZoom
.getDuration();
166 super.mouseUp(event
);
169 // ------------------------------------------------------------------------
171 // ------------------------------------------------------------------------
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
));
182 } else if (fDragState
== DRAG_ZOOM
) {
183 long endTime
= Math
.max(getStartTime(), Math
.min(getEndTime(), getTimestamp(event
.x
)));
184 fRangeDuration
= endTime
- fRangeStartTime
;
188 super.mouseMove(event
);
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
196 public void paintControl(PaintEvent event
) {
197 super.paintControl(event
);
199 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
200 assert image
!= null;
202 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
203 GC rangeWindowGC
= new GC(rangeRectangleImage
);
205 if ((fScaledData
!= null) && (fRangeDuration
!= 0 || fDragState
== DRAG_ZOOM
)) {
206 drawTimeRangeWindow(rangeWindowGC
, fRangeStartTime
, fRangeDuration
);
209 // Draws the buffer image onto the canvas.
210 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
212 rangeWindowGC
.dispose();
213 rangeRectangleImage
.dispose();
217 * Get the histogram zoom
218 * @return the histogram zoom
220 public HistogramZoom
getZoom() {