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