001: /*******************************************************************************
002: * Copyright (c) 2004, 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: * Tom Schindl <tom.shindl@bestsolution.at> - tooltip support
011: *******************************************************************************/package org.eclipse.jface.viewers;
012:
013: import org.eclipse.swt.graphics.Color;
014: import org.eclipse.swt.graphics.Font;
015: import org.eclipse.swt.graphics.Image;
016: import org.eclipse.swt.graphics.Point;
017:
018: /**
019: * The ViewerLabel is the class that is passed to a viewer to handle updates of
020: * labels. It keeps track of both original and updates text.
021: *
022: * @see IViewerLabelProvider
023: * @since 3.0
024: */
025: public class ViewerLabel {
026:
027: // New values for the receiver. Null if nothing has been set.
028: private String newText = null;
029:
030: private Image newImage = null;
031:
032: private boolean imageUpdated = false;
033:
034: private boolean textUpdated = false;
035:
036: private Color background = null;
037:
038: private Color foreground = null;
039:
040: private Font font = null;
041:
042: // The initial values for the receiver.
043: private String startText;
044:
045: private Image startImage;
046:
047: private boolean hasPendingDecorations;
048:
049: private String tooltipText;
050:
051: private Color tooltipForegroundColor;
052:
053: private Color tooltipBackgroundColor;
054:
055: private Point tooltipShift;
056:
057: /**
058: * Create a new instance of the receiver with the supplied initial text and
059: * image.
060: *
061: * @param initialText
062: * @param initialImage
063: */
064: public ViewerLabel(String initialText, Image initialImage) {
065: startText = initialText;
066: startImage = initialImage;
067: }
068:
069: /**
070: * Get the image for the receiver. If the new image has been set return it,
071: * otherwise return the starting image.
072: *
073: * @return Returns the image.
074: */
075: public final Image getImage() {
076: if (imageUpdated) {
077: return newImage;
078: }
079: return startImage;
080: }
081:
082: /**
083: * Set the image for the receiver.
084: *
085: * @param image
086: * The image to set.
087: */
088: public final void setImage(Image image) {
089: imageUpdated = true;
090: newImage = image;
091: }
092:
093: /**
094: * Get the text for the receiver. If the new text has been set return it,
095: * otherwise return the starting text.
096: *
097: * @return String or <code>null</code> if there was no initial text and
098: * nothing was updated.
099: */
100: public final String getText() {
101: if (textUpdated) {
102: return newText;
103: }
104: return startText;
105: }
106:
107: /**
108: * Set the text for the receiver.
109: *
110: * @param text
111: * String The label to set. This value should not be
112: * <code>null</code>.
113: * @see #hasNewText()
114: */
115: public final void setText(String text) {
116: newText = text;
117: textUpdated = true;
118: }
119:
120: /**
121: * Return whether or not the image has been set.
122: *
123: * @return boolean. <code>true</code> if the image has been set to
124: * something new.
125: *
126: * @since 3.1
127: */
128: public boolean hasNewImage() {
129:
130: // If we started with null any change is an update
131: if (startImage == null) {
132: return newImage != null;
133: }
134:
135: if (imageUpdated) {
136: return !(startImage.equals(newImage));
137: }
138: return false;
139: }
140:
141: /**
142: * Return whether or not the text has been set.
143: *
144: * @return boolean. <code>true</code> if the text has been set to
145: * something new.
146: */
147: public boolean hasNewText() {
148:
149: // If we started with null any change is an update
150: if (startText == null) {
151: return newText != null;
152: }
153:
154: if (textUpdated) {
155: return !(startText.equals(newText));
156: }
157:
158: return false;
159: }
160:
161: /**
162: * Return whether or not the background color has been set.
163: *
164: * @return boolean. <code>true</code> if the value has been set.
165: */
166: public boolean hasNewBackground() {
167: return background != null;
168: }
169:
170: /**
171: * Return whether or not the foreground color has been set.
172: *
173: * @return boolean. <code>true</code> if the value has been set.
174: *
175: * @since 3.1
176: */
177: public boolean hasNewForeground() {
178: return foreground != null;
179: }
180:
181: /**
182: * Return whether or not the font has been set.
183: *
184: * @return boolean. <code>true</code> if the value has been set.
185: *
186: * @since 3.1
187: */
188: public boolean hasNewFont() {
189: return font != null;
190: }
191:
192: /**
193: * Get the background Color.
194: *
195: * @return Color or <code>null</code> if no new value was set.
196: *
197: * @since 3.1
198: */
199: public Color getBackground() {
200: return background;
201: }
202:
203: /**
204: * Set the background Color.
205: *
206: * @param background
207: * Color. This value should not be <code>null</code>.
208: *
209: * @since 3.1
210: */
211: public void setBackground(Color background) {
212: this .background = background;
213: }
214:
215: /**
216: * Get the font.
217: *
218: * @return Font or <code>null</code> if no new value was set.
219: *
220: * @since 3.1
221: */
222: public Font getFont() {
223: return font;
224: }
225:
226: /**
227: * Set the font.
228: *
229: * @param font
230: * Font This value should not be <code>null</code>.
231: *
232: * @since 3.1
233: */
234: public void setFont(Font font) {
235: this .font = font;
236: }
237:
238: /**
239: * Get the foreground Color.
240: *
241: * @return Color or <code>null</code> if no new value was set.
242: *
243: * @since 3.1
244: */
245: public Color getForeground() {
246: return foreground;
247: }
248:
249: /**
250: * Set the foreground Color.
251: *
252: * @param foreground
253: * Color This value should not be <code>null</code>.
254: *
255: * @since 3.1
256: */
257: public void setForeground(Color foreground) {
258: this .foreground = foreground;
259: }
260:
261: /**
262: * Set whether or not there are any decorations pending.
263: *
264: * @param hasPendingDecorations
265: */
266: void setHasPendingDecorations(boolean hasPendingDecorations) {
267: this .hasPendingDecorations = hasPendingDecorations;
268: }
269:
270: /**
271: * @return <code>boolean</code>. <code>true</code> if there are any
272: * decorations pending.
273: */
274: boolean hasPendingDecorations() {
275: return hasPendingDecorations;
276: }
277:
278: /**
279: * Returns the tooltipText.
280: *
281: * @return {@link String} or <code>null</code> if the tool tip text was
282: * never set.
283: *
284: * @since 3.3
285: */
286: public String getTooltipText() {
287: return tooltipText;
288: }
289:
290: /**
291: * Set the tool tip text.
292: *
293: * @param tooltipText
294: * The tooltipText {@link String} to set. This value should not
295: * be <code>null</code>.
296: *
297: * @since 3.3
298: */
299: public void setTooltipText(String tooltipText) {
300: this .tooltipText = tooltipText;
301: }
302:
303: /**
304: * Return whether or not the tool tip text has been set.
305: *
306: * @return <code>boolean</code>. <code>true</code> if the tool tip text
307: * has been set.
308: *
309: * @since 3.3
310: */
311: public boolean hasNewTooltipText() {
312: return this .tooltipText != null;
313: }
314:
315: /**
316: * Return the tool tip background color.
317: *
318: * @return {@link Color} or <code>null</code> if the tool tip background
319: * color has not been set.
320: *
321: * @since 3.3
322: */
323: public Color getTooltipBackgroundColor() {
324: return tooltipBackgroundColor;
325: }
326:
327: /**
328: * Set the background {@link Color} for tool tip.
329: *
330: * @param tooltipBackgroundColor
331: * The {@link Color} to set. This value should not be
332: * <code>null</code>.
333: *
334: * @since 3.3
335: */
336: public void setTooltipBackgroundColor(Color tooltipBackgroundColor) {
337: this .tooltipBackgroundColor = tooltipBackgroundColor;
338: }
339:
340: /**
341: * Return whether or not the tool tip background color has been set.
342: *
343: * @return <code>boolean</code>. <code>true</code> if the tool tip text
344: * has been set.
345: *
346: * @since 3.3
347: */
348: public boolean hasNewTooltipBackgroundColor() {
349: return tooltipBackgroundColor != null;
350: }
351:
352: /**
353: * Return the foreground {@link Color}.
354: *
355: * @return Returns {@link Color} or <code>null</code> if the tool tip
356: * foreground color has not been set.
357: *
358: * @since 3.3
359: */
360: public Color getTooltipForegroundColor() {
361: return tooltipForegroundColor;
362: }
363:
364: /**
365: * Set the foreground {@link Color} for tool tip.
366: *
367: * @param tooltipForegroundColor
368: * The tooltipForegroundColor to set.
369: *
370: * @since 3.3
371: */
372: public void setTooltipForegroundColor(Color tooltipForegroundColor) {
373: this .tooltipForegroundColor = tooltipForegroundColor;
374: }
375:
376: /**
377: *
378: * Return whether or not the tool tip foreground color has been set.
379: *
380: * @return <code>boolean</code>. <code>true</code> if the tool tip foreground
381: * has been set.
382: *
383: * @since 3.3
384: */
385: public boolean hasNewTooltipForegroundColor() {
386: return tooltipForegroundColor != null;
387: }
388:
389: /**
390: * @return Returns the tooltipShift.
391: * @since 3.3
392: */
393: public Point getTooltipShift() {
394: return tooltipShift;
395: }
396:
397: /**
398: * @param tooltipShift
399: * The tooltipShift to set.
400: * @since 3.3
401: */
402: public void setTooltipShift(Point tooltipShift) {
403: this .tooltipShift = tooltipShift;
404: }
405:
406: /**
407: * @return Return whether or not the tool tip shift has been set.
408: * @since 3.3
409: */
410: public boolean hasTooltipShift() {
411: return this.tooltipShift != null;
412: }
413: }
|