Commit | Line | Data |
---|---|---|
c392540b | 1 | /******************************************************************************* |
e0752744 | 2 | * Copyright (c) 2011, 2012 Ericsson |
bfe038ff | 3 | * |
c392540b FC |
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 | |
bfe038ff | 8 | * |
c392540b FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
fbd124dd | 11 | * Bernd Hufmann - Changed to updated histogram data model |
e0752744 | 12 | * Francois Chouinard - Initial API and implementation |
c392540b FC |
13 | *******************************************************************************/ |
14 | ||
e0752744 | 15 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
c392540b FC |
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 | ||
6a13fa07 FC |
72 | public void setFullRange(long startTime, long endTime) { |
73 | fZoom.setFullRange(startTime, endTime); | |
74 | } | |
75 | ||
c392540b FC |
76 | public void setTimeRange(long startTime, long duration) { |
77 | fRangeStartTime = startTime; | |
78 | fRangeDuration = duration; | |
c392540b | 79 | fZoom.setNewRange(fRangeStartTime, fRangeDuration); |
fbd124dd | 80 | fDataModel.complete(); |
c392540b FC |
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); | |
bfe038ff | 100 | if ((timestamp < fZoom.getStartTime()) || (timestamp > fZoom.getEndTime())) { |
c392540b FC |
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; | |
bfe038ff | 128 | if (newStart < getStartTime()) { |
c392540b | 129 | newStart = getStartTime(); |
bfe038ff | 130 | } |
c392540b FC |
131 | long newEnd = newStart + fZoom.getDuration(); |
132 | if (newEnd > getEndTime()) { | |
133 | newEnd = getEndTime(); | |
134 | newStart = newEnd - fZoom.getDuration(); | |
135 | } | |
136 | fRangeStartTime = newStart; | |
fbd124dd | 137 | fDataModel.complete(); |
c392540b FC |
138 | } |
139 | } | |
140 | ||
141 | // ------------------------------------------------------------------------ | |
142 | // PaintListener | |
143 | // ------------------------------------------------------------------------ | |
144 | ||
145 | @Override | |
146 | public void paintControl(PaintEvent event) { | |
147 | super.paintControl(event); | |
148 | ||
149 | Image image = (Image) fCanvas.getData(IMAGE_KEY); | |
150 | assert image != null; | |
151 | ||
152 | Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY); | |
153 | GC rangeWindowGC = new GC(rangeRectangleImage); | |
154 | ||
bfe038ff | 155 | if ((fScaledData != null) && (fRangeStartTime != 0)) { |
c392540b FC |
156 | drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage); |
157 | } | |
158 | ||
159 | // Draws the buffer image onto the canvas. | |
160 | event.gc.drawImage(rangeRectangleImage, 0, 0); | |
161 | ||
162 | rangeWindowGC.dispose(); | |
163 | rangeRectangleImage.dispose(); | |
164 | } | |
165 | ||
166 | private void drawTimeRangeWindow(GC imageGC, Image image) { | |
167 | ||
168 | // Map times to histogram coordinates | |
bfe038ff | 169 | long bucketSpan = Math.max(fScaledData.fBucketDuration,1); |
c392540b FC |
170 | int rangeWidth = (int) (fRangeDuration / bucketSpan); |
171 | ||
fbd124dd | 172 | int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan); |
c392540b FC |
173 | int right = left + rangeWidth; |
174 | int center = (left + right) / 2; | |
175 | int height = fCanvas.getSize().y - 2; | |
176 | ||
177 | // Draw the selection window | |
178 | imageGC.setForeground(fTimeRangeColor); | |
179 | imageGC.setLineWidth(1); | |
180 | imageGC.setLineStyle(SWT.LINE_SOLID); | |
181 | imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, 15, 15); | |
182 | ||
183 | // Fill the selection window | |
184 | imageGC.setBackground(fTimeRangeColor); | |
185 | imageGC.setAlpha(35); | |
186 | imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, 15, 15); | |
187 | imageGC.setAlpha(255); | |
188 | ||
189 | // Draw the cross hair | |
190 | imageGC.setForeground(fTimeRangeColor); | |
191 | imageGC.setLineWidth(1); | |
192 | imageGC.setLineStyle(SWT.LINE_SOLID); | |
193 | ||
bfe038ff | 194 | int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2; |
c392540b | 195 | imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2); |
bfe038ff | 196 | imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth); |
c392540b FC |
197 | } |
198 | ||
199 | } |