Fix a .gitignore that is too permissive
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / FullTraceHistogram.java
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
13 package org.eclipse.linuxtools.lttng.ui.views.histogram;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.events.MouseMoveListener;
18 import org.eclipse.swt.events.PaintEvent;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.Composite;
23 import 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 */
32 public 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 setFullRange(long startTime, long endTime) {
71 fZoom.setFullRange(startTime, endTime);
72 }
73
74 public void setTimeRange(long startTime, long duration) {
75 fRangeStartTime = startTime;
76 fRangeDuration = duration;
77 fZoom.setNewRange(fRangeStartTime, fRangeDuration);
78 refresh();
79 }
80
81 @Override
82 public void updateTimeRange(long startTime, long endTime) {
83 ((HistogramView) fParentView).updateTimeRange(startTime, endTime);
84 }
85
86 // ------------------------------------------------------------------------
87 // MouseListener
88 // ------------------------------------------------------------------------
89
90 private boolean fMouseDown;
91 private int fStartPosition;
92
93 @Override
94 public void mouseDown(MouseEvent event) {
95 // Check if we are outside the time range; if so, just set the current
96 // event
97 long timestamp = getTimestamp(event.x);
98 if (timestamp < fZoom.getStartTime() || timestamp > fZoom.getEndTime()) {
99 super.mouseDown(event);
100 return;
101 }
102
103 // Otherwise start moving the range window
104 fMouseDown = true;
105 fStartPosition = event.x;
106 }
107
108 @Override
109 public void mouseUp(MouseEvent event) {
110 if (fMouseDown) {
111 fMouseDown = false;
112 ((HistogramView) fParentView).updateTimeRange(fRangeStartTime, fRangeStartTime + fZoom.getDuration());
113 }
114 }
115
116 // ------------------------------------------------------------------------
117 // MouseMoveListener
118 // ------------------------------------------------------------------------
119
120 @Override
121 public void mouseMove(MouseEvent event) {
122 if (fMouseDown) {
123 int nbBuckets = event.x - fStartPosition;
124 long delta = nbBuckets * fScaledData.fBucketDuration;
125 long newStart = fZoom.getStartTime() + delta;
126 if (newStart < getStartTime())
127 newStart = getStartTime();
128 long newEnd = newStart + fZoom.getDuration();
129 if (newEnd > getEndTime()) {
130 newEnd = getEndTime();
131 newStart = newEnd - fZoom.getDuration();
132 }
133 fRangeStartTime = newStart;
134 refresh();
135 }
136 }
137
138 // ------------------------------------------------------------------------
139 // PaintListener
140 // ------------------------------------------------------------------------
141
142 @Override
143 public void paintControl(PaintEvent event) {
144 super.paintControl(event);
145
146 Image image = (Image) fCanvas.getData(IMAGE_KEY);
147 assert image != null;
148
149 Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
150 GC rangeWindowGC = new GC(rangeRectangleImage);
151
152 if (fScaledData != null && fRangeStartTime != 0) {
153 drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage);
154 }
155
156 // Draws the buffer image onto the canvas.
157 event.gc.drawImage(rangeRectangleImage, 0, 0);
158
159 rangeWindowGC.dispose();
160 rangeRectangleImage.dispose();
161 }
162
163 private void drawTimeRangeWindow(GC imageGC, Image image) {
164
165 // Map times to histogram coordinates
166 long bucketSpan = fScaledData.fBucketDuration;
167 int rangeWidth = (int) (fRangeDuration / bucketSpan);
168
169 int left = (int) ((fRangeStartTime - fDataModel.getStartTime()) / bucketSpan);
170 int right = left + rangeWidth;
171 int center = (left + right) / 2;
172 int height = fCanvas.getSize().y - 2;
173
174 // Draw the selection window
175 imageGC.setForeground(fTimeRangeColor);
176 imageGC.setLineWidth(1);
177 imageGC.setLineStyle(SWT.LINE_SOLID);
178 imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15);
179
180 // Fill the selection window
181 imageGC.setBackground(fTimeRangeColor);
182 imageGC.setAlpha(35);
183 imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15);
184 imageGC.setAlpha(255);
185
186 // Draw the cross hair
187 imageGC.setForeground(fTimeRangeColor);
188 imageGC.setLineWidth(1);
189 imageGC.setLineStyle(SWT.LINE_SOLID);
190
191 int chHalfWidth = ((rangeWidth < 60) ? rangeWidth * 2 / 3 : 40) / 2;
192 imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
193 imageGC.drawLine(center, height / 2 - chHalfWidth, center, height / 2 + chHalfWidth);
194 }
195
196 }
This page took 0.034868 seconds and 5 git commands to generate.