Commit | Line | Data |
---|---|---|
c392540b | 1 | /******************************************************************************* |
11252342 | 2 | * Copyright (c) 2011, 2013 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 |
65cdf787 | 12 | * Patrick Tasse - Update for mouse wheel zoom |
c392540b FC |
13 | *******************************************************************************/ |
14 | ||
e0752744 | 15 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
c392540b FC |
16 | |
17 | import org.eclipse.swt.SWT; | |
18 | import org.eclipse.swt.events.MouseEvent; | |
c392540b FC |
19 | import org.eclipse.swt.events.PaintEvent; |
20 | import org.eclipse.swt.graphics.Color; | |
21 | import org.eclipse.swt.graphics.GC; | |
22 | import org.eclipse.swt.graphics.Image; | |
23 | import org.eclipse.swt.widgets.Composite; | |
24 | import 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. | |
f8177ba2 FC |
30 | * |
31 | * @version 1.1 | |
b544077e | 32 | * @author Francois Chouinard |
c392540b | 33 | */ |
f888477a | 34 | public class FullTraceHistogram extends Histogram { |
c392540b FC |
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 | ||
f8177ba2 | 49 | private long fRangeStartTime = 0L; |
c392540b FC |
50 | private long fRangeDuration; |
51 | ||
52 | // ------------------------------------------------------------------------ | |
53 | // Construction | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
b544077e | 56 | /** |
f8177ba2 FC |
57 | * Full Constructor |
58 | * | |
59 | * @param view A reference to the parent histogram view | |
b544077e BH |
60 | * @param parent A reference to the parent composite |
61 | */ | |
c392540b FC |
62 | public FullTraceHistogram(HistogramView view, Composite parent) { |
63 | super(view, parent); | |
65cdf787 PT |
64 | fZoom = new HistogramZoom(this, getStartTime(), getTimeLimit()); |
65 | addMouseWheelListener(fZoom); | |
c392540b FC |
66 | } |
67 | ||
68 | @Override | |
69 | public void dispose() { | |
70 | fTimeRangeColor.dispose(); | |
71 | super.dispose(); | |
72 | } | |
73 | ||
74 | // ------------------------------------------------------------------------ | |
75 | // Operations | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
1be49d83 PT |
78 | @Override |
79 | public void clear() { | |
80 | fRangeStartTime = 0L; | |
81 | fRangeDuration = 0L; | |
82 | if (fZoom != null) { | |
83 | fZoom.setFullRange(0L, 0L); | |
84 | fZoom.setNewRange(0L, 0L); | |
1be49d83 PT |
85 | } |
86 | super.clear(); | |
87 | } | |
88 | ||
b544077e | 89 | /** |
f8177ba2 FC |
90 | * Sets the time range of the full histogram. |
91 | * | |
b544077e BH |
92 | * @param startTime A start time |
93 | * @param endTime A end time | |
94 | */ | |
6a13fa07 FC |
95 | public void setFullRange(long startTime, long endTime) { |
96 | fZoom.setFullRange(startTime, endTime); | |
97 | } | |
98 | ||
b544077e BH |
99 | /** |
100 | * Sets the selected time range. | |
f8177ba2 FC |
101 | * |
102 | * @param startTime The histogram start time | |
103 | * @param duration The histogram duration | |
b544077e | 104 | */ |
c392540b FC |
105 | public void setTimeRange(long startTime, long duration) { |
106 | fRangeStartTime = startTime; | |
107 | fRangeDuration = duration; | |
c392540b | 108 | fZoom.setNewRange(fRangeStartTime, fRangeDuration); |
fbd124dd | 109 | fDataModel.complete(); |
c392540b FC |
110 | } |
111 | ||
c392540b FC |
112 | // ------------------------------------------------------------------------ |
113 | // MouseListener | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
c392540b | 116 | private int fStartPosition; |
f888477a | 117 | private boolean fMouseMoved; |
c392540b FC |
118 | |
119 | @Override | |
120 | public void mouseDown(MouseEvent event) { | |
f888477a PT |
121 | if ((event.button == 2 || (event.button == 1 && (event.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) && |
122 | fDragState == DRAG_NONE && fDataModel.getNbEvents() != 0) { | |
123 | fDragState = DRAG_RANGE; | |
124 | fDragButton = event.button; | |
125 | fStartPosition = event.x; | |
126 | fMouseMoved = false; | |
127 | return; | |
128 | } | |
129 | super.mouseDown(event); | |
c392540b | 130 | } |
f8177ba2 | 131 | |
c392540b FC |
132 | @Override |
133 | public void mouseUp(MouseEvent event) { | |
f888477a PT |
134 | if (fDragState == DRAG_RANGE && event.button == fDragButton) { |
135 | fDragState = DRAG_NONE; | |
136 | fDragButton = 0; | |
137 | if (!fMouseMoved) { | |
138 | // if single click without move, center on the click | |
139 | long startTime = getTimestamp(event.x) - fRangeDuration / 2; | |
140 | fRangeStartTime = Math.max(getStartTime(), Math.min(getEndTime() - fRangeDuration, startTime)); | |
1c467552 | 141 | } |
13ccc36b | 142 | ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fRangeDuration); |
f888477a | 143 | return; |
c392540b | 144 | } |
f888477a | 145 | super.mouseUp(event); |
c392540b | 146 | } |
f8177ba2 | 147 | |
c392540b FC |
148 | // ------------------------------------------------------------------------ |
149 | // MouseMoveListener | |
150 | // ------------------------------------------------------------------------ | |
151 | ||
152 | @Override | |
153 | public void mouseMove(MouseEvent event) { | |
f888477a | 154 | if (fDragState == DRAG_RANGE) { |
c392540b FC |
155 | int nbBuckets = event.x - fStartPosition; |
156 | long delta = nbBuckets * fScaledData.fBucketDuration; | |
157 | long newStart = fZoom.getStartTime() + delta; | |
bfe038ff | 158 | if (newStart < getStartTime()) { |
c392540b | 159 | newStart = getStartTime(); |
bfe038ff | 160 | } |
c392540b FC |
161 | long newEnd = newStart + fZoom.getDuration(); |
162 | if (newEnd > getEndTime()) { | |
163 | newEnd = getEndTime(); | |
164 | newStart = newEnd - fZoom.getDuration(); | |
165 | } | |
166 | fRangeStartTime = newStart; | |
f888477a PT |
167 | fCanvas.redraw(); |
168 | fMouseMoved = true; | |
169 | return; | |
c392540b | 170 | } |
f888477a | 171 | super.mouseMove(event); |
c392540b FC |
172 | } |
173 | ||
174 | // ------------------------------------------------------------------------ | |
175 | // PaintListener | |
176 | // ------------------------------------------------------------------------ | |
177 | ||
178 | @Override | |
179 | public void paintControl(PaintEvent event) { | |
180 | super.paintControl(event); | |
181 | ||
182 | Image image = (Image) fCanvas.getData(IMAGE_KEY); | |
183 | assert image != null; | |
184 | ||
185 | Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY); | |
186 | GC rangeWindowGC = new GC(rangeRectangleImage); | |
187 | ||
bfe038ff | 188 | if ((fScaledData != null) && (fRangeStartTime != 0)) { |
f8177ba2 | 189 | drawTimeRangeWindow(rangeWindowGC); |
c392540b FC |
190 | } |
191 | ||
192 | // Draws the buffer image onto the canvas. | |
193 | event.gc.drawImage(rangeRectangleImage, 0, 0); | |
194 | ||
195 | rangeWindowGC.dispose(); | |
196 | rangeRectangleImage.dispose(); | |
197 | } | |
198 | ||
f8177ba2 | 199 | private void drawTimeRangeWindow(GC imageGC) { |
c392540b FC |
200 | |
201 | // Map times to histogram coordinates | |
f8177ba2 | 202 | long bucketSpan = Math.max(fScaledData.fBucketDuration, 1); |
c392540b FC |
203 | int rangeWidth = (int) (fRangeDuration / bucketSpan); |
204 | ||
fbd124dd | 205 | int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan); |
c392540b FC |
206 | int right = left + rangeWidth; |
207 | int center = (left + right) / 2; | |
e60df94a | 208 | int height = fCanvas.getSize().y; |
c392540b FC |
209 | |
210 | // Draw the selection window | |
211 | imageGC.setForeground(fTimeRangeColor); | |
212 | imageGC.setLineWidth(1); | |
213 | imageGC.setLineStyle(SWT.LINE_SOLID); | |
214 | imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15); | |
215 | ||
216 | // Fill the selection window | |
217 | imageGC.setBackground(fTimeRangeColor); | |
218 | imageGC.setAlpha(35); | |
219 | imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15); | |
220 | imageGC.setAlpha(255); | |
221 | ||
222 | // Draw the cross hair | |
223 | imageGC.setForeground(fTimeRangeColor); | |
224 | imageGC.setLineWidth(1); | |
225 | imageGC.setLineStyle(SWT.LINE_SOLID); | |
226 | ||
bfe038ff | 227 | int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2; |
c392540b | 228 | imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2); |
bfe038ff | 229 | imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth); |
c392540b FC |
230 | } |
231 | ||
65cdf787 PT |
232 | /** |
233 | * Get the histogram zoom | |
234 | * @return the histogram zoom | |
235 | * @since 2.0 | |
236 | */ | |
237 | public HistogramZoom getZoom() { | |
238 | return fZoom; | |
239 | } | |
c392540b | 240 | } |