KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > TextStyle


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.swt.graphics;
12
13 import org.eclipse.swt.*;
14
15 /**
16  * <code>TextStyle</code> defines a set of styles that can be applied
17  * to a range of text.
18  * <p>
19  * The hashCode() method in this class uses the values of the public
20  * fields to compute the hash value. When storing instances of the
21  * class in hashed collections, do not modify these fields after the
22  * object has been inserted.
23  * </p>
24  * <p>
25  * Application code does <em>not</em> need to explicitly release the
26  * resources managed by each instance when those instances are no longer
27  * required, and thus no <code>dispose()</code> method is provided.
28  * </p>
29  *
30  * @see TextLayout
31  * @see Font
32  * @see Color
33  *
34  * @since 3.0
35  */

36 public class TextStyle {
37
38     /**
39      * the font of the style
40      */

41     public Font font;
42
43     /**
44      * the foreground of the style
45      */

46     public Color foreground;
47
48     /**
49      * the background of the style
50      */

51     public Color background;
52
53     /**
54      * the underline flag of the style
55      *
56      * @since 3.1
57      */

58     public boolean underline;
59     
60     /**
61      * the strikeout flag of the style
62      *
63      * @since 3.1
64      */

65     public boolean strikeout;
66     
67     /**
68      * the GlyphMetrics of the style
69      *
70      * @since 3.2
71      */

72     public GlyphMetrics metrics;
73     
74     /**
75      * the baseline rise of the style.
76      *
77      * @since 3.2
78      */

79     public int rise;
80     
81 /**
82  * Create a new text style with the specified font, foreground
83  * and background.
84  *
85  * @param font the font of the style, <code>null</code> if none
86  * @param foreground the foreground color of the style, <code>null</code> if none
87  * @param background the background color of the style, <code>null</code> if none
88  */

89 public TextStyle (Font font, Color foreground, Color background) {
90     if (font != null && font.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
91     if (foreground != null && foreground.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
92     if (background != null && background.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
93     this.font = font;
94     this.foreground = foreground;
95     this.background = background;
96 }
97
98 /**
99  * Compares the argument to the receiver, and returns true
100  * if they represent the <em>same</em> object using a class
101  * specific comparison.
102  *
103  * @param object the object to compare with this object
104  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
105  *
106  * @see #hashCode()
107  */

108 public boolean equals(Object JavaDoc object) {
109     if (object == this) return true;
110     if (object == null) return false;
111     if (!(object instanceof TextStyle)) return false;
112     TextStyle style = (TextStyle)object;
113     if (foreground != null) {
114         if (!foreground.equals(style.foreground)) return false;
115     } else if (style.foreground != null) return false;
116     if (background != null) {
117         if (!background.equals(style.background)) return false;
118     } else if (style.background != null) return false;
119     if (font != null) {
120         if (!font.equals(style.font)) return false;
121     } else if (style.font != null) return false;
122     if (metrics != null || style.metrics != null) return false;
123     if (underline != style.underline) return false;
124     if (strikeout != style.strikeout) return false;
125     if (rise != style.rise) return false;
126     return true;
127 }
128
129 /**
130  * Returns an integer hash code for the receiver. Any two
131  * objects that return <code>true</code> when passed to
132  * <code>equals</code> must return the same value for this
133  * method.
134  *
135  * @return the receiver's hash
136  *
137  * @see #equals(Object)
138  */

139 public int hashCode() {
140     int hash = 0;
141     if (foreground != null) hash ^= foreground.hashCode();
142     if (background != null) hash ^= background.hashCode();
143     if (font != null) hash ^= font.hashCode();
144     if (metrics != null) hash ^= metrics.hashCode();
145     if (underline) hash ^= hash;
146     if (strikeout) hash ^= hash;
147     hash ^= rise;
148     return hash;
149 }
150
151 /**
152  * Returns a string containing a concise, human-readable
153  * description of the receiver.
154  *
155  * @return a string representation of the <code>TextStyle</code>
156  */

157 public String JavaDoc toString () {
158     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("TextStyle {");
159     int startLength = buffer.length();
160     if (font != null) {
161         if (buffer.length() > startLength) buffer.append(", ");
162         buffer.append("font=");
163         buffer.append(font);
164     }
165     if (foreground != null) {
166         if (buffer.length() > startLength) buffer.append(", ");
167         buffer.append("foreground=");
168         buffer.append(foreground);
169     }
170     if (background != null) {
171         if (buffer.length() > startLength) buffer.append(", ");
172         buffer.append("background=");
173         buffer.append(background);
174     }
175     if (underline) {
176         if (buffer.length() > startLength) buffer.append(", ");
177         buffer.append("underlined");
178     }
179     if (strikeout) {
180         if (buffer.length() > startLength) buffer.append(", ");
181         buffer.append("striked out");
182     }
183     if (rise != 0) {
184         if (buffer.length() > startLength) buffer.append(", ");
185         buffer.append("rise=");
186         buffer.append(rise);
187     }
188     if (metrics != null) {
189         if (buffer.length() > startLength) buffer.append(", ");
190         buffer.append("metrics=");
191         buffer.append(metrics);
192     }
193     buffer.append("}");
194     return buffer.toString();
195 }
196
197 }
198
Popular Tags