KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.DocumentListener JavaDoc;
22 import javax.swing.event.UndoableEditListener JavaDoc;
23 import javax.swing.text.*;
24
25
26 // Document implementation
27

28 /** Document that delegates all functionality to given document.
29 * Useful if a subclass wants to modify a behaviour of a document.
30 * <p>{@link StyledDocument} methods are just defaulted to the plainest behavior.
31 *
32 * @author Jaroslav Tulach
33 */

34 public class FilterDocument extends Object JavaDoc implements StyledDocument {
35     /** default leaf element */
36     private static Element leaf;
37
38     /** the original document to delegate to */
39     protected Document original;
40
41     /** Create new document instance.
42      * @param original delegated-to document
43       */

44     public FilterDocument(Document original) {
45         this.original = original;
46     }
47
48     /* Length of document.
49     * @return number of characters >= 0
50     */

51     public int getLength() {
52         return original.getLength();
53     }
54
55     /* Add listener for changes in document */
56     public void addDocumentListener(DocumentListener JavaDoc l) {
57         original.addDocumentListener(l);
58     }
59
60     /* Remove listener for chagnes in document */
61     public void removeDocumentListener(DocumentListener JavaDoc l) {
62         original.removeDocumentListener(l);
63     }
64
65     /* Add listener for undoable edit actions over document */
66     public void addUndoableEditListener(UndoableEditListener JavaDoc listener) {
67         original.addUndoableEditListener(listener);
68     }
69
70     /* Remove listener for undoable edit actions */
71     public void removeUndoableEditListener(UndoableEditListener JavaDoc listener) {
72         original.removeUndoableEditListener(listener);
73     }
74
75     /* Gets document property by key */
76     public Object JavaDoc getProperty(Object JavaDoc key) {
77         return original.getProperty(key);
78     }
79
80     /* Puts new property of document */
81     public void putProperty(Object JavaDoc key, Object JavaDoc value) {
82         original.putProperty(key, value);
83     }
84
85     /* Removes portion of a document */
86     public void remove(int offset, int len) throws BadLocationException {
87         original.remove(offset, len);
88     }
89
90     /* Inserts string into document */
91     public void insertString(int offset, String JavaDoc str, AttributeSet a)
92     throws BadLocationException {
93         original.insertString(offset, str, a);
94     }
95
96     /* Get text at given offset in document as string */
97     public String JavaDoc getText(int offset, int len) throws BadLocationException {
98         return original.getText(offset, len);
99     }
100
101     /* Get text at given offset in document as segment */
102     public void getText(int offset, int len, Segment txt)
103     throws BadLocationException {
104         original.getText(offset, len, txt);
105     }
106
107     /* Get the start non-movable position in the document */
108     public Position getStartPosition() {
109         return original.getStartPosition();
110     }
111
112     /* Get end document position */
113     public Position getEndPosition() {
114         return original.getEndPosition();
115     }
116
117     /* Create position in document */
118     public Position createPosition(int offset) throws BadLocationException {
119         return original.createPosition(offset);
120     }
121
122     /* Return array of root elements - usually only one */
123     public Element[] getRootElements() {
124         return original.getRootElements();
125     }
126
127     /* Return default root element */
128     public Element getDefaultRootElement() {
129         return original.getDefaultRootElement();
130     }
131
132     /* Rendering on document as reader. Renderer must not make
133     * any mutations.
134     */

135     public void render(Runnable JavaDoc r) {
136         original.render(r);
137     }
138
139     //
140
// StyledDocument methods
141
//
142

143     /*
144     * Adds a new style into the logical style hierarchy. Style attributes
145     * resolve from bottom up so an attribute specified in a child
146     * will override an attribute specified in the parent.
147     *
148     * @param nm the name of the style (must be unique within the
149     * collection of named styles). The name may be null if the style
150     * is unnamed, but the caller is responsible
151     * for managing the reference returned as an unnamed style can't
152     * be fetched by name. An unnamed style may be useful for things
153     * like character attribute overrides such as found in a style
154     * run.
155     * @param parent the parent style. This may be null if unspecified
156     * attributes need not be resolved in some other style.
157     * @return the style
158     */

159     public Style addStyle(String JavaDoc nm, Style parent) {
160         return null;
161     }
162
163     /*
164     * Removes a named style previously added to the document.
165     *
166     * @param nm the name of the style to remove
167     */

168     public void removeStyle(String JavaDoc nm) {
169     }
170
171     /*
172     * Fetches a named style previously added.
173     *
174     * @param nm the name of the style
175     * @return the style
176     */

177     public Style getStyle(String JavaDoc nm) {
178         return null;
179     }
180
181     /*
182     * Changes the content element attributes used for the given range of
183     * existing content in the document. All of the attributes
184     * defined in the given Attributes argument are applied to the
185     * given range. This method can be used to completely remove
186     * all content level attributes for the given range by
187     * giving an Attributes argument that has no attributes defined
188     * and setting replace to true.
189     *
190     * @param offset the start of the change >= 0
191     * @param length the length of the change >= 0
192     * @param s the non-null attributes to change to. Any attributes
193     * defined will be applied to the text for the given range.
194     * @param replace indicates whether or not the previous
195     * attributes should be cleared before the new attributes
196     * as set. If true, the operation will replace the
197     * previous attributes entirely. If false, the new
198     * attributes will be merged with the previous attributes.
199     */

200     public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
201     }
202
203     /*
204     * Sets paragraph attributes.
205     *
206     * @param offset the start of the change >= 0
207     * @param length the length of the change >= 0
208     * @param s the non-null attributes to change to. Any attributes
209     * defined will be applied to the text for the given range.
210     * @param replace indicates whether or not the previous
211     * attributes should be cleared before the new attributes
212     * are set. If true, the operation will replace the
213     * previous attributes entirely. If false, the new
214     * attributes will be merged with the previous attributes.
215     */

216     public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace) {
217     }
218
219     /*
220     * Sets the logical style to use for the paragraph at the
221     * given position. If attributes aren't explicitly set
222     * for character and paragraph attributes they will resolve
223     * through the logical style assigned to the paragraph, which
224     * in turn may resolve through some hierarchy completely
225     * independent of the element hierarchy in the document.
226     *
227     * @param pos the starting position >= 0
228     * @param s the style to set
229     */

230     public void setLogicalStyle(int pos, Style s) {
231     }
232
233     /*
234     * Gets a logical style for a given position in a paragraph.
235     *
236     * @param p the position >= 0
237     * @return the style
238     */

239     public Style getLogicalStyle(int p) {
240         return null;
241     }
242
243     /*
244     * Gets the element that represents the paragraph that
245     * encloses the given offset within the document.
246     *
247     * @param pos the offset >= 0
248     * @return the element
249     */

250     public Element getParagraphElement(int pos) {
251         return getLeafElement();
252     }
253
254     /*
255     * Gets the element that represents the character that
256     * is at the given offset within the document.
257     *
258     * @param pos the offset >= 0
259     * @return the element
260     */

261     public Element getCharacterElement(int pos) {
262         return getLeafElement();
263     }
264
265     /*
266     * Takes a set of attributes and turn it into a foreground color
267     * specification. This might be used to specify things
268     * like brighter, more hue, etc.
269     *
270     * @param attr the set of attributes
271     * @return the color
272     */

273     public java.awt.Color JavaDoc getForeground(AttributeSet attr) {
274         return java.awt.Color.black;
275     }
276
277     /*
278     * Takes a set of attributes and turn it into a background color
279     * specification. This might be used to specify things
280     * like brighter, more hue, etc.
281     *
282     * @param attr the set of attributes
283     * @return the color
284     */

285     public java.awt.Color JavaDoc getBackground(AttributeSet attr) {
286         return java.awt.Color.white;
287     }
288
289     /*
290     * Takes a set of attributes and turn it into a font
291     * specification. This can be used to turn things like
292     * family, style, size, etc into a font that is available
293     * on the system the document is currently being used on.
294     *
295     * @param attr the set of attributes
296     * @return the font
297     */

298     public java.awt.Font JavaDoc getFont(AttributeSet attr) {
299         return null;
300     }
301
302     /** Lazy initialization of leaf element.
303     */

304     private static Element getLeafElement() {
305         if (leaf != null) {
306             return leaf;
307         }
308
309         AbstractDocument doc = new javax.swing.text.html.HTMLDocument JavaDoc();
310
311         return leaf = doc.new LeafElement(null, null, 0, 0);
312     }
313 }
314
Popular Tags