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 * Francois Chouinard - Initial API and implementation
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
.MouseMoveListener
;
20 import org
.eclipse
.swt
.events
.PaintEvent
;
21 import org
.eclipse
.swt
.graphics
.Color
;
22 import org
.eclipse
.swt
.graphics
.GC
;
23 import org
.eclipse
.swt
.graphics
.Image
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Display
;
28 * <b><u>FullTraceHistogram</u></b>
30 * A histogram that displays the full trace.
32 * It also features a selected range window that can be dragged and zoomed.
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 // ------------------------------------------------------------------------
56 public FullTraceHistogram(HistogramView view
, Composite parent
) {
58 fZoom
= new HistogramZoom(this, fCanvas
, getStartTime(), getTimeLimit());
59 fCanvas
.addMouseMoveListener(this);
63 public void dispose() {
64 fTimeRangeColor
.dispose();
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 public void setFullRange(long startTime
, long endTime
) {
73 fZoom
.setFullRange(startTime
, endTime
);
76 public void setTimeRange(long startTime
, long duration
) {
77 fRangeStartTime
= startTime
;
78 fRangeDuration
= duration
;
79 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
80 fDataModel
.complete();
84 public void updateTimeRange(long startTime
, long endTime
) {
85 ((HistogramView
) fParentView
).updateTimeRange(startTime
, endTime
);
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
92 private boolean fMouseDown
;
93 private int fStartPosition
;
96 public void mouseDown(MouseEvent event
) {
97 // Check if we are outside the time range; if so, just set the current
99 long timestamp
= getTimestamp(event
.x
);
100 if ((timestamp
< fZoom
.getStartTime()) || (timestamp
> fZoom
.getEndTime())) {
101 super.mouseDown(event
);
105 // Otherwise start moving the range window
107 fStartPosition
= event
.x
;
111 public void mouseUp(MouseEvent event
) {
114 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fZoom
.getDuration());
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
123 public void mouseMove(MouseEvent event
) {
125 int nbBuckets
= event
.x
- fStartPosition
;
126 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
127 long newStart
= fZoom
.getStartTime() + delta
;
128 if (newStart
< getStartTime()) {
129 newStart
= getStartTime();
131 long newEnd
= newStart
+ fZoom
.getDuration();
132 if (newEnd
> getEndTime()) {
133 newEnd
= getEndTime();
134 newStart
= newEnd
- fZoom
.getDuration();
136 fRangeStartTime
= newStart
;
137 fDataModel
.complete();
141 // ------------------------------------------------------------------------
143 // ------------------------------------------------------------------------
146 public void paintControl(PaintEvent event
) {
147 super.paintControl(event
);
149 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
150 assert image
!= null;
152 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
153 GC rangeWindowGC
= new GC(rangeRectangleImage
);
155 if ((fScaledData
!= null) && (fRangeStartTime
!= 0)) {
156 drawTimeRangeWindow(rangeWindowGC
, rangeRectangleImage
);
159 // Draws the buffer image onto the canvas.
160 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
162 rangeWindowGC
.dispose();
163 rangeRectangleImage
.dispose();
166 private void drawTimeRangeWindow(GC imageGC
, Image image
) {
168 // Map times to histogram coordinates
169 long bucketSpan
= Math
.max(fScaledData
.fBucketDuration
,1);
170 int rangeWidth
= (int) (fRangeDuration
/ bucketSpan
);
172 int left
= (int) ((fRangeStartTime
- fDataModel
.getFirstBucketTime()) / bucketSpan
);
173 int right
= left
+ rangeWidth
;
174 int center
= (left
+ right
) / 2;
175 int height
= fCanvas
.getSize().y
- 2;
177 // Draw the selection window
178 imageGC
.setForeground(fTimeRangeColor
);
179 imageGC
.setLineWidth(1);
180 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
181 imageGC
.drawRoundRectangle(left
, 0, rangeWidth
, height
- 1, 15, 15);
183 // Fill the selection window
184 imageGC
.setBackground(fTimeRangeColor
);
185 imageGC
.setAlpha(35);
186 imageGC
.fillRoundRectangle(left
+ 1, 1, rangeWidth
- 1, height
- 2, 15, 15);
187 imageGC
.setAlpha(255);
189 // Draw the cross hair
190 imageGC
.setForeground(fTimeRangeColor
);
191 imageGC
.setLineWidth(1);
192 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
194 int chHalfWidth
= ((rangeWidth
< 60) ?
(rangeWidth
* 2) / 3 : 40) / 2;
195 imageGC
.drawLine(center
- chHalfWidth
, height
/ 2, center
+ chHalfWidth
, height
/ 2);
196 imageGC
.drawLine(center
, (height
/ 2) - chHalfWidth
, center
, (height
/ 2) + chHalfWidth
);