1 /*******************************************************************************
2 * Copyright (c) 2011 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.MouseEvent
;
17 import org
.eclipse
.swt
.events
.MouseMoveListener
;
18 import org
.eclipse
.swt
.events
.PaintEvent
;
19 import org
.eclipse
.swt
.graphics
.Color
;
20 import org
.eclipse
.swt
.graphics
.GC
;
21 import org
.eclipse
.swt
.graphics
.Image
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Display
;
26 * <b><u>FullTraceHistogram</u></b>
28 * A histogram that displays the full trace.
30 * It also features a selected range window that can be dragged and zoomed.
32 public class FullTraceHistogram
extends Histogram
implements MouseMoveListener
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
39 private final Color fTimeRangeColor
= new Color(Display
.getCurrent(), 255, 128, 0);
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private final HistogramZoom fZoom
;
47 private long fRangeStartTime
;
48 private long fRangeDuration
;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 public FullTraceHistogram(HistogramView view
, Composite parent
) {
56 fZoom
= new HistogramZoom(this, fCanvas
, getStartTime(), getTimeLimit());
57 fCanvas
.addMouseMoveListener(this);
61 public void dispose() {
62 fTimeRangeColor
.dispose();
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 public void setFullRange(long startTime
, long endTime
) {
71 fZoom
.setFullRange(startTime
, endTime
);
74 public void setTimeRange(long startTime
, long duration
) {
75 fRangeStartTime
= startTime
;
76 fRangeDuration
= duration
;
77 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
82 public void updateTimeRange(long startTime
, long endTime
) {
83 ((HistogramView
) fParentView
).updateTimeRange(startTime
, endTime
);
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
90 private boolean fMouseDown
;
91 private int fStartPosition
;
94 public void mouseDown(MouseEvent event
) {
95 // Check if we are outside the time range; if so, just set the current
97 long timestamp
= getTimestamp(event
.x
);
98 if (timestamp
< fZoom
.getStartTime() || timestamp
> fZoom
.getEndTime()) {
99 super.mouseDown(event
);
103 // Otherwise start moving the range window
105 fStartPosition
= event
.x
;
109 public void mouseUp(MouseEvent event
) {
112 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fZoom
.getDuration());
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
121 public void mouseMove(MouseEvent event
) {
123 int nbBuckets
= event
.x
- fStartPosition
;
124 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
125 long newStart
= fZoom
.getStartTime() + delta
;
126 if (newStart
< getStartTime())
127 newStart
= getStartTime();
128 long newEnd
= newStart
+ fZoom
.getDuration();
129 if (newEnd
> getEndTime()) {
130 newEnd
= getEndTime();
131 newStart
= newEnd
- fZoom
.getDuration();
133 fRangeStartTime
= newStart
;
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 public void paintControl(PaintEvent event
) {
144 super.paintControl(event
);
146 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
147 assert image
!= null;
149 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
150 GC rangeWindowGC
= new GC(rangeRectangleImage
);
152 if (fScaledData
!= null && fRangeStartTime
!= 0) {
153 drawTimeRangeWindow(rangeWindowGC
, rangeRectangleImage
);
156 // Draws the buffer image onto the canvas.
157 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
159 rangeWindowGC
.dispose();
160 rangeRectangleImage
.dispose();
163 private void drawTimeRangeWindow(GC imageGC
, Image image
) {
165 // Map times to histogram coordinates
166 long bucketSpan
= fScaledData
.fBucketDuration
;
167 int rangeWidth
= (int) (fRangeDuration
/ bucketSpan
);
169 int left
= (int) ((fRangeStartTime
- fDataModel
.getStartTime()) / bucketSpan
);
170 int right
= left
+ rangeWidth
;
171 int center
= (left
+ right
) / 2;
172 int height
= fCanvas
.getSize().y
- 2;
174 // Draw the selection window
175 imageGC
.setForeground(fTimeRangeColor
);
176 imageGC
.setLineWidth(1);
177 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
178 imageGC
.drawRoundRectangle(left
, 0, rangeWidth
, height
- 1, 15, 15);
180 // Fill the selection window
181 imageGC
.setBackground(fTimeRangeColor
);
182 imageGC
.setAlpha(35);
183 imageGC
.fillRoundRectangle(left
+ 1, 1, rangeWidth
- 1, height
- 2, 15, 15);
184 imageGC
.setAlpha(255);
186 // Draw the cross hair
187 imageGC
.setForeground(fTimeRangeColor
);
188 imageGC
.setLineWidth(1);
189 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
191 int chHalfWidth
= ((rangeWidth
< 60) ? rangeWidth
* 2 / 3 : 40) / 2;
192 imageGC
.drawLine(center
- chHalfWidth
, height
/ 2, center
+ chHalfWidth
, height
/ 2);
193 imageGC
.drawLine(center
, height
/ 2 - chHalfWidth
, center
, height
/ 2 + chHalfWidth
);