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