1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
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
;
27 * A histogram widget that displays the event distribution of a whole trace.
29 * It also features a selected range window that can be dragged and zoomed.
32 * @author Francois Chouinard
34 public class FullTraceHistogram
extends Histogram
implements MouseMoveListener
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 private final Color fTimeRangeColor
= new Color(Display
.getCurrent(), 255, 128, 0);
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private final HistogramZoom fZoom
;
49 private long fRangeStartTime
= 0L;
50 private long fRangeDuration
;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
59 * @param view A reference to the parent histogram view
60 * @param parent A reference to the parent composite
62 public FullTraceHistogram(HistogramView view
, Composite parent
) {
64 fZoom
= new HistogramZoom(this, fCanvas
, getStartTime(), getTimeLimit());
65 fCanvas
.addMouseMoveListener(this);
69 public void dispose() {
70 fTimeRangeColor
.dispose();
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * @see org.eclipse.linuxtools.tmf.ui.views.histogram.Histogram#clear()
86 fZoom
.setFullRange(0L, 0L);
87 fZoom
.setNewRange(0L, 0L);
94 * Sets the time range of the full histogram.
96 * @param startTime A start time
97 * @param endTime A end time
99 public void setFullRange(long startTime
, long endTime
) {
100 fZoom
.setFullRange(startTime
, endTime
);
104 * Sets the selected time range.
106 * @param startTime The histogram start time
107 * @param duration The histogram duration
109 public void setTimeRange(long startTime
, long duration
) {
110 fRangeStartTime
= startTime
;
111 fRangeDuration
= duration
;
112 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
113 fDataModel
.complete();
117 * @see org.eclipse.linuxtools.tmf.ui.views.histogram.Histogram#updateTimeRange(long, long)
120 public void updateTimeRange(long startTime
, long endTime
) {
121 ((HistogramView
) fParentView
).updateTimeRange(startTime
, endTime
);
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 private boolean fMouseDown
;
129 private int fStartPosition
;
132 public void mouseDown(MouseEvent event
) {
134 fStartPosition
= event
.x
;
138 public void mouseUp(MouseEvent event
) {
141 // Check if mouse click without move; if so, just set the current event time
142 if (event
.x
== fStartPosition
) {
143 super.mouseDown(event
);
147 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fRangeDuration
);
153 // ------------------------------------------------------------------------
155 // ------------------------------------------------------------------------
158 public void mouseMove(MouseEvent event
) {
161 int nbBuckets
= event
.x
- fStartPosition
;
162 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
163 long newStart
= fZoom
.getStartTime() + delta
;
164 if (newStart
< getStartTime()) {
165 newStart
= getStartTime();
167 long newEnd
= newStart
+ fZoom
.getDuration();
168 if (newEnd
> getEndTime()) {
169 newEnd
= getEndTime();
170 newStart
= newEnd
- fZoom
.getDuration();
172 fRangeStartTime
= newStart
;
173 fDataModel
.complete();
177 // ------------------------------------------------------------------------
179 // ------------------------------------------------------------------------
182 public void paintControl(PaintEvent event
) {
183 super.paintControl(event
);
185 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
186 assert image
!= null;
188 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
189 GC rangeWindowGC
= new GC(rangeRectangleImage
);
191 if ((fScaledData
!= null) && (fRangeStartTime
!= 0)) {
192 drawTimeRangeWindow(rangeWindowGC
);
195 // Draws the buffer image onto the canvas.
196 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
198 rangeWindowGC
.dispose();
199 rangeRectangleImage
.dispose();
202 private void drawTimeRangeWindow(GC imageGC
) {
204 // Map times to histogram coordinates
205 long bucketSpan
= Math
.max(fScaledData
.fBucketDuration
, 1);
206 int rangeWidth
= (int) (fRangeDuration
/ bucketSpan
);
208 int left
= (int) ((fRangeStartTime
- fDataModel
.getFirstBucketTime()) / bucketSpan
);
209 int right
= left
+ rangeWidth
;
210 int center
= (left
+ right
) / 2;
211 int height
= fCanvas
.getSize().y
;
213 // Draw the selection window
214 imageGC
.setForeground(fTimeRangeColor
);
215 imageGC
.setLineWidth(1);
216 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
217 imageGC
.drawRoundRectangle(left
, 0, rangeWidth
, height
- 1, 15, 15);
219 // Fill the selection window
220 imageGC
.setBackground(fTimeRangeColor
);
221 imageGC
.setAlpha(35);
222 imageGC
.fillRoundRectangle(left
+ 1, 1, rangeWidth
- 1, height
- 2, 15, 15);
223 imageGC
.setAlpha(255);
225 // Draw the cross hair
226 imageGC
.setForeground(fTimeRangeColor
);
227 imageGC
.setLineWidth(1);
228 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
230 int chHalfWidth
= ((rangeWidth
< 60) ?
(rangeWidth
* 2) / 3 : 40) / 2;
231 imageGC
.drawLine(center
- chHalfWidth
, height
/ 2, center
+ chHalfWidth
, height
/ 2);
232 imageGC
.drawLine(center
, (height
/ 2) - chHalfWidth
, center
, (height
/ 2) + chHalfWidth
);