Fix time range synchronization issues
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / FullTraceHistogram.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.views.histogram;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.MouseEvent;
18 import org.eclipse.swt.events.MouseMoveListener;
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 /**
27 * A histogram widget that displays the event distribution of a whole trace.
28 * <p>
29 * It also features a selected range window that can be dragged and zoomed.
30 *
31 * @version 1.1
32 * @author Francois Chouinard
33 */
34 public 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 = 0L;
50 private long fRangeDuration;
51
52 // ------------------------------------------------------------------------
53 // Construction
54 // ------------------------------------------------------------------------
55
56 /**
57 * Full Constructor
58 *
59 * @param view A reference to the parent histogram view
60 * @param parent A reference to the parent composite
61 */
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
78 /**
79 * Sets the time range of the full histogram.
80 *
81 * @param startTime A start time
82 * @param endTime A end time
83 */
84 public void setFullRange(long startTime, long endTime) {
85 fZoom.setFullRange(startTime, endTime);
86 }
87
88 /**
89 * Sets the selected time range.
90 *
91 * @param startTime The histogram start time
92 * @param duration The histogram duration
93 */
94 public void setTimeRange(long startTime, long duration) {
95 fRangeStartTime = startTime;
96 fRangeDuration = duration;
97 fZoom.setNewRange(fRangeStartTime, fRangeDuration);
98 fDataModel.complete();
99 }
100
101 /* (non-Javadoc)
102 * @see org.eclipse.linuxtools.tmf.ui.views.histogram.Histogram#updateTimeRange(long, long)
103 */
104 @Override
105 public void updateTimeRange(long startTime, long endTime) {
106 ((HistogramView) fParentView).updateTimeRange(startTime, endTime);
107 }
108
109 // ------------------------------------------------------------------------
110 // MouseListener
111 // ------------------------------------------------------------------------
112
113 private boolean fMouseDown;
114 private int fStartPosition;
115
116 @Override
117 public void mouseDown(MouseEvent event) {
118 fMouseDown = true;
119 fStartPosition = event.x;
120 }
121
122 @Override
123 public void mouseUp(MouseEvent event) {
124 if (fMouseDown) {
125 fMouseDown = false;
126 // Check if mouse click without move; if so, just set the current event time
127 if (event.x == fStartPosition) {
128 super.mouseDown(event);
129 return;
130 }
131
132 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fRangeDuration);
133
134 }
135 }
136
137
138 // ------------------------------------------------------------------------
139 // MouseMoveListener
140 // ------------------------------------------------------------------------
141
142 @Override
143 public void mouseMove(MouseEvent event) {
144
145 if (fMouseDown) {
146 int nbBuckets = event.x - fStartPosition;
147 long delta = nbBuckets * fScaledData.fBucketDuration;
148 long newStart = fZoom.getStartTime() + delta;
149 if (newStart < getStartTime()) {
150 newStart = getStartTime();
151 }
152 long newEnd = newStart + fZoom.getDuration();
153 if (newEnd > getEndTime()) {
154 newEnd = getEndTime();
155 newStart = newEnd - fZoom.getDuration();
156 }
157 fRangeStartTime = newStart;
158 fDataModel.complete();
159 }
160 }
161
162 // ------------------------------------------------------------------------
163 // PaintListener
164 // ------------------------------------------------------------------------
165
166 @Override
167 public void paintControl(PaintEvent event) {
168 super.paintControl(event);
169
170 Image image = (Image) fCanvas.getData(IMAGE_KEY);
171 assert image != null;
172
173 Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
174 GC rangeWindowGC = new GC(rangeRectangleImage);
175
176 if ((fScaledData != null) && (fRangeStartTime != 0)) {
177 drawTimeRangeWindow(rangeWindowGC);
178 }
179
180 // Draws the buffer image onto the canvas.
181 event.gc.drawImage(rangeRectangleImage, 0, 0);
182
183 rangeWindowGC.dispose();
184 rangeRectangleImage.dispose();
185 }
186
187 private void drawTimeRangeWindow(GC imageGC) {
188
189 // Map times to histogram coordinates
190 long bucketSpan = Math.max(fScaledData.fBucketDuration, 1);
191 int rangeWidth = (int) (fRangeDuration / bucketSpan);
192
193 int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan);
194 int right = left + rangeWidth;
195 int center = (left + right) / 2;
196 int height = fCanvas.getSize().y;
197
198 // Draw the selection window
199 imageGC.setForeground(fTimeRangeColor);
200 imageGC.setLineWidth(1);
201 imageGC.setLineStyle(SWT.LINE_SOLID);
202 imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15);
203
204 // Fill the selection window
205 imageGC.setBackground(fTimeRangeColor);
206 imageGC.setAlpha(35);
207 imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15);
208 imageGC.setAlpha(255);
209
210 // Draw the cross hair
211 imageGC.setForeground(fTimeRangeColor);
212 imageGC.setLineWidth(1);
213 imageGC.setLineStyle(SWT.LINE_SOLID);
214
215 int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2;
216 imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
217 imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth);
218 }
219
220 }
This page took 0.035564 seconds and 6 git commands to generate.