KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ViewerLabel


1 /*******************************************************************************
2  * Copyright (c) 2004, 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  * Tom Schindl <tom.shindl@bestsolution.at> - tooltip support
11  *******************************************************************************/

12 package org.eclipse.jface.viewers;
13
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.Font;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Point;
18
19 /**
20  * The ViewerLabel is the class that is passed to a viewer to handle updates of
21  * labels. It keeps track of both original and updates text.
22  *
23  * @see IViewerLabelProvider
24  * @since 3.0
25  */

26 public class ViewerLabel {
27
28     // New values for the receiver. Null if nothing has been set.
29
private String JavaDoc newText = null;
30
31     private Image newImage = null;
32
33     private boolean imageUpdated = false;
34
35     private boolean textUpdated = false;
36
37     private Color background = null;
38
39     private Color foreground = null;
40
41     private Font font = null;
42
43     // The initial values for the receiver.
44
private String JavaDoc startText;
45
46     private Image startImage;
47
48     private boolean hasPendingDecorations;
49
50     private String JavaDoc tooltipText;
51
52     private Color tooltipForegroundColor;
53
54     private Color tooltipBackgroundColor;
55
56     private Point tooltipShift;
57
58     /**
59      * Create a new instance of the receiver with the supplied initial text and
60      * image.
61      *
62      * @param initialText
63      * @param initialImage
64      */

65     public ViewerLabel(String JavaDoc initialText, Image initialImage) {
66         startText = initialText;
67         startImage = initialImage;
68     }
69
70     /**
71      * Get the image for the receiver. If the new image has been set return it,
72      * otherwise return the starting image.
73      *
74      * @return Returns the image.
75      */

76     public final Image getImage() {
77         if (imageUpdated) {
78             return newImage;
79         }
80         return startImage;
81     }
82
83     /**
84      * Set the image for the receiver.
85      *
86      * @param image
87      * The image to set.
88      */

89     public final void setImage(Image image) {
90         imageUpdated = true;
91         newImage = image;
92     }
93
94     /**
95      * Get the text for the receiver. If the new text has been set return it,
96      * otherwise return the starting text.
97      *
98      * @return String or <code>null</code> if there was no initial text and
99      * nothing was updated.
100      */

101     public final String JavaDoc getText() {
102         if (textUpdated) {
103             return newText;
104         }
105         return startText;
106     }
107
108     /**
109      * Set the text for the receiver.
110      *
111      * @param text
112      * String The label to set. This value should not be
113      * <code>null</code>.
114      * @see #hasNewText()
115      */

116     public final void setText(String JavaDoc text) {
117         newText = text;
118         textUpdated = true;
119     }
120
121     /**
122      * Return whether or not the image has been set.
123      *
124      * @return boolean. <code>true</code> if the image has been set to
125      * something new.
126      *
127      * @since 3.1
128      */

129     public boolean hasNewImage() {
130
131         // If we started with null any change is an update
132
if (startImage == null) {
133             return newImage != null;
134         }
135
136         if (imageUpdated) {
137             return !(startImage.equals(newImage));
138         }
139         return false;
140     }
141
142     /**
143      * Return whether or not the text has been set.
144      *
145      * @return boolean. <code>true</code> if the text has been set to
146      * something new.
147      */

148     public boolean hasNewText() {
149
150         // If we started with null any change is an update
151
if (startText == null) {
152             return newText != null;
153         }
154
155         if (textUpdated) {
156             return !(startText.equals(newText));
157         }
158
159         return false;
160     }
161
162     /**
163      * Return whether or not the background color has been set.
164      *
165      * @return boolean. <code>true</code> if the value has been set.
166      */

167     public boolean hasNewBackground() {
168         return background != null;
169     }
170
171     /**
172      * Return whether or not the foreground color has been set.
173      *
174      * @return boolean. <code>true</code> if the value has been set.
175      *
176      * @since 3.1
177      */

178     public boolean hasNewForeground() {
179         return foreground != null;
180     }
181
182     /**
183      * Return whether or not the font has been set.
184      *
185      * @return boolean. <code>true</code> if the value has been set.
186      *
187      * @since 3.1
188      */

189     public boolean hasNewFont() {
190         return font != null;
191     }
192
193     /**
194      * Get the background Color.
195      *
196      * @return Color or <code>null</code> if no new value was set.
197      *
198      * @since 3.1
199      */

200     public Color getBackground() {
201         return background;
202     }
203
204     /**
205      * Set the background Color.
206      *
207      * @param background
208      * Color. This value should not be <code>null</code>.
209      *
210      * @since 3.1
211      */

212     public void setBackground(Color background) {
213         this.background = background;
214     }
215
216     /**
217      * Get the font.
218      *
219      * @return Font or <code>null</code> if no new value was set.
220      *
221      * @since 3.1
222      */

223     public Font getFont() {
224         return font;
225     }
226
227     /**
228      * Set the font.
229      *
230      * @param font
231      * Font This value should not be <code>null</code>.
232      *
233      * @since 3.1
234      */

235     public void setFont(Font font) {
236         this.font = font;
237     }
238
239     /**
240      * Get the foreground Color.
241      *
242      * @return Color or <code>null</code> if no new value was set.
243      *
244      * @since 3.1
245      */

246     public Color getForeground() {
247         return foreground;
248     }
249
250     /**
251      * Set the foreground Color.
252      *
253      * @param foreground
254      * Color This value should not be <code>null</code>.
255      *
256      * @since 3.1
257      */

258     public void setForeground(Color foreground) {
259         this.foreground = foreground;
260     }
261
262     /**
263      * Set whether or not there are any decorations pending.
264      *
265      * @param hasPendingDecorations
266      */

267     void setHasPendingDecorations(boolean hasPendingDecorations) {
268         this.hasPendingDecorations = hasPendingDecorations;
269     }
270
271     /**
272      * @return <code>boolean</code>. <code>true</code> if there are any
273      * decorations pending.
274      */

275     boolean hasPendingDecorations() {
276         return hasPendingDecorations;
277     }
278
279     /**
280      * Returns the tooltipText.
281      *
282      * @return {@link String} or <code>null</code> if the tool tip text was
283      * never set.
284      *
285      * @since 3.3
286      */

287     public String JavaDoc getTooltipText() {
288         return tooltipText;
289     }
290
291     /**
292      * Set the tool tip text.
293      *
294      * @param tooltipText
295      * The tooltipText {@link String} to set. This value should not
296      * be <code>null</code>.
297      *
298      * @since 3.3
299      */

300     public void setTooltipText(String JavaDoc tooltipText) {
301         this.tooltipText = tooltipText;
302     }
303
304     /**
305      * Return whether or not the tool tip text has been set.
306      *
307      * @return <code>boolean</code>. <code>true</code> if the tool tip text
308      * has been set.
309      *
310      * @since 3.3
311      */

312     public boolean hasNewTooltipText() {
313         return this.tooltipText != null;
314     }
315
316     /**
317      * Return the tool tip background color.
318      *
319      * @return {@link Color} or <code>null</code> if the tool tip background
320      * color has not been set.
321      *
322      * @since 3.3
323      */

324     public Color getTooltipBackgroundColor() {
325         return tooltipBackgroundColor;
326     }
327
328     /**
329      * Set the background {@link Color} for tool tip.
330      *
331      * @param tooltipBackgroundColor
332      * The {@link Color} to set. This value should not be
333      * <code>null</code>.
334      *
335      * @since 3.3
336      */

337     public void setTooltipBackgroundColor(Color tooltipBackgroundColor) {
338         this.tooltipBackgroundColor = tooltipBackgroundColor;
339     }
340
341     /**
342      * Return whether or not the tool tip background color has been set.
343      *
344      * @return <code>boolean</code>. <code>true</code> if the tool tip text
345      * has been set.
346      *
347      * @since 3.3
348      */

349     public boolean hasNewTooltipBackgroundColor() {
350         return tooltipBackgroundColor != null;
351     }
352
353     /**
354      * Return the foreground {@link Color}.
355      *
356      * @return Returns {@link Color} or <code>null</code> if the tool tip
357      * foreground color has not been set.
358      *
359      * @since 3.3
360      */

361     public Color getTooltipForegroundColor() {
362         return tooltipForegroundColor;
363     }
364
365     /**
366      * Set the foreground {@link Color} for tool tip.
367      *
368      * @param tooltipForegroundColor
369      * The tooltipForegroundColor to set.
370      *
371      * @since 3.3
372      */

373     public void setTooltipForegroundColor(Color tooltipForegroundColor) {
374         this.tooltipForegroundColor = tooltipForegroundColor;
375     }
376
377     /**
378      *
379      * Return whether or not the tool tip foreground color has been set.
380      *
381      * @return <code>boolean</code>. <code>true</code> if the tool tip foreground
382      * has been set.
383      *
384      * @since 3.3
385      */

386     public boolean hasNewTooltipForegroundColor() {
387         return tooltipForegroundColor != null;
388     }
389
390     /**
391      * @return Returns the tooltipShift.
392      * @since 3.3
393      */

394     public Point getTooltipShift() {
395         return tooltipShift;
396     }
397
398     /**
399      * @param tooltipShift
400      * The tooltipShift to set.
401      * @since 3.3
402      */

403     public void setTooltipShift(Point tooltipShift) {
404         this.tooltipShift = tooltipShift;
405     }
406
407     /**
408      * @return Return whether or not the tool tip shift has been set.
409      * @since 3.3
410      */

411     public boolean hasTooltipShift() {
412         return this.tooltipShift != null;
413     }
414 }
415
Popular Tags