001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.window;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.custom.CLabel;
014: import org.eclipse.swt.graphics.Color;
015: import org.eclipse.swt.graphics.Font;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Event;
021:
022: /**
023: * Default implementation of ToolTip that provides an iconofied label with font
024: * and color controls by subclass.
025: *
026: * @since 3.3
027: */
028: public class DefaultToolTip extends ToolTip {
029: private String text;
030:
031: private Color backgroundColor;
032:
033: private Font font;
034:
035: private Image backgroundImage;
036:
037: private Color foregroundColor;
038:
039: private Image image;
040:
041: private int style = SWT.SHADOW_NONE;
042:
043: /**
044: * Create new instance which add TooltipSupport to the widget
045: *
046: * @param control the control on whose action the tooltip is shown
047: */
048: public DefaultToolTip(Control control) {
049: super (control);
050: }
051:
052: /**
053: * Create new instance which add TooltipSupport to the widget
054: *
055: * @param control the control to which the tooltip is bound
056: * @param style style passed to control tooltip behaviour
057: * @param manualActivation <code>true</code> if the activation is done manually using
058: * {@link #show(Point)}
059: * @see #RECREATE
060: * @see #NO_RECREATE
061: */
062: public DefaultToolTip(Control control, int style,
063: boolean manualActivation) {
064: super (control, style, manualActivation);
065: }
066:
067: /**
068: * Creates the content are of the the tooltip. By default this creates a
069: * CLabel to display text. To customize the text Subclasses may override the
070: * following methods
071: * <ul>
072: * <li>{@link #getStyle(Event)}</li>
073: * <li>{@link #getBackgroundColor(Event)}</li>
074: * <li>{@link #getForegroundColor(Event)}</li>
075: * <li>{@link #getFont(Event)}</li>
076: * <li>{@link #getImage(Event)}</li>
077: * <li>{@link #getText(Event)}</li>
078: * <li>{@link #getBackgroundImage(Event)}</li>
079: * </ul>
080: *
081: * @param event
082: * the event that triggered the activation of the tooltip
083: * @param parent
084: * the parent of the content area
085: * @return the content area created
086: */
087: protected Composite createToolTipContentArea(Event event,
088: Composite parent) {
089: Image image = getImage(event);
090: Image bgImage = getBackgroundImage(event);
091: String text = getText(event);
092: Color fgColor = getForegroundColor(event);
093: Color bgColor = getBackgroundColor(event);
094: Font font = getFont(event);
095:
096: CLabel label = new CLabel(parent, getStyle(event));
097: if (text != null) {
098: label.setText(text);
099: }
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)
160: : foregroundColor;
161: }
162:
163: /**
164: * The background {@link Color} used by {@link CLabel} in the default
165: * implementation
166: *
167: * @param event
168: * the event triggered the popup of the tooltip
169: * @return the {@link Color} or <code>null</code> if default background
170: * color should be used
171: */
172: protected Color getBackgroundColor(Event event) {
173: return (backgroundColor == null) ? event.widget.getDisplay()
174: .getSystemColor(SWT.COLOR_INFO_BACKGROUND)
175: : backgroundColor;
176: }
177:
178: /**
179: * The background {@link Image} used by {@link CLabel} in the default
180: * implementation
181: *
182: * @param event
183: * the event triggered the popup of the tooltip
184: * @return the {@link Image} or <code>null</code> if no image should be
185: * displayed in the background
186: */
187: protected Image getBackgroundImage(Event event) {
188: return backgroundImage;
189: }
190:
191: /**
192: * The {@link Font} used by {@link CLabel} in the default implementation
193: *
194: * @param event
195: * the event triggered the popup of the tooltip
196: * @return the {@link Font} or <code>null</code> if the default font
197: * should be used
198: */
199: protected Font getFont(Event event) {
200: return font;
201: }
202:
203: /**
204: * The text displayed in the {@link CLabel} in the default implementation
205: *
206: * @param event
207: * the event triggered the popup of the tooltip
208: * @return the text or <code>null</code> if no text has to be displayed
209: */
210: protected String getText(Event event) {
211: return text;
212: }
213:
214: /**
215: * The background {@link Image} used by {@link CLabel} in the default
216: * implementation
217: *
218: * @param backgroundColor
219: * the {@link Color} or <code>null</code> if default background
220: * color ({@link SWT#COLOR_INFO_BACKGROUND}) should be used
221: */
222: public void setBackgroundColor(Color backgroundColor) {
223: this .backgroundColor = backgroundColor;
224: }
225:
226: /**
227: * The background {@link Image} used by {@link CLabel} in the default
228: * implementation
229: *
230: * @param backgroundImage
231: * the {@link Image} or <code>null</code> if no image should be
232: * displayed in the background
233: */
234: public void setBackgroundImage(Image backgroundImage) {
235: this .backgroundImage = backgroundImage;
236: }
237:
238: /**
239: * The {@link Font} used by {@link CLabel} in the default implementation
240: *
241: * @param font
242: * the {@link Font} or <code>null</code> if the default font
243: * should be used
244: */
245: public void setFont(Font font) {
246: this .font = font;
247: }
248:
249: /**
250: * The foreground {@link Color} used by {@link CLabel} in the default
251: * implementation
252: *
253: * @param foregroundColor
254: * the {@link Color} or <code>null</code> if default foreground
255: * color should be used
256: */
257: public void setForegroundColor(Color foregroundColor) {
258: this .foregroundColor = foregroundColor;
259: }
260:
261: /**
262: * The {@link Image} displayed in the {@link CLabel} in the default
263: * implementation implementation
264: *
265: * @param image
266: * the {@link Image} or <code>null</code> if no image should be
267: * displayed
268: */
269: public void setImage(Image image) {
270: this .image = image;
271: }
272:
273: /**
274: * The style used to create the {@link CLabel} in the default implementation
275: *
276: * @param style
277: * the event triggered the popup of the tooltip
278: */
279: public void setStyle(int style) {
280: this .style = style;
281: }
282:
283: /**
284: * The text displayed in the {@link CLabel} in the default implementation
285: *
286: * @param text
287: * the text or <code>null</code> if no text has to be displayed
288: */
289: public void setText(String text) {
290: this.text = text;
291: }
292:
293: }
|