Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
abbdd66a | 4 | * |
73005152 BH |
5 | * All rights reserved. This program and the accompanying materials |
6 | * are made available under the terms of the Eclipse Public License v1.0 | |
7 | * which accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
abbdd66a AM |
9 | * |
10 | * Contributors: | |
73005152 BH |
11 | * IBM - Initial API and implementation |
12 | * Bernd Hufmann - Updated for TMF | |
13 | **********************************************************************/ | |
14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd; | |
15 | ||
3bd46eef AM |
16 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
17 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
73005152 BH |
18 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages; |
19 | import org.eclipse.swt.SWT; | |
20 | import org.eclipse.swt.events.PaintEvent; | |
21 | import org.eclipse.swt.events.PaintListener; | |
22 | import org.eclipse.swt.graphics.Color; | |
23 | import org.eclipse.swt.graphics.Point; | |
24 | import org.eclipse.swt.layout.RowLayout; | |
25 | import org.eclipse.swt.widgets.Composite; | |
26 | import org.eclipse.swt.widgets.Display; | |
27 | import org.eclipse.swt.widgets.Shell; | |
28 | ||
29 | /** | |
df0b8ff4 | 30 | * <p> |
73005152 BH |
31 | * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse move hover the time |
32 | * compression bar used to display elapsed time using a tooltip. The tooltip is composed of 2 parts, the text value and | |
df0b8ff4 | 33 | * below, a scale to visually locate the value in a time range (usually the minimum an maximum elapsed time in the whole |
73005152 | 34 | * diagram) |
df0b8ff4 | 35 | * </p> |
abbdd66a | 36 | * |
df0b8ff4 | 37 | * @version 1.0 |
73005152 BH |
38 | * @author sveyrier |
39 | */ | |
40 | public class DrawableToolTip implements PaintListener { | |
41 | ||
df0b8ff4 BH |
42 | // ------------------------------------------------------------------------ |
43 | // Attributes | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
73005152 BH |
46 | /** |
47 | * The parent control where the tooltip must be drawn | |
48 | */ | |
eb63f5ff | 49 | protected Composite fParent = null; |
73005152 BH |
50 | /** |
51 | * The tooltip shell | |
52 | */ | |
eb63f5ff | 53 | protected Shell fToolTipShell = null; |
73005152 | 54 | /** |
df0b8ff4 | 55 | * The Time range data. |
73005152 | 56 | */ |
eb63f5ff | 57 | protected TmfTimeRange fMinMaxRange; |
df0b8ff4 BH |
58 | /** |
59 | * The current time. | |
60 | */ | |
eb63f5ff | 61 | protected ITmfTimestamp fCurrentValue; |
df0b8ff4 BH |
62 | /** |
63 | * The horizontal margin used for drawing. | |
64 | */ | |
eb63f5ff | 65 | private static int fHorMargin = 10; |
df0b8ff4 BH |
66 | /** |
67 | * The vertical margin used for drawing. | |
68 | */ | |
eb63f5ff | 69 | private static int fVertMargin = 10; |
df0b8ff4 BH |
70 | /** |
71 | * The minimum text scale margin. | |
72 | */ | |
eb63f5ff | 73 | private static int fTextScaleMargin = 20; |
df0b8ff4 BH |
74 | /** |
75 | * The length of the text scale. | |
76 | */ | |
eb63f5ff | 77 | private static int fScaleLength = 100; |
df0b8ff4 | 78 | /** |
abbdd66a | 79 | * The text to display |
df0b8ff4 | 80 | */ |
eb63f5ff | 81 | protected String fMessage; |
73005152 BH |
82 | /** |
83 | * The color array used to represent the 10 time range slices | |
84 | */ | |
eb63f5ff | 85 | protected Color[] fColors; |
73005152 | 86 | |
df0b8ff4 BH |
87 | // ------------------------------------------------------------------------ |
88 | // Constructors | |
89 | // ------------------------------------------------------------------------ | |
90 | ||
91 | /** | |
92 | * Creates a drawable tool tip instance. | |
abbdd66a | 93 | * |
eb63f5ff | 94 | * @param parent The parent composite. |
df0b8ff4 | 95 | */ |
eb63f5ff BH |
96 | public DrawableToolTip(Composite parent) { |
97 | fParent = parent; | |
98 | fToolTipShell = new Shell(fParent.getShell(), SWT.ON_TOP); | |
99 | fToolTipShell.setLayout(new RowLayout()); | |
100 | fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
101 | fToolTipShell.addPaintListener(this); | |
102 | fToolTipShell.setSize(200, 50); | |
73005152 | 103 | |
eb63f5ff BH |
104 | fColors = new Color[10]; |
105 | fColors[0] = new Color(Display.getDefault(), 255, 229, 229); | |
106 | fColors[1] = new Color(Display.getDefault(), 255, 204, 204); | |
107 | fColors[2] = new Color(Display.getDefault(), 255, 178, 178); | |
108 | fColors[3] = new Color(Display.getDefault(), 255, 153, 153); | |
109 | fColors[4] = new Color(Display.getDefault(), 255, 127, 127); | |
110 | fColors[5] = new Color(Display.getDefault(), 255, 102, 102); | |
111 | fColors[6] = new Color(Display.getDefault(), 255, 76, 76); | |
112 | fColors[7] = new Color(Display.getDefault(), 255, 51, 51); | |
113 | fColors[8] = new Color(Display.getDefault(), 255, 25, 25); | |
114 | fColors[9] = new Color(Display.getDefault(), 255, 0, 0); | |
73005152 BH |
115 | } |
116 | ||
df0b8ff4 BH |
117 | // ------------------------------------------------------------------------ |
118 | // Methods | |
119 | // ------------------------------------------------------------------------ | |
120 | /** | |
121 | * Returns the message to display. | |
abbdd66a | 122 | * |
df0b8ff4 BH |
123 | * @return the message to display. |
124 | */ | |
125 | public String getText() { | |
eb63f5ff | 126 | return fMessage; |
df0b8ff4 BH |
127 | } |
128 | ||
129 | /** | |
130 | * Returns teh current time to display. | |
abbdd66a | 131 | * |
df0b8ff4 BH |
132 | * @return the current time to display |
133 | */ | |
134 | public String getAccessibleText() { | |
eb63f5ff | 135 | return fCurrentValue.toString(); |
df0b8ff4 BH |
136 | } |
137 | ||
138 | /** | |
139 | * Returns the horizontal margin. | |
abbdd66a | 140 | * |
df0b8ff4 BH |
141 | * @return the horizontal margin. |
142 | */ | |
143 | protected static int getHorizontalMargin() { | |
eb63f5ff | 144 | return fHorMargin; |
df0b8ff4 BH |
145 | } |
146 | ||
147 | /** | |
148 | * Sets the horizontal margin. | |
abbdd66a | 149 | * |
df0b8ff4 BH |
150 | * @param margin The margin to set. |
151 | */ | |
152 | protected static void setHorizontalMargin(int margin) { | |
eb63f5ff | 153 | fHorMargin = margin; |
df0b8ff4 BH |
154 | } |
155 | ||
156 | /** | |
157 | * Returns the vertical margin. | |
abbdd66a | 158 | * |
df0b8ff4 BH |
159 | * @return the vertical margin. |
160 | */ | |
161 | protected static int getVerticalMargin() { | |
eb63f5ff | 162 | return fVertMargin; |
df0b8ff4 BH |
163 | } |
164 | ||
165 | /** | |
166 | * Sets the vertical margin. | |
abbdd66a | 167 | * |
df0b8ff4 BH |
168 | * @param margin The margin to set. |
169 | */ | |
170 | protected static void setVerticalMargin(int margin) { | |
eb63f5ff | 171 | fVertMargin = margin; |
df0b8ff4 BH |
172 | } |
173 | ||
174 | /** | |
175 | * Returns the text scale margin. | |
abbdd66a | 176 | * |
df0b8ff4 BH |
177 | * @return the text scale margin. |
178 | */ | |
179 | protected static int getTextScaleMargin() { | |
eb63f5ff | 180 | return fTextScaleMargin; |
df0b8ff4 BH |
181 | } |
182 | ||
183 | /** | |
184 | * Sets the text scale margin. | |
185 | * @param textScaleMargin The margin to set. | |
186 | */ | |
187 | protected static void setTestScaleMargin(int textScaleMargin) { | |
eb63f5ff | 188 | fTextScaleMargin = textScaleMargin; |
df0b8ff4 BH |
189 | } |
190 | ||
191 | /** | |
192 | * Returns the scale length. | |
abbdd66a | 193 | * |
df0b8ff4 BH |
194 | * @return the scale length. |
195 | */ | |
196 | protected static int getScaleLength() { | |
eb63f5ff | 197 | return fScaleLength; |
df0b8ff4 BH |
198 | } |
199 | ||
200 | /** | |
201 | * Sets the scale length. | |
abbdd66a | 202 | * |
df0b8ff4 BH |
203 | * @param scaleLength The scale length to set. |
204 | */ | |
205 | protected static void setScaleLength(int scaleLength) { | |
eb63f5ff | 206 | fScaleLength = scaleLength; |
df0b8ff4 | 207 | } |
abbdd66a | 208 | |
73005152 BH |
209 | /** |
210 | * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay | |
211 | * on screen until it is told otherwise | |
abbdd66a | 212 | * |
0d9a6d76 FC |
213 | * @param value the current in the scale |
214 | * @param min the scale min | |
215 | * @param max the scale max | |
3bd46eef | 216 | * @since 2.0 |
73005152 | 217 | */ |
0d9a6d76 | 218 | public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) { |
4593bd5b AM |
219 | fMinMaxRange = new TmfTimeRange(min, max); |
220 | fCurrentValue = value; | |
73005152 | 221 | |
eb63f5ff BH |
222 | int w = fToolTipShell.getBounds().width; |
223 | int h = fToolTipShell.getBounds().height; | |
73005152 | 224 | Point hr = Display.getDefault().getCursorLocation(); |
eb63f5ff BH |
225 | fToolTipShell.setBounds(hr.x, hr.y + 26, w, h); |
226 | fToolTipShell.setVisible(true); | |
73005152 BH |
227 | } |
228 | ||
229 | /** | |
df0b8ff4 | 230 | * Hide the tooltip. |
73005152 BH |
231 | */ |
232 | public void hideToolTip() { | |
eb63f5ff | 233 | fToolTipShell.setVisible(false); |
73005152 BH |
234 | } |
235 | ||
236 | /** | |
df0b8ff4 BH |
237 | * Disposes the system resource used by this kind of toolTips (a colors array essentially) |
238 | */ | |
239 | public void dispose() { | |
abbdd66a | 240 | for (int i = 0; i < fColors.length; i++) { |
eb63f5ff | 241 | fColors[i].dispose(); |
abbdd66a | 242 | } |
df0b8ff4 | 243 | } |
abbdd66a | 244 | |
df0b8ff4 BH |
245 | /* |
246 | * (non-Javadoc) | |
247 | * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent) | |
73005152 BH |
248 | */ |
249 | @Override | |
250 | public void paintControl(PaintEvent event) { | |
eb63f5ff BH |
251 | fMessage = SDMessages._138 + " " + fCurrentValue.toString(); //$NON-NLS-1$ |
252 | Point size = event.gc.textExtent(fMessage); | |
253 | if (size.x < fScaleLength) { | |
254 | size.x = fScaleLength; | |
df0b8ff4 | 255 | } |
eb63f5ff BH |
256 | event.gc.drawText(fMessage, fHorMargin, fVertMargin, true); |
257 | event.gc.drawLine(fHorMargin, fVertMargin + fTextScaleMargin + size.y, fHorMargin + fScaleLength, fVertMargin + fTextScaleMargin + size.y); | |
73005152 | 258 | |
eb63f5ff | 259 | int step = fScaleLength / 10; |
73005152 BH |
260 | |
261 | // double gr = (max - min) / 10; | |
abbdd66a | 262 | ITmfTimestamp minMaxdelta = fMinMaxRange.getEndTime().getDelta(fMinMaxRange.getStartTime()); |
73005152 BH |
263 | double gr = (minMaxdelta.getValue()) / (double) 10; |
264 | ||
265 | // double delta = currentValue-min; | |
abbdd66a | 266 | ITmfTimestamp delta = fCurrentValue.getDelta(fMinMaxRange.getStartTime()); |
73005152 | 267 | long absDelta = Math.abs(delta.getValue()); |
abbdd66a | 268 | |
73005152 BH |
269 | int colIndex = 0; |
270 | if (gr != 0) { | |
271 | // colIndex = Math.round((float)(Math.log(1+delta)/gr)); | |
272 | colIndex = Math.round((float) (absDelta / gr)); | |
eb63f5ff BH |
273 | if (colIndex > fColors.length) { |
274 | colIndex = fColors.length; | |
df0b8ff4 | 275 | } else if (colIndex <= 1) { |
73005152 | 276 | colIndex = 1; |
df0b8ff4 BH |
277 | } |
278 | } else { | |
73005152 | 279 | colIndex = 1; |
df0b8ff4 | 280 | } |
73005152 BH |
281 | |
282 | for (int i = 0; i <= 10; i++) { | |
df0b8ff4 | 283 | if (i < 10) { |
eb63f5ff | 284 | event.gc.setBackground(fColors[i]); |
df0b8ff4 BH |
285 | } |
286 | if ((i < colIndex) && (i < 10)) { | |
eb63f5ff | 287 | event.gc.fillRectangle(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - 5, step, 11); |
df0b8ff4 BH |
288 | } |
289 | if (i == 0) { | |
eb63f5ff | 290 | event.gc.drawText(SDMessages._56, fHorMargin, size.y + 2 * fVertMargin + fTextScaleMargin, true); |
df0b8ff4 | 291 | } |
73005152 BH |
292 | if (i == 0) { |
293 | int len = event.gc.textExtent(SDMessages._55).x; | |
eb63f5ff | 294 | event.gc.drawText(SDMessages._55, fHorMargin + fScaleLength - len + 1, size.y + 2 * fVertMargin + fTextScaleMargin, true); |
73005152 BH |
295 | } |
296 | int lineWidth = 10; | |
df0b8ff4 | 297 | if ((i == 0) || (i == 10)) { |
73005152 | 298 | lineWidth = 14; |
df0b8ff4 | 299 | } |
eb63f5ff | 300 | event.gc.drawLine(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - lineWidth / 2, fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y + lineWidth / 2); |
73005152 | 301 | } |
eb63f5ff | 302 | fToolTipShell.setSize(size.x + 2 * fHorMargin + 2, 2 * size.y + 3 * fVertMargin + fTextScaleMargin); |
73005152 | 303 | } |
73005152 | 304 | } |