tmf: Bug 490400: Leaking widgets due to incorrect cleanup in dispose()
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / DrawableToolTip.java
CommitLineData
73005152 1/**********************************************************************
25033fef 2 * Copyright (c) 2005, 2016 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 13package org.eclipse.tracecompass.tmf.ui.views.uml2sd;
73005152 14
73005152
BH
15import org.eclipse.swt.SWT;
16import org.eclipse.swt.events.PaintEvent;
17import org.eclipse.swt.events.PaintListener;
18import org.eclipse.swt.graphics.Color;
19import org.eclipse.swt.graphics.Point;
20import org.eclipse.swt.layout.RowLayout;
21import org.eclipse.swt.widgets.Composite;
22import org.eclipse.swt.widgets.Display;
23import org.eclipse.swt.widgets.Shell;
2bdf0193
AM
24import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
25import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
6cfc180e 26import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
2bdf0193 27import 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 */
40public 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);
25033fef
PT
116 fToolTipShell.addDisposeListener((e) -> {
117 for (int i = 0; i < fColors.length; i++) {
118 fColors[i].dispose();
119 }
120 });
73005152 121
f26e290b
BH
122 fColors = new Color[NUMBER_STEPS];
123 int greenBlue = BASE_GREEN_BLUE_VALUE;
124 final int step = COLOR_STEP;
125 for (int i = 0; i < fColors.length; i++) {
126 fColors[i] = new Color(Display.getDefault(), BASE_RED_VALUE, greenBlue, greenBlue);
127 greenBlue -= step;
128 }
73005152
BH
129 }
130
df0b8ff4
BH
131 // ------------------------------------------------------------------------
132 // Methods
133 // ------------------------------------------------------------------------
134 /**
135 * Returns the message to display.
abbdd66a 136 *
df0b8ff4
BH
137 * @return the message to display.
138 */
139 public String getText() {
eb63f5ff 140 return fMessage;
df0b8ff4
BH
141 }
142
143 /**
144 * Returns teh current time to display.
abbdd66a 145 *
df0b8ff4
BH
146 * @return the current time to display
147 */
148 public String getAccessibleText() {
eb63f5ff 149 return fCurrentValue.toString();
df0b8ff4
BH
150 }
151
152 /**
153 * Returns the horizontal margin.
abbdd66a 154 *
df0b8ff4
BH
155 * @return the horizontal margin.
156 */
157 protected static int getHorizontalMargin() {
eb63f5ff 158 return fHorMargin;
df0b8ff4
BH
159 }
160
161 /**
162 * Sets the horizontal margin.
abbdd66a 163 *
df0b8ff4
BH
164 * @param margin The margin to set.
165 */
166 protected static void setHorizontalMargin(int margin) {
eb63f5ff 167 fHorMargin = margin;
df0b8ff4
BH
168 }
169
170 /**
171 * Returns the vertical margin.
abbdd66a 172 *
df0b8ff4
BH
173 * @return the vertical margin.
174 */
175 protected static int getVerticalMargin() {
eb63f5ff 176 return fVertMargin;
df0b8ff4
BH
177 }
178
179 /**
180 * Sets the vertical margin.
abbdd66a 181 *
df0b8ff4
BH
182 * @param margin The margin to set.
183 */
184 protected static void setVerticalMargin(int margin) {
eb63f5ff 185 fVertMargin = margin;
df0b8ff4
BH
186 }
187
188 /**
189 * Returns the text scale margin.
abbdd66a 190 *
df0b8ff4
BH
191 * @return the text scale margin.
192 */
193 protected static int getTextScaleMargin() {
eb63f5ff 194 return fTextScaleMargin;
df0b8ff4
BH
195 }
196
197 /**
198 * Sets the text scale margin.
199 * @param textScaleMargin The margin to set.
200 */
201 protected static void setTestScaleMargin(int textScaleMargin) {
eb63f5ff 202 fTextScaleMargin = textScaleMargin;
df0b8ff4
BH
203 }
204
205 /**
206 * Returns the scale length.
abbdd66a 207 *
df0b8ff4
BH
208 * @return the scale length.
209 */
210 protected static int getScaleLength() {
eb63f5ff 211 return fScaleLength;
df0b8ff4
BH
212 }
213
214 /**
215 * Sets the scale length.
abbdd66a 216 *
df0b8ff4
BH
217 * @param scaleLength The scale length to set.
218 */
219 protected static void setScaleLength(int scaleLength) {
eb63f5ff 220 fScaleLength = scaleLength;
df0b8ff4 221 }
abbdd66a 222
73005152
BH
223 /**
224 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
225 * on screen until it is told otherwise
abbdd66a 226 *
0d9a6d76
FC
227 * @param value the current in the scale
228 * @param min the scale min
229 * @param max the scale max
73005152 230 */
0d9a6d76 231 public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) {
6cfc180e
GB
232 ITmfTimestamp minTime = min;
233 ITmfTimestamp maxTime = max;
234 if (minTime == null) {
235 minTime = TmfTimestamp.BIG_BANG;
236 }
237 if (maxTime == null) {
238 maxTime = TmfTimestamp.BIG_CRUNCH;
239 }
240 fMinMaxRange = new TmfTimeRange(minTime, maxTime);
4593bd5b 241 fCurrentValue = value;
73005152 242
eb63f5ff
BH
243 int w = fToolTipShell.getBounds().width;
244 int h = fToolTipShell.getBounds().height;
73005152 245 Point hr = Display.getDefault().getCursorLocation();
f26e290b 246 fToolTipShell.setBounds(hr.x, hr.y + BOUNDS_Y_OFFSET, w, h);
eb63f5ff 247 fToolTipShell.setVisible(true);
73005152
BH
248 }
249
250 /**
df0b8ff4 251 * Hide the tooltip.
73005152
BH
252 */
253 public void hideToolTip() {
eb63f5ff 254 fToolTipShell.setVisible(false);
73005152
BH
255 }
256
257 /**
25033fef 258 * Dispose this tool tip
df0b8ff4
BH
259 */
260 public void dispose() {
25033fef 261 fToolTipShell.dispose();
df0b8ff4 262 }
abbdd66a 263
73005152
BH
264 @Override
265 public void paintControl(PaintEvent event) {
92330441 266 fMessage = Messages.SequenceDiagram_Delta + " " + fCurrentValue.toString(); //$NON-NLS-1$
eb63f5ff
BH
267 Point size = event.gc.textExtent(fMessage);
268 if (size.x < fScaleLength) {
269 size.x = fScaleLength;
df0b8ff4 270 }
eb63f5ff
BH
271 event.gc.drawText(fMessage, fHorMargin, fVertMargin, true);
272 event.gc.drawLine(fHorMargin, fVertMargin + fTextScaleMargin + size.y, fHorMargin + fScaleLength, fVertMargin + fTextScaleMargin + size.y);
73005152 273
f26e290b 274 int step = fScaleLength / NUMBER_STEPS;
73005152 275
abbdd66a 276 ITmfTimestamp minMaxdelta = fMinMaxRange.getEndTime().getDelta(fMinMaxRange.getStartTime());
f26e290b 277 double gr = (minMaxdelta.getValue()) / (double) NUMBER_STEPS;
73005152 278
abbdd66a 279 ITmfTimestamp delta = fCurrentValue.getDelta(fMinMaxRange.getStartTime());
73005152 280 long absDelta = Math.abs(delta.getValue());
abbdd66a 281
73005152
BH
282 int colIndex = 0;
283 if (gr != 0) {
73005152 284 colIndex = Math.round((float) (absDelta / gr));
eb63f5ff
BH
285 if (colIndex > fColors.length) {
286 colIndex = fColors.length;
df0b8ff4 287 } else if (colIndex <= 1) {
73005152 288 colIndex = 1;
df0b8ff4
BH
289 }
290 } else {
73005152 291 colIndex = 1;
df0b8ff4 292 }
73005152 293
f26e290b
BH
294 for (int i = 0; i <= NUMBER_STEPS; i++) {
295 if (i < NUMBER_STEPS) {
eb63f5ff 296 event.gc.setBackground(fColors[i]);
df0b8ff4 297 }
f26e290b
BH
298 if ((i < colIndex) && (i < NUMBER_STEPS)) {
299 event.gc.fillRectangle(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - 5, step, RECTANGLE_HEIGHT);
df0b8ff4
BH
300 }
301 if (i == 0) {
92330441 302 event.gc.drawText(Messages.SequenceDiagram_Min, fHorMargin, size.y + 2 * fVertMargin + fTextScaleMargin, true);
df0b8ff4 303 }
73005152 304 if (i == 0) {
92330441
AM
305 int len = event.gc.textExtent(Messages.SequenceDiagram_Max).x;
306 event.gc.drawText(Messages.SequenceDiagram_Max, fHorMargin + fScaleLength - len + 1, size.y + 2 * fVertMargin + fTextScaleMargin, true);
73005152 307 }
f26e290b
BH
308 int lineWidth = DEFAULT_LINE_WIDTH;
309 if ((i == 0) || (i == NUMBER_STEPS)) {
310 lineWidth = BORDER_LINE_WIDTH;
df0b8ff4 311 }
eb63f5ff 312 event.gc.drawLine(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - lineWidth / 2, fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y + lineWidth / 2);
73005152 313 }
eb63f5ff 314 fToolTipShell.setSize(size.x + 2 * fHorMargin + 2, 2 * size.y + 3 * fVertMargin + fTextScaleMargin);
73005152 315 }
73005152 316}
This page took 0.112942 seconds and 5 git commands to generate.