Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 81c4404f35de8122a6594a2c367a94191d7daf99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text;


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;


/**
 * Description of textual attributes such as color and style. Text attributes
 * are considered value objects.
 * <p>
 * Clients usually instantiate object of the class.</p>
 */
public class TextAttribute {

	/**
	 * Text attribute for strikethrough style.
	 * (value <code>1 << 29</code>).
	 * @since 3.1
	 */
	public static final int STRIKETHROUGH= 1 << 29;

	/**
	 * Text attribute for underline style.
	 * (value <code>1 << 30</code>)
	 * @since 3.1
	 */
	public static final int UNDERLINE= 1 << 30;


	/** Foreground color */
	private Color foreground;

	/** Background color */
	private Color background;

	/** The text style */
	private int style;

	/**
	 * The text font.
	 * @since 3.3
	 */
	private Font font;

	/**
	 * Cached hash code.
	 * @since 3.3
	 */
	private int fHashCode;

	/**
	 * Creates a text attribute with the given colors and style.
	 *
	 * @param foreground the foreground color, <code>null</code> if none
	 * @param background the background color, <code>null</code> if none
	 * @param style the style
	 */
	public TextAttribute(Color foreground, Color background, int style) {
		this.foreground= foreground;
		this.background= background;
		this.style= style;
	}

	/**
	 * Creates a text attribute with the given colors and style.
	 *
	 * @param foreground the foreground color, <code>null</code> if none
	 * @param background the background color, <code>null</code> if none
	 * @param style the style
	 * @param font the font, <code>null</code> if none
	 * @since 3.3
	 */
	public TextAttribute(Color foreground, Color background, int style, Font font) {
		this.foreground= foreground;
		this.background= background;
		this.style= style;
		this.font= font;
	}

	/**
	 * Creates a text attribute for the given foreground color, no background color and
	 * with the SWT normal style.
	 *
	 * @param foreground the foreground color, <code>null</code> if none
	 */
	public TextAttribute(Color foreground) {
		this(foreground, null, SWT.NORMAL);
	}

	@Override
	public boolean equals(Object object) {

		if (object == this)
			return true;

		if (!(object instanceof TextAttribute))
			return false;
		TextAttribute a= (TextAttribute)object;

		return (a.style == style && equals(a.foreground, foreground) && equals(a.background, background) && equals(a.font, font));
	}

	/**
	 * Returns whether the two given objects are equal.
	 *
	 * @param o1 the first object, can be <code>null</code>
	 * @param o2 the second object, can be <code>null</code>
	 * @return <code>true</code> if the given objects are equals
	 * @since 2.0
	 */
	private boolean equals(Object o1, Object o2) {
		if (o1 != null)
			return o1.equals(o2);
		return (o2 == null);
	}

	 @Override
	public int hashCode() {
		 if (fHashCode == 0) {
			 int multiplier= 37; // some prime
			 fHashCode= 13; // some random value
			 fHashCode= multiplier * fHashCode + (font == null ? 0 : font.hashCode());
			 fHashCode= multiplier * fHashCode + (background == null ? 0 : background.hashCode());
			 fHashCode= multiplier * fHashCode + (foreground == null ? 0 : foreground.hashCode());
			 fHashCode= multiplier * fHashCode + style;
		 }
	 	return fHashCode;
	 }

	/**
	 * Returns the attribute's foreground color.
	 *
	 * @return the attribute's foreground color or <code>null</code> if not set
	 */
	public Color getForeground() {
		return foreground;
	}

	/**
	 * Returns the attribute's background color.
	 *
	 * @return the attribute's background color or <code>null</code> if not set
	 */
	public Color getBackground() {
		return background;
	}

	/**
	 * Returns the attribute's style.
	 *
	 * @return the attribute's style
	 */
	public int getStyle() {
		return style;
	}

	/**
	 * Returns the attribute's font.
	 *
	 * @return the attribute's font or <code>null</code> if not set
	 * @since 3.3
	 */
	public Font getFont() {
		return font;
	}
}

Back to the top