Merge branch 'master' into lttng_2_0_control_dev
[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 * Francois Chouinard - Initial API and implementation
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 * <b><u>FullTraceHistogram</u></b>
29 * <p>
30 * A histogram that displays the full trace.
31 * <p>
32 * It also features a selected range window that can be dragged and zoomed.
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;
50 private long fRangeDuration;
51
52 // ------------------------------------------------------------------------
53 // Construction
54 // ------------------------------------------------------------------------
55
56 public FullTraceHistogram(HistogramView view, Composite parent) {
57 super(view, parent);
58 fZoom = new HistogramZoom(this, fCanvas, getStartTime(), getTimeLimit());
59 fCanvas.addMouseMoveListener(this);
60 }
61
62 @Override
63 public void dispose() {
64 fTimeRangeColor.dispose();
65 super.dispose();
66 }
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 public void setFullRange(long startTime, long endTime) {
73 fZoom.setFullRange(startTime, endTime);
74 }
75
76 public void setTimeRange(long startTime, long duration) {
77 fRangeStartTime = startTime;
78 fRangeDuration = duration;
79 fZoom.setNewRange(fRangeStartTime, fRangeDuration);
80 fDataModel.complete();
81 }
82
83 @Override
84 public void updateTimeRange(long startTime, long endTime) {
85 ((HistogramView) fParentView).updateTimeRange(startTime, endTime);
86 }
87
88 // ------------------------------------------------------------------------
89 // MouseListener
90 // ------------------------------------------------------------------------
91
92 private boolean fMouseDown;
93 private int fStartPosition;
94
95 @Override
96 public void mouseDown(MouseEvent event) {
97 // Check if we are outside the time range; if so, just set the current
98 // event
99 long timestamp = getTimestamp(event.x);
100 if (timestamp < fZoom.getStartTime() || timestamp > fZoom.getEndTime()) {
101 super.mouseDown(event);
102 return;
103 }
104
105 // Otherwise start moving the range window
106 fMouseDown = true;
107 fStartPosition = event.x;
108 }
109
110 @Override
111 public void mouseUp(MouseEvent event) {
112 if (fMouseDown) {
113 fMouseDown = false;
114 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fZoom.getDuration());
115 }
116 }
117
118 // ------------------------------------------------------------------------
119 // MouseMoveListener
120 // ------------------------------------------------------------------------
121
122 @Override
123 public void mouseMove(MouseEvent event) {
124 if (fMouseDown) {
125 int nbBuckets = event.x - fStartPosition;
126 long delta = nbBuckets * fScaledData.fBucketDuration;
127 long newStart = fZoom.getStartTime() + delta;
128 if (newStart < getStartTime())
129 newStart = getStartTime();
130 long newEnd = newStart + fZoom.getDuration();
131 if (newEnd > getEndTime()) {
132 newEnd = getEndTime();
133 newStart = newEnd - fZoom.getDuration();
134 }
135 fRangeStartTime = newStart;
136 fDataModel.complete();
137 }
138 }
139
140 // ------------------------------------------------------------------------
141 // PaintListener
142 // ------------------------------------------------------------------------
143
144 @Override
145 public void paintControl(PaintEvent event) {
146 super.paintControl(event);
147
148 Image image = (Image) fCanvas.getData(IMAGE_KEY);
149 assert image != null;
150
151 Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
152 GC rangeWindowGC = new GC(rangeRectangleImage);
153
154 if (fScaledData != null && fRangeStartTime != 0) {
155 drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage);
156 }
157
158 // Draws the buffer image onto the canvas.
159 event.gc.drawImage(rangeRectangleImage, 0, 0);
160
161 rangeWindowGC.dispose();
162 rangeRectangleImage.dispose();
163 }
164
165 private void drawTimeRangeWindow(GC imageGC, Image image) {
166
167 // Map times to histogram coordinates
168 long bucketSpan = fScaledData.fBucketDuration;
169 int rangeWidth = (int) (fRangeDuration / bucketSpan);
170
171 int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan);
172 int right = left + rangeWidth;
173 int center = (left + right) / 2;
174 int height = fCanvas.getSize().y - 2;
175
176 // Draw the selection window
177 imageGC.setForeground(fTimeRangeColor);
178 imageGC.setLineWidth(1);
179 imageGC.setLineStyle(SWT.LINE_SOLID);
180 imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15);
181
182 // Fill the selection window
183 imageGC.setBackground(fTimeRangeColor);
184 imageGC.setAlpha(35);
185 imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15);
186 imageGC.setAlpha(255);
187
188 // Draw the cross hair
189 imageGC.setForeground(fTimeRangeColor);
190 imageGC.setLineWidth(1);
191 imageGC.setLineStyle(SWT.LINE_SOLID);
192
193 int chHalfWidth = ((rangeWidth < 60) ? rangeWidth * 2 / 3 : 40) / 2;
194 imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
195 imageGC.drawLine(center, height / 2 - chHalfWidth, center, height / 2 + chHalfWidth);
196 }
197
198 }
This page took 0.035411 seconds and 6 git commands to generate.