Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e68a8ced2a9f899b0b7c5bafc0e8a48eaa1d3fbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.window;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;

/**
 * Default implementation of ToolTip that provides an iconofied label with font
 * and color controls by subclass.
 * 
 * @since 3.3 <strong>EXPERIMENTAL</strong> This class or interface has been
 *        added as part of a work in progress. This API may change at any given
 *        time. Please do not use this API without consulting with the
 *        Platform/UI team.
 */
public class DefaultToolTip extends ToolTip {
	private String text;

	private Color backgroundColor;

	private Font font;

	private Image backgroundImage;

	private Color foregroundColor;

	private Image image;

	private int style = SWT.SHADOW_NONE;

	/**
	 * Create new instance which add TooltipSupport to the widget
	 * 
	 * @param control the control on whose action the tooltip is shown
	 */
	public DefaultToolTip(Control control) {
		super(control);
	}

	/**
	 * Create new instance which add TooltipSupport to the widget
	 * 
	 * @param control the control to which the tooltip is bound
	 * @param style style passed to control tooltip behaviour
	 * @param manualActivation <code>true</code> if the activation is done manually using
	 *            {@link #show(Point)}
	 * @see #RECREATE
	 * @see #NO_RECREATE
	 */
	public DefaultToolTip(Control control, int style, boolean manualActivation) {
		super(control, style, manualActivation);
	}
	
	/**
	 * Creates the content are of the the tooltip. By default this creates a
	 * CLabel to display text. To customize the text Subclasses may override the
	 * following methods
	 * <ul>
	 * <li>{@link #getStyle(Event)}</li>
	 * <li>{@link #getBackgroundColor(Event)}</li>
	 * <li>{@link #getForegroundColor(Event)}</li>
	 * <li>{@link #getFont(Event)}</li>
	 * <li>{@link #getImage(Event)}</li>
	 * <li>{@link #getText(Event)}</li>
	 * <li>{@link #getBackgroundImage(Event)}</li>
	 * </ul>
	 * 
	 * @param event
	 *            the event that triggered the activation of the tooltip
	 * @param parent
	 *            the parent of the content area
	 * @return the content area created
	 */
	protected Composite createToolTipContentArea(Event event, Composite parent) {
		Image image = getImage(event);
		Image bgImage = getBackgroundImage(event);
		String text = getText(event);
		Color fgColor = getForegroundColor(event);
		Color bgColor = getBackgroundColor(event);
		Font font = getFont(event);

		CLabel label = new CLabel(parent, getStyle(event));
		if (text != null) {
			label.setText(text);
		}

		if (image != null) {
			label.setImage(image);
		}

		if (fgColor != null) {
			label.setForeground(fgColor);
		}

		if (bgColor != null) {
			label.setBackground(bgColor);
		}

		if (bgImage != null) {
			label.setBackgroundImage(image);
		}

		if (font != null) {
			label.setFont(font);
		}

		return label;
	}

	/**
	 * The style used to create the {@link CLabel} in the default implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the style
	 */
	protected int getStyle(Event event) {
		return style;
	}

	/**
	 * The {@link Image} displayed in the {@link CLabel} in the default
	 * implementation implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the {@link Image} or <code>null</code> if no image should be
	 *         displayed
	 */
	protected Image getImage(Event event) {
		return image;
	}

	/**
	 * The foreground {@link Color} used by {@link CLabel} in the default
	 * implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the {@link Color} or <code>null</code> if default foreground
	 *         color should be used
	 */
	protected Color getForegroundColor(Event event) {
		return (foregroundColor == null) ? event.widget.getDisplay()
				.getSystemColor(SWT.COLOR_INFO_FOREGROUND) : foregroundColor;
	}

	/**
	 * The background {@link Color} used by {@link CLabel} in the default
	 * implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the {@link Color} or <code>null</code> if default background
	 *         color should be used
	 */
	protected Color getBackgroundColor(Event event) {
		return (backgroundColor == null) ? event.widget.getDisplay()
				.getSystemColor(SWT.COLOR_INFO_BACKGROUND) : backgroundColor;
	}

	/**
	 * The background {@link Image} used by {@link CLabel} in the default
	 * implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the {@link Image} or <code>null</code> if no image should be
	 *         displayed in the background
	 */
	protected Image getBackgroundImage(Event event) {
		return backgroundImage;
	}

	/**
	 * The {@link Font} used by {@link CLabel} in the default implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the {@link Font} or <code>null</code> if the default font
	 *         should be used
	 */
	protected Font getFont(Event event) {
		return font;
	}

	/**
	 * The text displayed in the {@link CLabel} in the default implementation
	 * 
	 * @param event
	 *            the event triggered the popup of the tooltip
	 * @return the text or <code>null</code> if no text has to be displayed
	 */
	protected String getText(Event event) {
		return text;
	}

	/**
	 * The background {@link Image} used by {@link CLabel} in the default
	 * implementation
	 * 
	 * @param backgroundColor
	 *            the {@link Color} or <code>null</code> if default background
	 *            color ({@link SWT#COLOR_INFO_BACKGROUND}) should be used
	 */
	public void setBackgroundColor(Color backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	/**
	 * The background {@link Image} used by {@link CLabel} in the default
	 * implementation
	 * 
	 * @param backgroundImage
	 *            the {@link Image} or <code>null</code> if no image should be
	 *            displayed in the background
	 */
	public void setBackgroundImage(Image backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	/**
	 * The {@link Font} used by {@link CLabel} in the default implementation
	 * 
	 * @param font
	 *            the {@link Font} or <code>null</code> if the default font
	 *            should be used
	 */
	public void setFont(Font font) {
		this.font = font;
	}

	/**
	 * The foreground {@link Color} used by {@link CLabel} in the default
	 * implementation
	 * 
	 * @param foregroundColor
	 *            the {@link Color} or <code>null</code> if default foreground
	 *            color should be used
	 */
	public void setForegroundColor(Color foregroundColor) {
		this.foregroundColor = foregroundColor;
	}

	/**
	 * The {@link Image} displayed in the {@link CLabel} in the default
	 * implementation implementation
	 * 
	 * @param image
	 *            the {@link Image} or <code>null</code> if no image should be
	 *            displayed
	 */
	public void setImage(Image image) {
		this.image = image;
	}

	/**
	 * The style used to create the {@link CLabel} in the default implementation
	 * 
	 * @param style
	 *            the event triggered the popup of the tooltip
	 */
	public void setStyle(int style) {
		this.style = style;
	}

	/**
	 * The text displayed in the {@link CLabel} in the default implementation
	 * 
	 * @param text
	 *            the text or <code>null</code> if no text has to be displayed
	 */
	public void setText(String text) {
		this.text = text;
	}

}

Back to the top