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