Fix for Histogram widget minor display issues.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / FullTraceHistogram.java
CommitLineData
c392540b
FC
1/*******************************************************************************
2 * Copyright (c) 2011 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
fbd124dd 11 * Bernd Hufmann - Changed to updated histogram data model
c392540b
FC
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.lttng.ui.views.histogram;
15
16import org.eclipse.swt.SWT;
17import org.eclipse.swt.events.MouseEvent;
18import org.eclipse.swt.events.MouseMoveListener;
19import org.eclipse.swt.events.PaintEvent;
20import org.eclipse.swt.graphics.Color;
21import org.eclipse.swt.graphics.GC;
22import org.eclipse.swt.graphics.Image;
23import org.eclipse.swt.widgets.Composite;
24import org.eclipse.swt.widgets.Display;
25
26/**
27 * <b><u>FullTraceHistogram</u></b>
28 * <p>
29 * A histogram that displays the full trace.
30 * <p>
31 * It also features a selected range window that can be dragged and zoomed.
32 */
33public class FullTraceHistogram extends Histogram implements MouseMoveListener {
34
35 // ------------------------------------------------------------------------
36 // Constants
37 // ------------------------------------------------------------------------
38
39 // Histogram colors
40 private final Color fTimeRangeColor = new Color(Display.getCurrent(), 255, 128, 0);
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45
46 private final HistogramZoom fZoom;
47
48 private long fRangeStartTime;
49 private long fRangeDuration;
50
51 // ------------------------------------------------------------------------
52 // Construction
53 // ------------------------------------------------------------------------
54
55 public FullTraceHistogram(HistogramView view, Composite parent) {
56 super(view, parent);
57 fZoom = new HistogramZoom(this, fCanvas, getStartTime(), getTimeLimit());
58 fCanvas.addMouseMoveListener(this);
59 }
60
61 @Override
62 public void dispose() {
63 fTimeRangeColor.dispose();
64 super.dispose();
65 }
66
67 // ------------------------------------------------------------------------
68 // Operations
69 // ------------------------------------------------------------------------
70
6a13fa07
FC
71 public void setFullRange(long startTime, long endTime) {
72 fZoom.setFullRange(startTime, endTime);
73 }
74
c392540b
FC
75 public void setTimeRange(long startTime, long duration) {
76 fRangeStartTime = startTime;
77 fRangeDuration = duration;
c392540b 78 fZoom.setNewRange(fRangeStartTime, fRangeDuration);
fbd124dd 79 fDataModel.complete();
c392540b
FC
80 }
81
82 @Override
83 public void updateTimeRange(long startTime, long endTime) {
84 ((HistogramView) fParentView).updateTimeRange(startTime, endTime);
85 }
86
87 // ------------------------------------------------------------------------
88 // MouseListener
89 // ------------------------------------------------------------------------
90
91 private boolean fMouseDown;
92 private int fStartPosition;
93
94 @Override
95 public void mouseDown(MouseEvent event) {
96 // Check if we are outside the time range; if so, just set the current
97 // event
98 long timestamp = getTimestamp(event.x);
99 if (timestamp < fZoom.getStartTime() || timestamp > fZoom.getEndTime()) {
100 super.mouseDown(event);
101 return;
102 }
103
104 // Otherwise start moving the range window
105 fMouseDown = true;
106 fStartPosition = event.x;
107 }
108
109 @Override
110 public void mouseUp(MouseEvent event) {
111 if (fMouseDown) {
112 fMouseDown = false;
113 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fZoom.getDuration());
114 }
115 }
116
117 // ------------------------------------------------------------------------
118 // MouseMoveListener
119 // ------------------------------------------------------------------------
120
121 @Override
122 public void mouseMove(MouseEvent event) {
123 if (fMouseDown) {
124 int nbBuckets = event.x - fStartPosition;
125 long delta = nbBuckets * fScaledData.fBucketDuration;
126 long newStart = fZoom.getStartTime() + delta;
127 if (newStart < getStartTime())
128 newStart = getStartTime();
129 long newEnd = newStart + fZoom.getDuration();
130 if (newEnd > getEndTime()) {
131 newEnd = getEndTime();
132 newStart = newEnd - fZoom.getDuration();
133 }
134 fRangeStartTime = newStart;
fbd124dd 135 fDataModel.complete();
c392540b
FC
136 }
137 }
138
139 // ------------------------------------------------------------------------
140 // PaintListener
141 // ------------------------------------------------------------------------
142
143 @Override
144 public void paintControl(PaintEvent event) {
145 super.paintControl(event);
146
147 Image image = (Image) fCanvas.getData(IMAGE_KEY);
148 assert image != null;
149
150 Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
151 GC rangeWindowGC = new GC(rangeRectangleImage);
152
4dc47e28 153 if (fScaledData != null && fRangeStartTime != 0) {
c392540b
FC
154 drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage);
155 }
156
157 // Draws the buffer image onto the canvas.
158 event.gc.drawImage(rangeRectangleImage, 0, 0);
159
160 rangeWindowGC.dispose();
161 rangeRectangleImage.dispose();
162 }
163
164 private void drawTimeRangeWindow(GC imageGC, Image image) {
165
166 // Map times to histogram coordinates
167 long bucketSpan = fScaledData.fBucketDuration;
168 int rangeWidth = (int) (fRangeDuration / bucketSpan);
169
fbd124dd 170 int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan);
c392540b
FC
171 int right = left + rangeWidth;
172 int center = (left + right) / 2;
173 int height = fCanvas.getSize().y - 2;
174
175 // Draw the selection window
176 imageGC.setForeground(fTimeRangeColor);
177 imageGC.setLineWidth(1);
178 imageGC.setLineStyle(SWT.LINE_SOLID);
179 imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15);
180
181 // Fill the selection window
182 imageGC.setBackground(fTimeRangeColor);
183 imageGC.setAlpha(35);
184 imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15);
185 imageGC.setAlpha(255);
186
187 // Draw the cross hair
188 imageGC.setForeground(fTimeRangeColor);
189 imageGC.setLineWidth(1);
190 imageGC.setLineStyle(SWT.LINE_SOLID);
191
192 int chHalfWidth = ((rangeWidth < 60) ? rangeWidth * 2 / 3 : 40) / 2;
193 imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
194 imageGC.drawLine(center, height / 2 - chHalfWidth, center, height / 2 + chHalfWidth);
195 }
196
197}
This page took 0.032822 seconds and 5 git commands to generate.