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