KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > window > DefaultToolTip


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jface.window;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CLabel;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Event;
23
24 /**
25  * Default implementation of ToolTip that provides an iconofied label with font
26  * and color controls by subclass.
27  *
28  * @since 3.3
29  */

30 public class DefaultToolTip extends ToolTip {
31     private String JavaDoc text;
32
33     private Color backgroundColor;
34
35     private Font font;
36
37     private Image backgroundImage;
38
39     private Color foregroundColor;
40
41     private Image image;
42
43     private int style = SWT.SHADOW_NONE;
44
45     /**
46      * Create new instance which add TooltipSupport to the widget
47      *
48      * @param control the control on whose action the tooltip is shown
49      */

50     public DefaultToolTip(Control control) {
51         super(control);
52     }
53
54     /**
55      * Create new instance which add TooltipSupport to the widget
56      *
57      * @param control the control to which the tooltip is bound
58      * @param style style passed to control tooltip behaviour
59      * @param manualActivation <code>true</code> if the activation is done manually using
60      * {@link #show(Point)}
61      * @see #RECREATE
62      * @see #NO_RECREATE
63      */

64     public DefaultToolTip(Control control, int style, boolean manualActivation) {
65         super(control, style, manualActivation);
66     }
67     
68     /**
69      * Creates the content are of the the tooltip. By default this creates a
70      * CLabel to display text. To customize the text Subclasses may override the
71      * following methods
72      * <ul>
73      * <li>{@link #getStyle(Event)}</li>
74      * <li>{@link #getBackgroundColor(Event)}</li>
75      * <li>{@link #getForegroundColor(Event)}</li>
76      * <li>{@link #getFont(Event)}</li>
77      * <li>{@link #getImage(Event)}</li>
78      * <li>{@link #getText(Event)}</li>
79      * <li>{@link #getBackgroundImage(Event)}</li>
80      * </ul>
81      *
82      * @param event
83      * the event that triggered the activation of the tooltip
84      * @param parent
85      * the parent of the content area
86      * @return the content area created
87      */

88     protected Composite createToolTipContentArea(Event event, Composite parent) {
89         Image image = getImage(event);
90         Image bgImage = getBackgroundImage(event);
91         String JavaDoc text = getText(event);
92         Color fgColor = getForegroundColor(event);
93         Color bgColor = getBackgroundColor(event);
94         Font font = getFont(event);
95
96         CLabel label = new CLabel(parent, getStyle(event));
97         if (text != null) {
98             label.setText(text);
99         }
100
101         if (image != null) {
102             label.setImage(image);
103         }
104
105         if (fgColor != null) {
106             label.setForeground(fgColor);
107         }
108
109         if (bgColor != null) {
110             label.setBackground(bgColor);
111         }
112
113         if (bgImage != null) {
114             label.setBackgroundImage(image);
115         }
116
117         if (font != null) {
118             label.setFont(font);
119         }
120
121         return label;
122     }
123
124     /**
125      * The style used to create the {@link CLabel} in the default implementation
126      *
127      * @param event
128      * the event triggered the popup of the tooltip
129      * @return the style
130      */

131     protected int getStyle(Event event) {
132         return style;
133     }
134
135     /**
136      * The {@link Image} displayed in the {@link CLabel} in the default
137      * implementation implementation
138      *
139      * @param event
140      * the event triggered the popup of the tooltip
141      * @return the {@link Image} or <code>null</code> if no image should be
142      * displayed
143      */

144     protected Image getImage(Event event) {
145         return image;
146     }
147
148     /**
149      * The foreground {@link Color} used by {@link CLabel} in the default
150      * implementation
151      *
152      * @param event
153      * the event triggered the popup of the tooltip
154      * @return the {@link Color} or <code>null</code> if default foreground
155      * color should be used
156      */

157     protected Color getForegroundColor(Event event) {
158         return (foregroundColor == null) ? event.widget.getDisplay()
159                 .getSystemColor(SWT.COLOR_INFO_FOREGROUND) : foregroundColor;
160     }
161
162     /**
163      * The background {@link Color} used by {@link CLabel} in the default
164      * implementation
165      *
166      * @param event
167      * the event triggered the popup of the tooltip
168      * @return the {@link Color} or <code>null</code> if default background
169      * color should be used
170      */

171     protected Color getBackgroundColor(Event event) {
172         return (backgroundColor == null) ? event.widget.getDisplay()
173                 .getSystemColor(SWT.COLOR_INFO_BACKGROUND) : backgroundColor;
174     }
175
176     /**
177      * The background {@link Image} used by {@link CLabel} in the default
178      * implementation
179      *
180      * @param event
181      * the event triggered the popup of the tooltip
182      * @return the {@link Image} or <code>null</code> if no image should be
183      * displayed in the background
184      */

185     protected Image getBackgroundImage(Event event) {
186         return backgroundImage;
187     }
188
189     /**
190      * The {@link Font} used by {@link CLabel} in the default implementation
191      *
192      * @param event
193      * the event triggered the popup of the tooltip
194      * @return the {@link Font} or <code>null</code> if the default font
195      * should be used
196      */

197     protected Font getFont(Event event) {
198         return font;
199     }
200
201     /**
202      * The text displayed in the {@link CLabel} in the default implementation
203      *
204      * @param event
205      * the event triggered the popup of the tooltip
206      * @return the text or <code>null</code> if no text has to be displayed
207      */

208     protected String JavaDoc getText(Event event) {
209         return text;
210     }
211
212     /**
213      * The background {@link Image} used by {@link CLabel} in the default
214      * implementation
215      *
216      * @param backgroundColor
217      * the {@link Color} or <code>null</code> if default background
218      * color ({@link SWT#COLOR_INFO_BACKGROUND}) should be used
219      */

220     public void setBackgroundColor(Color backgroundColor) {
221         this.backgroundColor = backgroundColor;
222     }
223
224     /**
225      * The background {@link Image} used by {@link CLabel} in the default
226      * implementation
227      *
228      * @param backgroundImage
229      * the {@link Image} or <code>null</code> if no image should be
230      * displayed in the background
231      */

232     public void setBackgroundImage(Image backgroundImage) {
233         this.backgroundImage = backgroundImage;
234     }
235
236     /**
237      * The {@link Font} used by {@link CLabel} in the default implementation
238      *
239      * @param font
240      * the {@link Font} or <code>null</code> if the default font
241      * should be used
242      */

243     public void setFont(Font font) {
244         this.font = font;
245     }
246
247     /**
248      * The foreground {@link Color} used by {@link CLabel} in the default
249      * implementation
250      *
251      * @param foregroundColor
252      * the {@link Color} or <code>null</code> if default foreground
253      * color should be used
254      */

255     public void setForegroundColor(Color foregroundColor) {
256         this.foregroundColor = foregroundColor;
257     }
258
259     /**
260      * The {@link Image} displayed in the {@link CLabel} in the default
261      * implementation implementation
262      *
263      * @param image
264      * the {@link Image} or <code>null</code> if no image should be
265      * displayed
266      */

267     public void setImage(Image image) {
268         this.image = image;
269     }
270
271     /**
272      * The style used to create the {@link CLabel} in the default implementation
273      *
274      * @param style
275      * the event triggered the popup of the tooltip
276      */

277     public void setStyle(int style) {
278         this.style = style;
279     }
280
281     /**
282      * The text displayed in the {@link CLabel} in the default implementation
283      *
284      * @param text
285      * the text or <code>null</code> if no text has to be displayed
286      */

287     public void setText(String JavaDoc text) {
288         this.text = text;
289     }
290
291 }
292
Popular Tags