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 * Francois Chouinard - Moved from LTTng to TMF
13 * Patrick Tasse - Update for mouse wheel zoom
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.histogram
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.events
.MouseEvent
;
20 import org
.eclipse
.swt
.events
.PaintEvent
;
21 import org
.eclipse
.swt
.graphics
.GC
;
22 import org
.eclipse
.swt
.graphics
.Image
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
26 * A basic histogram widget that displays the event distribution of a specific time range of a trace.
27 * It has the following additional features:
29 * <li>zoom in: mouse wheel up (or forward)
30 * <li>zoom out: mouse wheel down (or backward)
34 * @author Francois Chouinard
36 public class TimeRangeHistogram
extends Histogram
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 private HistogramZoom fZoom
= null;
44 private long fRangeStartTime
= 0L;
45 private long fRangeDuration
;
46 private long fFullRangeStartTime
= 0L;
47 private long fFullRangeEndTime
= 0L;
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
54 * @param view The parent histogram view
55 * @param parent The parent composite
57 public TimeRangeHistogram(HistogramView view
, Composite parent
) {
59 fZoom
= new HistogramZoom(this, getStartTime(), getTimeLimit());
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 public synchronized void clear() {
70 fFullRangeStartTime
= 0L;
71 fFullRangeEndTime
= 0L;
74 fZoom
.setFullRange(0L, 0L);
75 fZoom
.setNewRange(0L, 0L);
81 * Sets the time range of the histogram
82 * @param startTime The start time
83 * @param duration The duration of the time range
85 public synchronized void setTimeRange(long startTime
, long duration
) {
86 fRangeStartTime
= startTime
;
87 fRangeDuration
= duration
;
88 fZoom
.setNewRange(startTime
, duration
);
89 if (getDataModel().getNbEvents() == 0) {
90 getDataModel().setTimeRange(startTime
, startTime
+ duration
);
91 getDataModel().setEndTime(startTime
+ duration
);
96 * Sets the full time range of the whole trace.
97 * @param startTime The start time
98 * @param endTime The end time
100 public void setFullRange(long startTime
, long endTime
) {
101 fFullRangeStartTime
= startTime
;
102 fFullRangeEndTime
= endTime
;
103 fZoom
.setFullRange(startTime
, endTime
);
104 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
111 private int fStartPosition
;
112 private int fMinOffset
;
113 private int fMaxOffset
;
116 public void mouseDown(MouseEvent event
) {
117 if (fScaledData
!= null && fDragState
== DRAG_NONE
&& fDataModel
.getStartTime() < fDataModel
.getEndTime()) {
118 if (event
.button
== 2 || (event
.button
== 1 && (event
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
119 fDragState
= DRAG_RANGE
;
120 fDragButton
= event
.button
;
121 fStartPosition
= event
.x
;
122 long maxOffset
= (fRangeStartTime
- fFullRangeStartTime
) / fScaledData
.fBucketDuration
;
123 long minOffset
= (fRangeStartTime
+ fRangeDuration
- fFullRangeEndTime
) / fScaledData
.fBucketDuration
;
124 fMaxOffset
= (int) Math
.max(Integer
.MIN_VALUE
, Math
.min(Integer
.MAX_VALUE
, maxOffset
));
125 fMinOffset
= (int) Math
.max(Integer
.MIN_VALUE
, Math
.min(Integer
.MAX_VALUE
, minOffset
));
127 } else if (event
.button
== 3) {
128 fDragState
= DRAG_ZOOM
;
129 fDragButton
= event
.button
;
130 fRangeStartTime
= Math
.min(getTimestamp(event
.x
), getEndTime());
136 super.mouseDown(event
);
140 public void mouseUp(MouseEvent event
) {
141 if (fDragState
== DRAG_RANGE
&& event
.button
== fDragButton
) {
142 fDragState
= DRAG_NONE
;
144 if (event
.x
!= fStartPosition
) {
145 int nbBuckets
= event
.x
- fStartPosition
;
146 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
147 long startTime
= fRangeStartTime
- delta
;
148 fRangeStartTime
= Math
.max(fFullRangeStartTime
, Math
.min(fFullRangeEndTime
- fRangeDuration
, startTime
));
149 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
153 } else if (fDragState
== DRAG_ZOOM
&& event
.button
== fDragButton
) {
154 fDragState
= DRAG_NONE
;
156 if (fRangeDuration
< 0) {
157 fRangeStartTime
= fRangeStartTime
+ fRangeDuration
;
158 fRangeDuration
= -fRangeDuration
;
160 if (fRangeDuration
> 0) {
161 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
163 fRangeStartTime
= fZoom
.getStartTime();
164 fRangeDuration
= fZoom
.getDuration();
169 super.mouseUp(event
);
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
177 public void mouseMove(MouseEvent event
) {
178 if (fDragState
== DRAG_RANGE
) {
179 int offset
= Math
.max(fMinOffset
, Math
.min(fMaxOffset
, event
.x
- fStartPosition
));
183 } else if (fDragState
== DRAG_ZOOM
) {
184 long endTime
= Math
.max(getStartTime(), Math
.min(getEndTime(), getTimestamp(event
.x
)));
185 fRangeDuration
= endTime
- fRangeStartTime
;
189 super.mouseMove(event
);
192 // ------------------------------------------------------------------------
194 // ------------------------------------------------------------------------
197 public void paintControl(PaintEvent event
) {
198 super.paintControl(event
);
200 if (fDragState
== DRAG_ZOOM
) {
201 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
202 assert image
!= null;
204 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
205 GC rangeWindowGC
= new GC(rangeRectangleImage
);
207 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();