1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
15
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18
19 /**
20 * <code>StyleRange</code> defines a set of styles for a specified
21 * range of text.
22 * <p>
23 * The hashCode() method in this class uses the values of the public
24 * fields to compute the hash value. When storing instances of the
25 * class in hashed collections, do not modify these fields after the
26 * object has been inserted.
27 * </p>
28 *
29 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
30 */
31 public class StyleRange extends TextStyle implements Cloneable {
32
33 /**
34 * the start offset of the range, zero-based from the document start
35 */
36 public int start;
37
38 /**
39 * the length of the range
40 */
41 public int length;
42
43 /**
44 * the font style of the range. It may be a combination of
45 * SWT.NORMAL, SWT.ITALIC or SWT.BOLD
46 *
47 * Note: the font style is not used if the <code>font</code> attribute
48 * is set
49 */
50 public int fontStyle = SWT.NORMAL;
51
52 /**
53 * Create a new style range with no styles
54 *
55 * @since 3.2
56 */
StyleRange()57 public StyleRange() {
58 }
59
60 /**
61 * Create a new style range from an existing text style.
62 *
63 * @param style the text style to copy
64 *
65 * @since 3.4
66 */
StyleRange(TextStyle style)67 public StyleRange(TextStyle style) {
68 super(style);
69 }
70
71 /**
72 * Create a new style range.
73 *
74 * @param start start offset of the style
75 * @param length length of the style
76 * @param foreground foreground color of the style, null if none
77 * @param background background color of the style, null if none
78 */
StyleRange(int start, int length, Color foreground, Color background)79 public StyleRange(int start, int length, Color foreground, Color background) {
80 super(null, foreground, background);
81 this.start = start;
82 this.length = length;
83 }
84
85 /**
86 * Create a new style range.
87 *
88 * @param start start offset of the style
89 * @param length length of the style
90 * @param foreground foreground color of the style, null if none
91 * @param background background color of the style, null if none
92 * @param fontStyle font style of the style, may be SWT.NORMAL, SWT.ITALIC or SWT.BOLD
93 */
StyleRange(int start, int length, Color foreground, Color background, int fontStyle)94 public StyleRange(int start, int length, Color foreground, Color background, int fontStyle) {
95 this(start, length, foreground, background);
96 this.fontStyle = fontStyle;
97 }
98
99 /**
100 * Compares the argument to the receiver, and returns true
101 * if they represent the <em>same</em> object using a class
102 * specific comparison.
103 *
104 * @param object the object to compare with this object
105 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
106 *
107 * @see #hashCode()
108 */
109 @Override
equals(Object object)110 public boolean equals(Object object) {
111 if (object == this) return true;
112 if (object instanceof StyleRange) {
113 StyleRange style = (StyleRange)object;
114 if (start != style.start) return false;
115 if (length != style.length) return false;
116 return similarTo(style);
117 }
118 return false;
119 }
120
121 /**
122 * Returns an integer hash code for the receiver. Any two
123 * objects that return <code>true</code> when passed to
124 * <code>equals</code> must return the same value for this
125 * method.
126 *
127 * @return the receiver's hash
128 *
129 * @see #equals(Object)
130 */
131 @Override
hashCode()132 public int hashCode() {
133 return super.hashCode() ^ fontStyle;
134 }
isVariableHeight()135 boolean isVariableHeight() {
136 return font != null || (metrics != null && (metrics.ascent != 0 || metrics.descent != 0)) || rise != 0;
137 }
138 /**
139 * Returns whether or not the receiver is unstyled (i.e., does not have any
140 * style attributes specified).
141 *
142 * @return true if the receiver is unstyled, false otherwise.
143 */
isUnstyled()144 public boolean isUnstyled() {
145 if (font != null) return false;
146 if (rise != 0) return false;
147 if (metrics != null) return false;
148 if (foreground != null) return false;
149 if (background != null) return false;
150 if (fontStyle != SWT.NORMAL) return false;
151 if (underline) return false;
152 if (strikeout) return false;
153 if (borderStyle != SWT.NONE) return false;
154 return true;
155 }
156
157 /**
158 * Compares the specified object to this StyleRange and answer if the two
159 * are similar. The object must be an instance of StyleRange and have the
160 * same field values for except for start and length.
161 *
162 * @param style the object to compare with this object
163 * @return true if the objects are similar, false otherwise
164 */
similarTo(StyleRange style)165 public boolean similarTo(StyleRange style) {
166 if (!super.equals(style)) return false;
167 if (fontStyle != style.fontStyle) return false;
168 return true;
169 }
170
171 /**
172 * Returns a new StyleRange with the same values as this StyleRange.
173 *
174 * @return a shallow copy of this StyleRange
175 */
176 @Override
clone()177 public Object clone() {
178 try {
179 return super.clone();
180 } catch (CloneNotSupportedException e) {
181 return null;
182 }
183 }
184
185 /**
186 * Returns a string containing a concise, human-readable
187 * description of the receiver.
188 *
189 * @return a string representation of the StyleRange
190 */
191 @Override
toString()192 public String toString() {
193 StringBuilder buffer = new StringBuilder();
194 buffer.append("StyleRange {");
195 buffer.append(start);
196 buffer.append(", ");
197 buffer.append(length);
198 buffer.append(", fontStyle=");
199 switch (fontStyle) {
200 case SWT.BOLD:
201 buffer.append("bold");
202 break;
203 case SWT.ITALIC:
204 buffer.append("italic");
205 break;
206 case SWT.BOLD | SWT.ITALIC:
207 buffer.append("bold-italic");
208 break;
209 default:
210 buffer.append("normal");
211 }
212 String str = super.toString();
213 int index = str.indexOf('{');
214 str = str.substring(index + 1);
215 if (str.length() > 1) buffer.append(", ");
216 buffer.append(str);
217 return buffer.toString();
218 }
219 }
220