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 that displays the full 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
;
50 private long fRangeDuration
;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * Standard Constructor.
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 * 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
);
89 * Sets the selected time range.
91 * @param startTime A start time
92 * @param duration A window duration
94 public void setTimeRange(long startTime
, long duration
) {
95 fRangeStartTime
= startTime
;
96 fRangeDuration
= duration
;
97 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
98 fDataModel
.complete();
102 public void updateTimeRange(long startTime
, long endTime
) {
103 ((HistogramView
) fParentView
).updateTimeRange(startTime
, endTime
);
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
110 private boolean fMouseDown
;
111 private int fStartPosition
;
114 public void mouseDown(MouseEvent event
) {
116 fStartPosition
= event
.x
;
120 public void mouseUp(MouseEvent event
) {
123 // Check if mouse click without move; if so, just set the current event time
124 if (event
.x
== fStartPosition
) {
125 super.mouseDown(event
);
129 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fZoom
.getDuration());
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
140 public void mouseMove(MouseEvent event
) {
143 int nbBuckets
= event
.x
- fStartPosition
;
144 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
145 long newStart
= fZoom
.getStartTime() + delta
;
146 if (newStart
< getStartTime()) {
147 newStart
= getStartTime();
149 long newEnd
= newStart
+ fZoom
.getDuration();
150 if (newEnd
> getEndTime()) {
151 newEnd
= getEndTime();
152 newStart
= newEnd
- fZoom
.getDuration();
154 fRangeStartTime
= newStart
;
155 fDataModel
.complete();
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 public void paintControl(PaintEvent event
) {
165 super.paintControl(event
);
167 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
168 assert image
!= null;
170 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
171 GC rangeWindowGC
= new GC(rangeRectangleImage
);
173 if ((fScaledData
!= null) && (fRangeStartTime
!= 0)) {
174 drawTimeRangeWindow(rangeWindowGC
, rangeRectangleImage
);
177 // Draws the buffer image onto the canvas.
178 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
180 rangeWindowGC
.dispose();
181 rangeRectangleImage
.dispose();
184 private void drawTimeRangeWindow(GC imageGC
, Image image
) {
186 // Map times to histogram coordinates
187 long bucketSpan
= Math
.max(fScaledData
.fBucketDuration
,1);
188 int rangeWidth
= (int) (fRangeDuration
/ bucketSpan
);
190 int left
= (int) ((fRangeStartTime
- fDataModel
.getFirstBucketTime()) / bucketSpan
);
191 int right
= left
+ rangeWidth
;
192 int center
= (left
+ right
) / 2;
193 int height
= fCanvas
.getSize().y
- 2;
195 // Draw the selection window
196 imageGC
.setForeground(fTimeRangeColor
);
197 imageGC
.setLineWidth(1);
198 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
199 imageGC
.drawRoundRectangle(left
, 0, rangeWidth
, height
- 1, 15, 15);
201 // Fill the selection window
202 imageGC
.setBackground(fTimeRangeColor
);
203 imageGC
.setAlpha(35);
204 imageGC
.fillRoundRectangle(left
+ 1, 1, rangeWidth
- 1, height
- 2, 15, 15);
205 imageGC
.setAlpha(255);
207 // Draw the cross hair
208 imageGC
.setForeground(fTimeRangeColor
);
209 imageGC
.setLineWidth(1);
210 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
212 int chHalfWidth
= ((rangeWidth
< 60) ?
(rangeWidth
* 2) / 3 : 40) / 2;
213 imageGC
.drawLine(center
- chHalfWidth
, height
/ 2, center
+ chHalfWidth
, height
/ 2);
214 imageGC
.drawLine(center
, (height
/ 2) - chHalfWidth
, center
, (height
/ 2) + chHalfWidth
);