1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
20 * <code>StyleRange</code> defines a set of styles for a specified
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.
29 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
31 public class StyleRange extends TextStyle implements Cloneable {
34 * the start offset of the range, zero-based from the document start
39 * the length of the range
44 * the font style of the range. It may be a combination of
45 * SWT.NORMAL, SWT.ITALIC or SWT.BOLD
47 * Note: the font style is not used if the <code>font</code> attribute
50 public int fontStyle = SWT.NORMAL;
53 * Create a new style range with no styles
61 * Create a new style range from an existing text style.
63 * @param style the text style to copy
67 public StyleRange(TextStyle style) {
72 * Create a new style range.
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
79 public StyleRange(int start, int length, Color foreground, Color background) {
80 super(null, foreground, background);
86 * Create a new style range.
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
94 public StyleRange(int start, int length, Color foreground, Color background, int fontStyle) {
95 this(start, length, foreground, background);
96 this.fontStyle = fontStyle;
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.
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
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);
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
127 * @return the receiver's hash
129 * @see #equals(Object)
132 public int hashCode() {
133 return super.hashCode() ^ fontStyle;
135 boolean isVariableHeight() {
136 return font != null || (metrics != null && (metrics.ascent != 0 || metrics.descent != 0)) || rise != 0;
139 * Returns whether or not the receiver is unstyled (i.e., does not have any
140 * style attributes specified).
142 * @return true if the receiver is unstyled, false otherwise.
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;
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.
162 * @param style the object to compare with this object
163 * @return true if the objects are similar, false otherwise
165 public boolean similarTo(StyleRange style) {
166 if (!super.equals(style)) return false;
167 if (fontStyle != style.fontStyle) return false;
172 * Returns a new StyleRange with the same values as this StyleRange.
174 * @return a shallow copy of this StyleRange
177 public Object clone() {
179 return super.clone();
180 } catch (CloneNotSupportedException e) {
186 * Returns a string containing a concise, human-readable
187 * description of the receiver.
189 * @return a string representation of the StyleRange
192 public String toString() {
193 StringBuilder buffer = new StringBuilder();
194 buffer.append("StyleRange {");
195 buffer.append(start);
197 buffer.append(length);
198 buffer.append(", fontStyle=");
201 buffer.append("bold");
204 buffer.append("italic");
206 case SWT.BOLD | SWT.ITALIC:
207 buffer.append("bold-italic");
210 buffer.append("normal");
212 String str = super.toString();
213 int index = str.indexOf('{');
214 str = str.substring(index + 1);
215 if (str.length() > 1) buffer.append(", ");
217 return buffer.toString();