Fix for bug 384417: Incorrect range displayed in histogram tool tip.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / FullTraceHistogram.java
CommitLineData
c392540b 1/*******************************************************************************
e0752744 2 * Copyright (c) 2011, 2012 Ericsson
bfe038ff 3 *
c392540b
FC
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
bfe038ff 8 *
c392540b
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
fbd124dd 11 * Bernd Hufmann - Changed to updated histogram data model
c392540b
FC
12 *******************************************************************************/
13
e0752744 14package org.eclipse.linuxtools.tmf.ui.views.histogram;
c392540b
FC
15
16import org.eclipse.swt.SWT;
17import org.eclipse.swt.events.MouseEvent;
18import org.eclipse.swt.events.MouseMoveListener;
19import org.eclipse.swt.events.PaintEvent;
20import org.eclipse.swt.graphics.Color;
21import org.eclipse.swt.graphics.GC;
22import org.eclipse.swt.graphics.Image;
23import org.eclipse.swt.widgets.Composite;
24import org.eclipse.swt.widgets.Display;
25
26/**
d26274e7 27 * A histogram widget that displays the event distribution of a whole trace.
c392540b
FC
28 * <p>
29 * It also features a selected range window that can be dragged and zoomed.
b544077e
BH
30 *
31 * @version 1.0
32 * @author Francois Chouinard
c392540b
FC
33 */
34public class FullTraceHistogram extends Histogram implements MouseMoveListener {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39
40 // Histogram colors
41 private final Color fTimeRangeColor = new Color(Display.getCurrent(), 255, 128, 0);
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 private final HistogramZoom fZoom;
48
49 private long fRangeStartTime;
50 private long fRangeDuration;
51
52 // ------------------------------------------------------------------------
53 // Construction
54 // ------------------------------------------------------------------------
55
b544077e
BH
56 /**
57 * Standard Constructor.
58 *
59 * @param view A reference to the parent histogram view
60 * @param parent A reference to the parent composite
61 */
c392540b
FC
62 public FullTraceHistogram(HistogramView view, Composite parent) {
63 super(view, parent);
64 fZoom = new HistogramZoom(this, fCanvas, getStartTime(), getTimeLimit());
65 fCanvas.addMouseMoveListener(this);
66 }
67
68 @Override
69 public void dispose() {
70 fTimeRangeColor.dispose();
71 super.dispose();
72 }
73
74 // ------------------------------------------------------------------------
75 // Operations
76 // ------------------------------------------------------------------------
77
b544077e
BH
78 /**
79 * Sets the time range of the full histogram.
80 *
81 * @param startTime A start time
82 * @param endTime A end time
83 */
6a13fa07
FC
84 public void setFullRange(long startTime, long endTime) {
85 fZoom.setFullRange(startTime, endTime);
86 }
87
b544077e
BH
88 /**
89 * Sets the selected time range.
90 *
91 * @param startTime A start time
92 * @param duration A window duration
93 */
c392540b
FC
94 public void setTimeRange(long startTime, long duration) {
95 fRangeStartTime = startTime;
96 fRangeDuration = duration;
c392540b 97 fZoom.setNewRange(fRangeStartTime, fRangeDuration);
fbd124dd 98 fDataModel.complete();
c392540b
FC
99 }
100
101 @Override
102 public void updateTimeRange(long startTime, long endTime) {
103 ((HistogramView) fParentView).updateTimeRange(startTime, endTime);
104 }
105
106 // ------------------------------------------------------------------------
107 // MouseListener
108 // ------------------------------------------------------------------------
109
110 private boolean fMouseDown;
111 private int fStartPosition;
112
113 @Override
114 public void mouseDown(MouseEvent event) {
c392540b
FC
115 fMouseDown = true;
116 fStartPosition = event.x;
117 }
1c467552 118
c392540b
FC
119 @Override
120 public void mouseUp(MouseEvent event) {
121 if (fMouseDown) {
122 fMouseDown = false;
1c467552
BH
123 // Check if mouse click without move; if so, just set the current event time
124 if (event.x == fStartPosition) {
125 super.mouseDown(event);
126 return;
127 }
128
c392540b 129 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fZoom.getDuration());
1c467552 130
c392540b
FC
131 }
132 }
1c467552
BH
133
134
c392540b
FC
135 // ------------------------------------------------------------------------
136 // MouseMoveListener
137 // ------------------------------------------------------------------------
138
139 @Override
140 public void mouseMove(MouseEvent event) {
1c467552 141
c392540b
FC
142 if (fMouseDown) {
143 int nbBuckets = event.x - fStartPosition;
144 long delta = nbBuckets * fScaledData.fBucketDuration;
145 long newStart = fZoom.getStartTime() + delta;
bfe038ff 146 if (newStart < getStartTime()) {
c392540b 147 newStart = getStartTime();
bfe038ff 148 }
c392540b
FC
149 long newEnd = newStart + fZoom.getDuration();
150 if (newEnd > getEndTime()) {
151 newEnd = getEndTime();
152 newStart = newEnd - fZoom.getDuration();
153 }
154 fRangeStartTime = newStart;
fbd124dd 155 fDataModel.complete();
c392540b
FC
156 }
157 }
158
159 // ------------------------------------------------------------------------
160 // PaintListener
161 // ------------------------------------------------------------------------
162
163 @Override
164 public void paintControl(PaintEvent event) {
165 super.paintControl(event);
166
167 Image image = (Image) fCanvas.getData(IMAGE_KEY);
168 assert image != null;
169
170 Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
171 GC rangeWindowGC = new GC(rangeRectangleImage);
172
bfe038ff 173 if ((fScaledData != null) && (fRangeStartTime != 0)) {
c392540b
FC
174 drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage);
175 }
176
177 // Draws the buffer image onto the canvas.
178 event.gc.drawImage(rangeRectangleImage, 0, 0);
179
180 rangeWindowGC.dispose();
181 rangeRectangleImage.dispose();
182 }
183
184 private void drawTimeRangeWindow(GC imageGC, Image image) {
185
186 // Map times to histogram coordinates
bfe038ff 187 long bucketSpan = Math.max(fScaledData.fBucketDuration,1);
c392540b
FC
188 int rangeWidth = (int) (fRangeDuration / bucketSpan);
189
fbd124dd 190 int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan);
c392540b
FC
191 int right = left + rangeWidth;
192 int center = (left + right) / 2;
193 int height = fCanvas.getSize().y - 2;
194
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);
200
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);
206
207 // Draw the cross hair
208 imageGC.setForeground(fTimeRangeColor);
209 imageGC.setLineWidth(1);
210 imageGC.setLineStyle(SWT.LINE_SOLID);
211
bfe038ff 212 int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2;
c392540b 213 imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
bfe038ff 214 imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth);
c392540b
FC
215 }
216
217}
This page took 0.040715 seconds and 5 git commands to generate.