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 setTimeRange(long startTime
, long duration
) {
71 fRangeStartTime
= startTime
;
72 fRangeDuration
= duration
;
73 fZoom
.setFullRange(getStartTime(), getEndTime());
74 fZoom
.setNewRange(fRangeStartTime
, fRangeDuration
);
79 public void updateTimeRange(long startTime
, long endTime
) {
80 ((HistogramView
) fParentView
).updateTimeRange(startTime
, endTime
);
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
87 private boolean fMouseDown
;
88 private int fStartPosition
;
91 public void mouseDown(MouseEvent event
) {
92 // Check if we are outside the time range; if so, just set the current
94 long timestamp
= getTimestamp(event
.x
);
95 if (timestamp
< fZoom
.getStartTime() || timestamp
> fZoom
.getEndTime()) {
96 super.mouseDown(event
);
100 // Otherwise start moving the range window
102 fStartPosition
= event
.x
;
106 public void mouseUp(MouseEvent event
) {
109 ((HistogramView
) fParentView
).updateTimeRange(fRangeStartTime
, fRangeStartTime
+ fZoom
.getDuration());
113 // ------------------------------------------------------------------------
115 // ------------------------------------------------------------------------
118 public void mouseMove(MouseEvent event
) {
120 int nbBuckets
= event
.x
- fStartPosition
;
121 long delta
= nbBuckets
* fScaledData
.fBucketDuration
;
122 long newStart
= fZoom
.getStartTime() + delta
;
123 if (newStart
< getStartTime())
124 newStart
= getStartTime();
125 long newEnd
= newStart
+ fZoom
.getDuration();
126 if (newEnd
> getEndTime()) {
127 newEnd
= getEndTime();
128 newStart
= newEnd
- fZoom
.getDuration();
130 fRangeStartTime
= newStart
;
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
140 public void paintControl(PaintEvent event
) {
141 super.paintControl(event
);
143 Image image
= (Image
) fCanvas
.getData(IMAGE_KEY
);
144 assert image
!= null;
146 Image rangeRectangleImage
= new Image(image
.getDevice(), image
, SWT
.IMAGE_COPY
);
147 GC rangeWindowGC
= new GC(rangeRectangleImage
);
149 if (fRangeStartTime
!= 0) {
150 drawTimeRangeWindow(rangeWindowGC
, rangeRectangleImage
);
153 // Draws the buffer image onto the canvas.
154 event
.gc
.drawImage(rangeRectangleImage
, 0, 0);
156 rangeWindowGC
.dispose();
157 rangeRectangleImage
.dispose();
160 private void drawTimeRangeWindow(GC imageGC
, Image image
) {
162 // Map times to histogram coordinates
163 long bucketSpan
= fScaledData
.fBucketDuration
;
164 int rangeWidth
= (int) (fRangeDuration
/ bucketSpan
);
166 int left
= (int) ((fRangeStartTime
- fDataModel
.getStartTime()) / bucketSpan
);
167 int right
= left
+ rangeWidth
;
168 int center
= (left
+ right
) / 2;
169 int height
= fCanvas
.getSize().y
- 2;
171 // Draw the selection window
172 imageGC
.setForeground(fTimeRangeColor
);
173 imageGC
.setLineWidth(1);
174 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
175 imageGC
.drawRoundRectangle(left
, 0, rangeWidth
, height
- 1, 15, 15);
177 // Fill the selection window
178 imageGC
.setBackground(fTimeRangeColor
);
179 imageGC
.setAlpha(35);
180 imageGC
.fillRoundRectangle(left
+ 1, 1, rangeWidth
- 1, height
- 2, 15, 15);
181 imageGC
.setAlpha(255);
183 // Draw the cross hair
184 imageGC
.setForeground(fTimeRangeColor
);
185 imageGC
.setLineWidth(1);
186 imageGC
.setLineStyle(SWT
.LINE_SOLID
);
188 int chHalfWidth
= ((rangeWidth
< 60) ? rangeWidth
* 2 / 3 : 40) / 2;
189 imageGC
.drawLine(center
- chHalfWidth
, height
/ 2, center
+ chHalfWidth
, height
/ 2);
190 imageGC
.drawLine(center
, height
/ 2 - chHalfWidth
, center
, height
/ 2 + chHalfWidth
);