KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > StyleRange


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.swt.custom;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.internal.CloneableCompatibility;
16
17 public class StyleRange extends TextStyle implements CloneableCompatibility {
18     
19     /**
20      * the start offset of the range, zero-based from the document start
21      */

22     public int start;
23     
24     /**
25      * the length of the range
26      */

27     public int length;
28
29     /**
30      * the font style of the range. It may be a combination of
31      * SWT.NORMAL, SWT.ITALIC or SWT.BOLD
32      *
33      * Note: the font style is not used if the <code>font</code> attribute
34      * is set
35      */

36     public int fontStyle = SWT.NORMAL;
37     
38 /**
39  * Create a new style range with no styles
40  *
41  * @since 3.2
42  */

43 public StyleRange() {
44     super(null, null, null);
45 }
46
47 /**
48  * Create a new style range.
49  *
50  * @param start start offset of the style
51  * @param length length of the style
52  * @param foreground foreground color of the style, null if none
53  * @param background background color of the style, null if none
54  */

55 public StyleRange(int start, int length, Color foreground, Color background) {
56     super(null, foreground, background);
57     this.start = start;
58     this.length = length;
59 }
60
61 /**
62  * Create a new style range.
63  *
64  * @param start start offset of the style
65  * @param length length of the style
66  * @param foreground foreground color of the style, null if none
67  * @param background background color of the style, null if none
68  * @param fontStyle font style of the style, may be SWT.NORMAL, SWT.ITALIC or SWT.BOLD
69  */

70 public StyleRange(int start, int length, Color foreground, Color background, int fontStyle) {
71     this(start, length, foreground, background);
72     this.fontStyle = fontStyle;
73 }
74
75 /**
76  * Compares the argument to the receiver, and returns true
77  * if they represent the <em>same</em> object using a class
78  * specific comparison.
79  *
80  * @param object the object to compare with this object
81  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
82  *
83  * @see #hashCode()
84  */

85 public boolean equals(Object JavaDoc object) {
86     if (object == this) return true;
87     if (object instanceof StyleRange) {
88         StyleRange style = (StyleRange)object;
89         if (start != style.start) return false;
90         if (length != style.length) return false;
91         return similarTo(style);
92     }
93     return false;
94 }
95
96 /**
97  * Returns an integer hash code for the receiver. Any two
98  * objects that return <code>true</code> when passed to
99  * <code>equals</code> must return the same value for this
100  * method.
101  *
102  * @return the receiver's hash
103  *
104  * @see #equals(Object)
105  */

106 public int hashCode() {
107     return super.hashCode() ^ fontStyle;
108 }
109 boolean isVariableHeight() {
110     return font != null || metrics != null || rise != 0;
111 }
112 /**
113  * Returns whether or not the receiver is unstyled (i.e., does not have any
114  * style attributes specified).
115  *
116  * @return true if the receiver is unstyled, false otherwise.
117  */

118 public boolean isUnstyled() {
119     if (font != null) return false;
120     if (rise != 0) return false;
121     if (metrics != null) return false;
122     if (foreground != null) return false;
123     if (background != null) return false;
124     if (fontStyle != SWT.NORMAL) return false;
125     if (underline) return false;
126     if (strikeout) return false;
127     return true;
128 }
129
130 /**
131  * Compares the specified object to this StyleRange and answer if the two
132  * are similar. The object must be an instance of StyleRange and have the
133  * same field values for except for start and length.
134  *
135  * @param style the object to compare with this object
136  * @return true if the objects are similar, false otherwise
137  */

138 public boolean similarTo(StyleRange style) {
139     if (!super.equals(style)) return false;
140     if (fontStyle != style.fontStyle) return false;
141     return true;
142 }
143
144 /**
145  * Returns a new StyleRange with the same values as this StyleRange.
146  *
147  * @return a shallow copy of this StyleRange
148  */

149 public Object JavaDoc clone() {
150     try {
151         return super.clone();
152     } catch (CloneNotSupportedException JavaDoc e) {
153         return null;
154     }
155 }
156
157 /**
158  * Returns a string containing a concise, human-readable
159  * description of the receiver.
160  *
161  * @return a string representation of the StyleRange
162  */

163 public String JavaDoc toString() {
164     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
165     buffer.append("StyleRange {");
166     buffer.append(start);
167     buffer.append(", ");
168     buffer.append(length);
169     buffer.append(", fontStyle=");
170     switch (fontStyle) {
171         case SWT.BOLD:
172             buffer.append("bold");
173             break;
174         case SWT.ITALIC:
175             buffer.append("italic");
176             break;
177         case SWT.BOLD | SWT.ITALIC:
178             buffer.append("bold-italic");
179             break;
180         default:
181             buffer.append("normal");
182     }
183     String JavaDoc str = super.toString();
184     int index = str.indexOf('{');
185     str = str.substring(index + 1);
186     if (str.length() > 1) buffer.append(", ");
187     buffer.append(str);
188     return buffer.toString();
189 }
190 }
191
Popular Tags