Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
ed902a2b | 2 | * Copyright (c) 2005, 2014 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 | |
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ui.views.uml2sd; |
73005152 | 14 | |
73005152 BH |
15 | import org.eclipse.swt.SWT; |
16 | import org.eclipse.swt.events.PaintEvent; | |
17 | import org.eclipse.swt.events.PaintListener; | |
18 | import org.eclipse.swt.graphics.Color; | |
19 | import org.eclipse.swt.graphics.Point; | |
20 | import org.eclipse.swt.layout.RowLayout; | |
21 | import org.eclipse.swt.widgets.Composite; | |
22 | import org.eclipse.swt.widgets.Display; | |
23 | import org.eclipse.swt.widgets.Shell; | |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
25 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
6cfc180e | 26 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
2bdf0193 | 27 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.util.Messages; |
73005152 BH |
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 | ||
f26e290b BH |
42 | // ------------------------------------------------------------------------ |
43 | // Constants | |
44 | // ------------------------------------------------------------------------ | |
45 | private static final int HORIZONTAL_MARGIN = 10; | |
46 | private static final int VERTICAL_MARGIN = 10; | |
47 | private static final int TEXT_SCALE_MARGIN = 20; | |
48 | private static final int SCALE_LENGTH = 100; | |
49 | private static final int SHELL_WIDTH = 200; | |
50 | private static final int SHELL_HEIGHT = 50; | |
51 | private static final int NUMBER_STEPS = 10; | |
52 | private static final int BASE_RED_VALUE = 255; | |
53 | private static final int BASE_GREEN_BLUE_VALUE = 225; | |
54 | private static final int COLOR_STEP = 25; | |
55 | private static final int BOUNDS_Y_OFFSET = 26; | |
56 | private static final int RECTANGLE_HEIGHT = 11; | |
57 | private static final int DEFAULT_LINE_WIDTH = 10; | |
58 | private static final int BORDER_LINE_WIDTH = 14; | |
59 | ||
df0b8ff4 BH |
60 | // ------------------------------------------------------------------------ |
61 | // Attributes | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
73005152 BH |
64 | /** |
65 | * The tooltip shell | |
66 | */ | |
cab6c8ff | 67 | private Shell fToolTipShell = null; |
73005152 | 68 | /** |
df0b8ff4 | 69 | * The Time range data. |
73005152 | 70 | */ |
cab6c8ff | 71 | private TmfTimeRange fMinMaxRange; |
df0b8ff4 BH |
72 | /** |
73 | * The current time. | |
74 | */ | |
cab6c8ff | 75 | private ITmfTimestamp fCurrentValue; |
df0b8ff4 BH |
76 | /** |
77 | * The horizontal margin used for drawing. | |
78 | */ | |
f26e290b | 79 | private static int fHorMargin = HORIZONTAL_MARGIN; |
df0b8ff4 BH |
80 | /** |
81 | * The vertical margin used for drawing. | |
82 | */ | |
f26e290b | 83 | private static int fVertMargin = VERTICAL_MARGIN; |
df0b8ff4 BH |
84 | /** |
85 | * The minimum text scale margin. | |
86 | */ | |
f26e290b | 87 | private static int fTextScaleMargin = TEXT_SCALE_MARGIN; |
df0b8ff4 BH |
88 | /** |
89 | * The length of the text scale. | |
90 | */ | |
f26e290b | 91 | private static int fScaleLength = SCALE_LENGTH; |
df0b8ff4 | 92 | /** |
abbdd66a | 93 | * The text to display |
df0b8ff4 | 94 | */ |
cab6c8ff | 95 | private String fMessage; |
73005152 BH |
96 | /** |
97 | * The color array used to represent the 10 time range slices | |
98 | */ | |
cab6c8ff | 99 | private Color[] fColors; |
73005152 | 100 | |
df0b8ff4 BH |
101 | // ------------------------------------------------------------------------ |
102 | // Constructors | |
103 | // ------------------------------------------------------------------------ | |
104 | ||
105 | /** | |
106 | * Creates a drawable tool tip instance. | |
abbdd66a | 107 | * |
eb63f5ff | 108 | * @param parent The parent composite. |
df0b8ff4 | 109 | */ |
eb63f5ff | 110 | public DrawableToolTip(Composite parent) { |
f26e290b | 111 | fToolTipShell = new Shell(parent.getShell(), SWT.ON_TOP); |
eb63f5ff BH |
112 | fToolTipShell.setLayout(new RowLayout()); |
113 | fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
114 | fToolTipShell.addPaintListener(this); | |
f26e290b | 115 | fToolTipShell.setSize(SHELL_WIDTH, SHELL_HEIGHT); |
73005152 | 116 | |
f26e290b BH |
117 | fColors = new Color[NUMBER_STEPS]; |
118 | int greenBlue = BASE_GREEN_BLUE_VALUE; | |
119 | final int step = COLOR_STEP; | |
120 | for (int i = 0; i < fColors.length; i++) { | |
121 | fColors[i] = new Color(Display.getDefault(), BASE_RED_VALUE, greenBlue, greenBlue); | |
122 | greenBlue -= step; | |
123 | } | |
73005152 BH |
124 | } |
125 | ||
df0b8ff4 BH |
126 | // ------------------------------------------------------------------------ |
127 | // Methods | |
128 | // ------------------------------------------------------------------------ | |
129 | /** | |
130 | * Returns the message to display. | |
abbdd66a | 131 | * |
df0b8ff4 BH |
132 | * @return the message to display. |
133 | */ | |
134 | public String getText() { | |
eb63f5ff | 135 | return fMessage; |
df0b8ff4 BH |
136 | } |
137 | ||
138 | /** | |
139 | * Returns teh current time to display. | |
abbdd66a | 140 | * |
df0b8ff4 BH |
141 | * @return the current time to display |
142 | */ | |
143 | public String getAccessibleText() { | |
eb63f5ff | 144 | return fCurrentValue.toString(); |
df0b8ff4 BH |
145 | } |
146 | ||
147 | /** | |
148 | * Returns the horizontal margin. | |
abbdd66a | 149 | * |
df0b8ff4 BH |
150 | * @return the horizontal margin. |
151 | */ | |
152 | protected static int getHorizontalMargin() { | |
eb63f5ff | 153 | return fHorMargin; |
df0b8ff4 BH |
154 | } |
155 | ||
156 | /** | |
157 | * Sets the horizontal margin. | |
abbdd66a | 158 | * |
df0b8ff4 BH |
159 | * @param margin The margin to set. |
160 | */ | |
161 | protected static void setHorizontalMargin(int margin) { | |
eb63f5ff | 162 | fHorMargin = margin; |
df0b8ff4 BH |
163 | } |
164 | ||
165 | /** | |
166 | * Returns the vertical margin. | |
abbdd66a | 167 | * |
df0b8ff4 BH |
168 | * @return the vertical margin. |
169 | */ | |
170 | protected static int getVerticalMargin() { | |
eb63f5ff | 171 | return fVertMargin; |
df0b8ff4 BH |
172 | } |
173 | ||
174 | /** | |
175 | * Sets the vertical margin. | |
abbdd66a | 176 | * |
df0b8ff4 BH |
177 | * @param margin The margin to set. |
178 | */ | |
179 | protected static void setVerticalMargin(int margin) { | |
eb63f5ff | 180 | fVertMargin = margin; |
df0b8ff4 BH |
181 | } |
182 | ||
183 | /** | |
184 | * Returns the text scale margin. | |
abbdd66a | 185 | * |
df0b8ff4 BH |
186 | * @return the text scale margin. |
187 | */ | |
188 | protected static int getTextScaleMargin() { | |
eb63f5ff | 189 | return fTextScaleMargin; |
df0b8ff4 BH |
190 | } |
191 | ||
192 | /** | |
193 | * Sets the text scale margin. | |
194 | * @param textScaleMargin The margin to set. | |
195 | */ | |
196 | protected static void setTestScaleMargin(int textScaleMargin) { | |
eb63f5ff | 197 | fTextScaleMargin = textScaleMargin; |
df0b8ff4 BH |
198 | } |
199 | ||
200 | /** | |
201 | * Returns the scale length. | |
abbdd66a | 202 | * |
df0b8ff4 BH |
203 | * @return the scale length. |
204 | */ | |
205 | protected static int getScaleLength() { | |
eb63f5ff | 206 | return fScaleLength; |
df0b8ff4 BH |
207 | } |
208 | ||
209 | /** | |
210 | * Sets the scale length. | |
abbdd66a | 211 | * |
df0b8ff4 BH |
212 | * @param scaleLength The scale length to set. |
213 | */ | |
214 | protected static void setScaleLength(int scaleLength) { | |
eb63f5ff | 215 | fScaleLength = scaleLength; |
df0b8ff4 | 216 | } |
abbdd66a | 217 | |
73005152 BH |
218 | /** |
219 | * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay | |
220 | * on screen until it is told otherwise | |
abbdd66a | 221 | * |
0d9a6d76 FC |
222 | * @param value the current in the scale |
223 | * @param min the scale min | |
224 | * @param max the scale max | |
73005152 | 225 | */ |
0d9a6d76 | 226 | public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) { |
6cfc180e GB |
227 | ITmfTimestamp minTime = min; |
228 | ITmfTimestamp maxTime = max; | |
229 | if (minTime == null) { | |
230 | minTime = TmfTimestamp.BIG_BANG; | |
231 | } | |
232 | if (maxTime == null) { | |
233 | maxTime = TmfTimestamp.BIG_CRUNCH; | |
234 | } | |
235 | fMinMaxRange = new TmfTimeRange(minTime, maxTime); | |
4593bd5b | 236 | fCurrentValue = value; |
73005152 | 237 | |
eb63f5ff BH |
238 | int w = fToolTipShell.getBounds().width; |
239 | int h = fToolTipShell.getBounds().height; | |
73005152 | 240 | Point hr = Display.getDefault().getCursorLocation(); |
f26e290b | 241 | fToolTipShell.setBounds(hr.x, hr.y + BOUNDS_Y_OFFSET, w, h); |
eb63f5ff | 242 | fToolTipShell.setVisible(true); |
73005152 BH |
243 | } |
244 | ||
245 | /** | |
df0b8ff4 | 246 | * Hide the tooltip. |
73005152 BH |
247 | */ |
248 | public void hideToolTip() { | |
eb63f5ff | 249 | fToolTipShell.setVisible(false); |
73005152 BH |
250 | } |
251 | ||
252 | /** | |
df0b8ff4 BH |
253 | * Disposes the system resource used by this kind of toolTips (a colors array essentially) |
254 | */ | |
255 | public void dispose() { | |
abbdd66a | 256 | for (int i = 0; i < fColors.length; i++) { |
eb63f5ff | 257 | fColors[i].dispose(); |
abbdd66a | 258 | } |
df0b8ff4 | 259 | } |
abbdd66a | 260 | |
73005152 BH |
261 | @Override |
262 | public void paintControl(PaintEvent event) { | |
92330441 | 263 | fMessage = Messages.SequenceDiagram_Delta + " " + fCurrentValue.toString(); //$NON-NLS-1$ |
eb63f5ff BH |
264 | Point size = event.gc.textExtent(fMessage); |
265 | if (size.x < fScaleLength) { | |
266 | size.x = fScaleLength; | |
df0b8ff4 | 267 | } |
eb63f5ff BH |
268 | event.gc.drawText(fMessage, fHorMargin, fVertMargin, true); |
269 | event.gc.drawLine(fHorMargin, fVertMargin + fTextScaleMargin + size.y, fHorMargin + fScaleLength, fVertMargin + fTextScaleMargin + size.y); | |
73005152 | 270 | |
f26e290b | 271 | int step = fScaleLength / NUMBER_STEPS; |
73005152 | 272 | |
abbdd66a | 273 | ITmfTimestamp minMaxdelta = fMinMaxRange.getEndTime().getDelta(fMinMaxRange.getStartTime()); |
f26e290b | 274 | double gr = (minMaxdelta.getValue()) / (double) NUMBER_STEPS; |
73005152 | 275 | |
abbdd66a | 276 | ITmfTimestamp delta = fCurrentValue.getDelta(fMinMaxRange.getStartTime()); |
73005152 | 277 | long absDelta = Math.abs(delta.getValue()); |
abbdd66a | 278 | |
73005152 BH |
279 | int colIndex = 0; |
280 | if (gr != 0) { | |
73005152 | 281 | colIndex = Math.round((float) (absDelta / gr)); |
eb63f5ff BH |
282 | if (colIndex > fColors.length) { |
283 | colIndex = fColors.length; | |
df0b8ff4 | 284 | } else if (colIndex <= 1) { |
73005152 | 285 | colIndex = 1; |
df0b8ff4 BH |
286 | } |
287 | } else { | |
73005152 | 288 | colIndex = 1; |
df0b8ff4 | 289 | } |
73005152 | 290 | |
f26e290b BH |
291 | for (int i = 0; i <= NUMBER_STEPS; i++) { |
292 | if (i < NUMBER_STEPS) { | |
eb63f5ff | 293 | event.gc.setBackground(fColors[i]); |
df0b8ff4 | 294 | } |
f26e290b BH |
295 | if ((i < colIndex) && (i < NUMBER_STEPS)) { |
296 | event.gc.fillRectangle(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - 5, step, RECTANGLE_HEIGHT); | |
df0b8ff4 BH |
297 | } |
298 | if (i == 0) { | |
92330441 | 299 | event.gc.drawText(Messages.SequenceDiagram_Min, fHorMargin, size.y + 2 * fVertMargin + fTextScaleMargin, true); |
df0b8ff4 | 300 | } |
73005152 | 301 | if (i == 0) { |
92330441 AM |
302 | int len = event.gc.textExtent(Messages.SequenceDiagram_Max).x; |
303 | event.gc.drawText(Messages.SequenceDiagram_Max, fHorMargin + fScaleLength - len + 1, size.y + 2 * fVertMargin + fTextScaleMargin, true); | |
73005152 | 304 | } |
f26e290b BH |
305 | int lineWidth = DEFAULT_LINE_WIDTH; |
306 | if ((i == 0) || (i == NUMBER_STEPS)) { | |
307 | lineWidth = BORDER_LINE_WIDTH; | |
df0b8ff4 | 308 | } |
eb63f5ff | 309 | event.gc.drawLine(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - lineWidth / 2, fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y + lineWidth / 2); |
73005152 | 310 | } |
eb63f5ff | 311 | fToolTipShell.setSize(size.x + 2 * fHorMargin + 2, 2 * size.y + 3 * fVertMargin + fTextScaleMargin); |
73005152 | 312 | } |
73005152 | 313 | } |