tmf: Fix NullPointerExeptions in Histogram view
[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.PaintEvent;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.Composite;
23
24 /**
25 * A histogram widget that displays the event distribution of a whole trace.
26 * <p>
27 * It also features a selected range window that can be dragged and zoomed.
28 *
29 * @version 1.1
30 * @author Francois Chouinard
31 */
32 public class FullTraceHistogram extends Histogram {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 private final HistogramZoom fZoom;
39
40 private long fRangeStartTime = 0L;
41 private long fRangeDuration;
42
43 // ------------------------------------------------------------------------
44 // Construction
45 // ------------------------------------------------------------------------
46
47 /**
48 * Full Constructor
49 *
50 * @param view A reference to the parent histogram view
51 * @param parent A reference to the parent composite
52 */
53 public FullTraceHistogram(HistogramView view, Composite parent) {
54 super(view, parent);
55 fZoom = new HistogramZoom(this, getStartTime(), getTimeLimit());
56 addMouseWheelListener(fZoom);
57 }
58
59 @Override
60 public void dispose() {
61 super.dispose();
62 }
63
64 // ------------------------------------------------------------------------
65 // Operations
66 // ------------------------------------------------------------------------
67
68 @Override
69 public void clear() {
70 fRangeStartTime = 0L;
71 fRangeDuration = 0L;
72 if (fZoom != null) {
73 fZoom.setFullRange(0L, 0L);
74 fZoom.setNewRange(0L, 0L);
75 }
76 super.clear();
77 }
78
79 /**
80 * Sets the time range of the full histogram.
81 *
82 * @param startTime A start time
83 * @param endTime A end time
84 */
85 public void setFullRange(long startTime, long endTime) {
86 fZoom.setFullRange(startTime, endTime);
87 }
88
89 /**
90 * Sets the selected time range.
91 *
92 * @param startTime The histogram start time
93 * @param duration The histogram duration
94 */
95 public void setTimeRange(long startTime, long duration) {
96 fRangeStartTime = startTime;
97 fRangeDuration = duration;
98 fZoom.setNewRange(fRangeStartTime, fRangeDuration);
99 fDataModel.complete();
100 }
101
102 // ------------------------------------------------------------------------
103 // MouseListener
104 // ------------------------------------------------------------------------
105
106 private int fStartPosition;
107 private boolean fMouseMoved;
108
109 @Override
110 public void mouseDown(MouseEvent event) {
111 if (fScaledData != null && fDragState == DRAG_NONE && fDataModel.getNbEvents() != 0) {
112 if (event.button == 2 || (event.button == 1 && (event.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) {
113 fDragState = DRAG_RANGE;
114 fDragButton = event.button;
115 fStartPosition = event.x;
116 fMouseMoved = false;
117 return;
118 } else if (event.button == 3) {
119 fDragState = DRAG_ZOOM;
120 fDragButton = event.button;
121 long time = Math.min(getTimestamp(event.x), getEndTime());
122 if ((event.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
123 if (time < fRangeStartTime + fRangeDuration / 2) {
124 fRangeStartTime = fRangeStartTime + fRangeDuration;
125 }
126 } else {
127 fRangeStartTime = time;
128 }
129 fRangeDuration = time - fRangeStartTime;
130 fCanvas.redraw();
131 return;
132 }
133 }
134 super.mouseDown(event);
135 }
136
137 @Override
138 public void mouseUp(MouseEvent event) {
139 if (fDragState == DRAG_RANGE && event.button == fDragButton) {
140 fDragState = DRAG_NONE;
141 fDragButton = 0;
142 if (!fMouseMoved) {
143 // if single click without move, center on the click
144 long startTime = getTimestamp(event.x) - fRangeDuration / 2;
145 fRangeStartTime = Math.max(getStartTime(), Math.min(getEndTime() - fRangeDuration, startTime));
146 }
147 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fRangeDuration);
148 return;
149 } else if (fDragState == DRAG_ZOOM && event.button == fDragButton) {
150 fDragState = DRAG_NONE;
151 fDragButton = 0;
152 if (fRangeDuration < 0) {
153 fRangeStartTime = fRangeStartTime + fRangeDuration;
154 fRangeDuration = -fRangeDuration;
155 }
156 if (fRangeDuration > 0) {
157 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fRangeDuration);
158 } else {
159 fRangeStartTime = fZoom.getStartTime();
160 fRangeDuration = fZoom.getDuration();
161 fCanvas.redraw();
162 }
163 return;
164 }
165 super.mouseUp(event);
166 }
167
168 // ------------------------------------------------------------------------
169 // MouseMoveListener
170 // ------------------------------------------------------------------------
171
172 @Override
173 public void mouseMove(MouseEvent event) {
174 if (fDragState == DRAG_RANGE) {
175 int nbBuckets = event.x - fStartPosition;
176 long delta = nbBuckets * fScaledData.fBucketDuration;
177 long newStart = fZoom.getStartTime() + delta;
178 if (newStart < getStartTime()) {
179 newStart = getStartTime();
180 }
181 long newEnd = newStart + fZoom.getDuration();
182 if (newEnd > getEndTime()) {
183 newEnd = getEndTime();
184 newStart = newEnd - fZoom.getDuration();
185 }
186 fRangeStartTime = newStart;
187 fCanvas.redraw();
188 fMouseMoved = true;
189 return;
190 } else if (fDragState == DRAG_ZOOM) {
191 long endTime = Math.max(getStartTime(), Math.min(getEndTime(), getTimestamp(event.x)));
192 fRangeDuration = endTime - fRangeStartTime;
193 fCanvas.redraw();
194 return;
195 }
196 super.mouseMove(event);
197 }
198
199 // ------------------------------------------------------------------------
200 // PaintListener
201 // ------------------------------------------------------------------------
202
203 @Override
204 public void paintControl(PaintEvent event) {
205 super.paintControl(event);
206
207 Image image = (Image) fCanvas.getData(IMAGE_KEY);
208 assert image != null;
209
210 Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
211 GC rangeWindowGC = new GC(rangeRectangleImage);
212
213 if ((fScaledData != null) && (fRangeDuration != 0 || fDragState == DRAG_ZOOM)) {
214 drawTimeRangeWindow(rangeWindowGC, fRangeStartTime, fRangeDuration);
215 }
216
217 // Draws the buffer image onto the canvas.
218 event.gc.drawImage(rangeRectangleImage, 0, 0);
219
220 rangeWindowGC.dispose();
221 rangeRectangleImage.dispose();
222 }
223
224 /**
225 * Get the histogram zoom
226 * @return the histogram zoom
227 * @since 2.0
228 */
229 public HistogramZoom getZoom() {
230 return fZoom;
231 }
232 }
This page took 0.036852 seconds and 6 git commands to generate.