KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > FilterStyledDocument


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.text;
20
21 import javax.swing.text.*;
22
23
24 // Document implementation
25

26 /** Document that delegates all functionality to a given {@link StyledDocument}.
27 * Useful if a subclass wants to modify the behaviour of a document.
28 * <p>Note that unlike {@link FilterDocument}, no methods are faked here, since a real styled document is available.
29 *
30 * @author Jaroslav Tulach
31 */

32 public class FilterStyledDocument extends FilterDocument {
33     /** Create new document instance.
34      * @param original the delegated-to styled document
35     */

36     public FilterStyledDocument(StyledDocument original) {
37         super(original);
38     }
39
40     //
41
// StyledDocument methods
42
//
43

44     /*
45     * Adds a new style into the logical style hierarchy. Style attributes
46     * resolve from bottom up so an attribute specified in a child
47     * will override an attribute specified in the parent.
48     *
49     * @param nm the name of the style (must be unique within the
50     * collection of named styles). The name may be null if the style
51     * is unnamed, but the caller is responsible
52     * for managing the reference returned as an unnamed style can't
53     * be fetched by name. An unnamed style may be useful for things
54     * like character attribute overrides such as found in a style
55     * run.
56     * @param parent the parent style. This may be null if unspecified
57     * attributes need not be resolved in some other style.
58     * @return the style
59     */

60     public Style addStyle(String JavaDoc nm, Style parent) {
61         return ((StyledDocument) original).addStyle(nm, parent);
62     }
63
64     /*
65     * Removes a named style previously added to the document.
66     *
67     * @param nm the name of the style to remove
68     */

69     public void removeStyle(String JavaDoc nm) {
70         ((StyledDocument) original).removeStyle(nm);
71     }
72
73     /*
74     * Fetches a named style previously added.
75     *
76     * @param nm the name of the style
77     * @return the style
78     */

79     public Style getStyle(String JavaDoc nm) {
80         return ((StyledDocument) original).getStyle(nm);
81     }
82
83     /*
84     * Changes the content element attributes used for the given range of
85     * existing content in the document. All of the attributes
86     * defined in the given Attributes argument are applied to the
87     * given range. This method can be used to completely remove
88     * all content level attributes for the given range by
89     * giving an Attributes argument that has no attributes defined
90     * and setting replace to true.
91     *
92     * @param offset the start of the change >= 0
93     * @param length the length of the change >= 0
94     * @param s the non-null attributes to change to. Any attributes
95     * defined will be applied to the text for the given range.
96     * @param replace indicates whether or not the previous
97     * attributes should be cleared before the new attributes
98     * as set. If true, the operation will replace the
99     * previous attributes entirely. If false, the new
100     * attributes will be merged with the previous attributes.
101     */

102     public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
103         ((StyledDocument) original).setCharacterAttributes(offset, length, s, replace);
104     }
105
106     /*
107     * Sets paragraph attributes.
108     *
109     * @param offset the start of the change >= 0
110     * @param length the length of the change >= 0
111     * @param s the non-null attributes to change to. Any attributes
112     * defined will be applied to the text for the given range.
113     * @param replace indicates whether or not the previous
114     * attributes should be cleared before the new attributes
115     * are set. If true, the operation will replace the
116     * previous attributes entirely. If false, the new
117     * attributes will be merged with the previous attributes.
118     */

119     public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace) {
120         ((StyledDocument) original).setParagraphAttributes(offset, length, s, replace);
121     }
122
123     /*
124     * Sets the logical style to use for the paragraph at the
125     * given position. If attributes aren't explicitly set
126     * for character and paragraph attributes they will resolve
127     * through the logical style assigned to the paragraph, which
128     * in turn may resolve through some hierarchy completely
129     * independent of the element hierarchy in the document.
130     *
131     * @param pos the starting position >= 0
132     * @param s the style to set
133     */

134     public void setLogicalStyle(int pos, Style s) {
135         ((StyledDocument) original).setLogicalStyle(pos, s);
136     }
137
138     /*
139     * Gets a logical style for a given position in a paragraph.
140     *
141     * @param p the position >= 0
142     * @return the style
143     */

144     public Style getLogicalStyle(int p) {
145         return ((StyledDocument) original).getLogicalStyle(p);
146     }
147
148     /*
149     * Gets the element that represents the paragraph that
150     * encloses the given offset within the document.
151     *
152     * @param pos the offset >= 0
153     * @return the element
154     */

155     public Element getParagraphElement(int pos) {
156         return ((StyledDocument) original).getParagraphElement(pos);
157     }
158
159     /*
160     * Gets the element that represents the character that
161     * is at the given offset within the document.
162     *
163     * @param pos the offset >= 0
164     * @return the element
165     */

166     public Element getCharacterElement(int pos) {
167         return ((StyledDocument) original).getCharacterElement(pos);
168     }
169
170     /*
171     * Takes a set of attributes and turn it into a foreground color
172     * specification. This might be used to specify things
173     * like brighter, more hue, etc.
174     *
175     * @param attr the set of attributes
176     * @return the color
177     */

178     public java.awt.Color JavaDoc getForeground(AttributeSet attr) {
179         return ((StyledDocument) original).getForeground(attr);
180     }
181
182     /*
183     * Takes a set of attributes and turn it into a background color
184     * specification. This might be used to specify things
185     * like brighter, more hue, etc.
186     *
187     * @param attr the set of attributes
188     * @return the color
189     */

190     public java.awt.Color JavaDoc getBackground(AttributeSet attr) {
191         return ((StyledDocument) original).getBackground(attr);
192     }
193
194     /*
195     * Takes a set of attributes and turn it into a font
196     * specification. This can be used to turn things like
197     * family, style, size, etc into a font that is available
198     * on the system the document is currently being used on.
199     *
200     * @param attr the set of attributes
201     * @return the font
202     */

203     public java.awt.Font JavaDoc getFont(AttributeSet attr) {
204         return ((StyledDocument) original).getFont(attr);
205     }
206 }
207
Popular Tags