KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > TextAttribute


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.jface.text;
12
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.Font;
17
18
19 /**
20  * Description of textual attributes such as color and style. Text attributes
21  * are considered value objects.
22  * <p>
23  * Clients usually instantiate object of the class.</p>
24  */

25 public class TextAttribute {
26
27     /**
28      * Text attribute for strikethrough style.
29      * (value <code>1 << 29</code>).
30      * @since 3.1
31      */

32     public static final int STRIKETHROUGH= 1 << 29;
33
34     /**
35      * Text attribute for underline style.
36      * (value <code>1 << 30</code>)
37      * @since 3.1
38      */

39     public static final int UNDERLINE= 1 << 30;
40
41
42     /** Foreground color */
43     private Color foreground;
44
45     /** Background color */
46     private Color background;
47
48     /** The text style */
49     private int style;
50     
51     /**
52      * The text font.
53      * @since 3.3
54      */

55     private Font font;
56
57     /**
58      * Cached hash code.
59      * @since 3.3
60      */

61     private int fHashCode;
62
63     /**
64      * Creates a text attribute with the given colors and style.
65      *
66      * @param foreground the foreground color, <code>null</code> if none
67      * @param background the background color, <code>null</code> if none
68      * @param style the style
69      */

70     public TextAttribute(Color foreground, Color background, int style) {
71         this.foreground= foreground;
72         this.background= background;
73         this.style= style;
74     }
75     
76     /**
77      * Creates a text attribute with the given colors and style.
78      *
79      * @param foreground the foreground color, <code>null</code> if none
80      * @param background the background color, <code>null</code> if none
81      * @param style the style
82      * @param font the font, <code>null</code> if none
83      * @since 3.3
84      */

85     public TextAttribute(Color foreground, Color background, int style, Font font) {
86         this.foreground= foreground;
87         this.background= background;
88         this.style= style;
89         this.font= font;
90     }
91
92     /**
93      * Creates a text attribute for the given foreground color, no background color and
94      * with the SWT normal style.
95      *
96      * @param foreground the foreground color, <code>null</code> if none
97      */

98     public TextAttribute(Color foreground) {
99         this(foreground, null, SWT.NORMAL);
100     }
101
102     /*
103      * @see Object#equals(Object)
104      */

105     public boolean equals(Object JavaDoc object) {
106
107         if (object == this)
108             return true;
109
110         if (!(object instanceof TextAttribute))
111             return false;
112         TextAttribute a= (TextAttribute)object;
113         
114         return (a.style == style && equals(a.foreground, foreground) && equals(a.background, background) && equals(a.font, font));
115     }
116
117     /**
118      * Returns whether the two given objects are equal.
119      *
120      * @param o1 the first object, can be <code>null</code>
121      * @param o2 the second object, can be <code>null</code>
122      * @return <code>true</code> if the given objects are equals
123      * @since 2.0
124      */

125     private boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
126         if (o1 != null)
127             return o1.equals(o2);
128         return (o2 == null);
129     }
130
131     /*
132      * @see Object#hashCode()
133      */

134      public int hashCode() {
135          if (fHashCode == 0) {
136              int multiplier= 37; // some prime
137
fHashCode= 13; // some random value
138
fHashCode= multiplier * fHashCode + (font == null ? 0 : font.hashCode());
139              fHashCode= multiplier * fHashCode + (background == null ? 0 : background.hashCode());
140              fHashCode= multiplier * fHashCode + (foreground == null ? 0 : foreground.hashCode());
141              fHashCode= multiplier * fHashCode + style;
142          }
143         return fHashCode;
144      }
145
146     /**
147      * Returns the attribute's foreground color.
148      *
149      * @return the attribute's foreground color or <code>null</code> if not set
150      */

151     public Color getForeground() {
152         return foreground;
153     }
154
155     /**
156      * Returns the attribute's background color.
157      *
158      * @return the attribute's background color or <code>null</code> if not set
159      */

160     public Color getBackground() {
161         return background;
162     }
163
164     /**
165      * Returns the attribute's style.
166      *
167      * @return the attribute's style
168      */

169     public int getStyle() {
170         return style;
171     }
172     
173     /**
174      * Returns the attribute's font.
175      *
176      * @return the attribute's font or <code>null</code> if not set
177      * @since 3.3
178      */

179     public Font getFont() {
180         return font;
181     }
182 }
183
Popular Tags