comparison dwtx/jface/text/TextAttribute.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.TextAttribute;
14
15 import dwt.dwthelper.utils;
16
17
18 import dwt.DWT;
19 import dwt.graphics.Color;
20 import dwt.graphics.Font;
21
22
23 /**
24 * Description of textual attributes such as color and style. Text attributes
25 * are considered value objects.
26 * <p>
27 * Clients usually instantiate object of the class.</p>
28 */
29 public class TextAttribute {
30
31 /**
32 * Text attribute for strikethrough style.
33 * (value <code>1 << 29</code>).
34 * @since 3.1
35 */
36 public static final int STRIKETHROUGH= 1 << 29;
37
38 /**
39 * Text attribute for underline style.
40 * (value <code>1 << 30</code>)
41 * @since 3.1
42 */
43 public static final int UNDERLINE= 1 << 30;
44
45
46 /** Foreground color */
47 private Color foreground;
48
49 /** Background color */
50 private Color background;
51
52 /** The text style */
53 private int style;
54
55 /**
56 * The text font.
57 * @since 3.3
58 */
59 private Font font;
60
61 /**
62 * Cached hash code.
63 * @since 3.3
64 */
65 private int fHashCode;
66
67 /**
68 * Creates a text attribute with the given colors and style.
69 *
70 * @param foreground the foreground color, <code>null</code> if none
71 * @param background the background color, <code>null</code> if none
72 * @param style the style
73 */
74 public TextAttribute(Color foreground, Color background, int style) {
75 this.foreground= foreground;
76 this.background= background;
77 this.style= style;
78 }
79
80 /**
81 * Creates a text attribute with the given colors and style.
82 *
83 * @param foreground the foreground color, <code>null</code> if none
84 * @param background the background color, <code>null</code> if none
85 * @param style the style
86 * @param font the font, <code>null</code> if none
87 * @since 3.3
88 */
89 public TextAttribute(Color foreground, Color background, int style, Font font) {
90 this.foreground= foreground;
91 this.background= background;
92 this.style= style;
93 this.font= font;
94 }
95
96 /**
97 * Creates a text attribute for the given foreground color, no background color and
98 * with the DWT normal style.
99 *
100 * @param foreground the foreground color, <code>null</code> if none
101 */
102 public TextAttribute(Color foreground) {
103 this(foreground, null, DWT.NORMAL);
104 }
105
106 /*
107 * @see Object#equals(Object)
108 */
109 public bool equals(Object object) {
110
111 if (object is this)
112 return true;
113
114 if (!(object instanceof TextAttribute))
115 return false;
116 TextAttribute a= (TextAttribute)object;
117
118 return (a.style is style && equals(a.foreground, foreground) && equals(a.background, background) && equals(a.font, font));
119 }
120
121 /**
122 * Returns whether the two given objects are equal.
123 *
124 * @param o1 the first object, can be <code>null</code>
125 * @param o2 the second object, can be <code>null</code>
126 * @return <code>true</code> if the given objects are equals
127 * @since 2.0
128 */
129 private bool equals(Object o1, Object o2) {
130 if (o1 !is null)
131 return o1.equals(o2);
132 return (o2 is null);
133 }
134
135 /*
136 * @see Object#hashCode()
137 */
138 public int hashCode() {
139 if (fHashCode is 0) {
140 int multiplier= 37; // some prime
141 fHashCode= 13; // some random value
142 fHashCode= multiplier * fHashCode + (font is null ? 0 : font.hashCode());
143 fHashCode= multiplier * fHashCode + (background is null ? 0 : background.hashCode());
144 fHashCode= multiplier * fHashCode + (foreground is null ? 0 : foreground.hashCode());
145 fHashCode= multiplier * fHashCode + style;
146 }
147 return fHashCode;
148 }
149
150 /**
151 * Returns the attribute's foreground color.
152 *
153 * @return the attribute's foreground color or <code>null</code> if not set
154 */
155 public Color getForeground() {
156 return foreground;
157 }
158
159 /**
160 * Returns the attribute's background color.
161 *
162 * @return the attribute's background color or <code>null</code> if not set
163 */
164 public Color getBackground() {
165 return background;
166 }
167
168 /**
169 * Returns the attribute's style.
170 *
171 * @return the attribute's style
172 */
173 public int getStyle() {
174 return style;
175 }
176
177 /**
178 * Returns the attribute's font.
179 *
180 * @return the attribute's font or <code>null</code> if not set
181 * @since 3.3
182 */
183 public Font getFont() {
184 return font;
185 }
186 }