KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > text > StringContent


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Robin Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: StringContent.java,v $
11    Revision 1.1 2004/04/28 11:02:05 bobintetley
12    PlainDocument implementation
13
14
15 */

16
17 package swingwtx.swing.text;
18
19 import swingwtx.swing.undo.*;
20
21 /**
22  * This class is used to represent document content
23  * with a StringBuffer. Hopefully it should be efficient enough.
24  *
25  * @author Robin Rawson-Tetley
26  */

27 public class StringContent implements AbstractDocument.Content {
28     
29     /** The content */
30     private StringBuffer JavaDoc s = null;
31     
32     public StringContent() {
33         s = new StringBuffer JavaDoc();
34     }
35     
36     public StringContent(int initialSize) {
37         s = new StringBuffer JavaDoc(initialSize);
38     }
39
40     /** Creates an anonymous class implementing <code>Position</code> for the
41      * offset given.
42      */

43     public Position createPosition(final int offset) throws BadLocationException {
44         return new Position() {
45             public int getOffset() { return offset; }
46         };
47     }
48
49     /** Returns the content length */
50     public int length() { return s.length(); }
51
52     /**
53      * Inserts a string into the content at the specified position.
54      * @throws BadLocationException if the position is invalid
55      * FIXME: UNDO needs implementing
56      */

57     public UndoableEdit insertString(int where, String JavaDoc str) throws BadLocationException {
58         if (where < 0 || where > s.length())
59             throw new BadLocationException("Out of bounds", where);
60         s.insert(where, str);
61     return null;
62     }
63
64     /**
65      * Removes nitems chars from the content at the specified position.
66      * @throws BadLocationException if the position is invalid
67      * FIXME: UNDO needs implementing
68      */

69     public UndoableEdit remove(int where, int nitems) throws BadLocationException {
70         if (where < 0 || where > s.length() || where + nitems > s.length())
71             throw new BadLocationException("Out of bounds", where);
72         s.replace(where, where + nitems, "");
73     return null;
74     }
75
76     /**
77      * Retrieves a string from the content
78      * @param where The offset
79      * @param len The length of the string
80      * @return The substring specified
81      * @throws BadLocationException if the position is invalid
82      */

83     public String JavaDoc getString(int where, int len) throws BadLocationException {
84         if (where < 0 || where > s.length() || where + len > s.length())
85             throw new BadLocationException("Out of bounds", where);
86         return s.toString().substring(where, where + len);
87     }
88
89     /**
90      * Assigns the specified substring to the passed Segment
91      * @param where The offset
92      * @param len The length
93      * @param txt The segment object to put the substring in
94      */

95     public void getChars(int where, int len, Segment txt) throws BadLocationException {
96         txt.array = getString(where, len).toCharArray();
97     }
98         
99 }
100
Popular Tags