1 |
/* GapContent.java -- |
/* GapContent.java -- |
2 |
Copyright (C) 2002, 2004 Free Software Foundation, Inc. |
Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
44 |
// lets just use a stringbuffer instead. |
// lets just use a stringbuffer instead. |
45 |
import javax.swing.undo.UndoableEdit; |
import javax.swing.undo.UndoableEdit; |
46 |
|
|
47 |
|
/** |
48 |
|
* This implementation of {@link AbstractDocument.Content} uses a gapped |
49 |
|
* buffer. This takes advantage of the fact that text area content is |
50 |
|
* mostly inserted sequentially. The buffer is a char array that maintains |
51 |
|
* a gap at the current insertion point. If characters a inserted at |
52 |
|
* gap boundaries, the cost is minimal (simple array access). The array only |
53 |
|
* has to be shifted around when the insertion point moves (then the gap also |
54 |
|
* moves and one array copy is necessary) or when the gap is filled up and |
55 |
|
* the buffer has to be enlarged. |
56 |
|
*/ |
57 |
public class GapContent |
public class GapContent |
58 |
implements AbstractDocument.Content, Serializable |
implements AbstractDocument.Content, Serializable |
59 |
{ |
{ |
61 |
|
|
62 |
StringBuffer buf = new StringBuffer(); |
StringBuffer buf = new StringBuffer(); |
63 |
|
|
64 |
|
/** |
65 |
|
* Creates a new GapContent object. |
66 |
|
*/ |
67 |
public GapContent() |
public GapContent() |
68 |
{ |
{ |
69 |
this(10); |
this(10); |
70 |
} |
} |
71 |
|
|
72 |
|
/** |
73 |
|
* Creates a new GapContent object with a specified initial size. |
74 |
|
* |
75 |
|
* @param size the initial size of the buffer |
76 |
|
*/ |
77 |
public GapContent(int size) |
public GapContent(int size) |
78 |
{ |
{ |
79 |
buf.append("\n"); |
buf.append("\n"); |
80 |
} |
} |
81 |
|
|
82 |
|
/** |
83 |
|
* Creates and returns a mark at the specified position. |
84 |
|
* |
85 |
|
* @param offset the position at which to create the mark |
86 |
|
* |
87 |
|
* @return the create Position object for the mark |
88 |
|
* |
89 |
|
* @throws BadLocationException if the offset is not a valid position in |
90 |
|
* the buffer |
91 |
|
*/ |
92 |
public Position createPosition(final int offset) throws BadLocationException |
public Position createPosition(final int offset) throws BadLocationException |
93 |
{ |
{ |
94 |
return new Position() |
return new Position() |
102 |
}; |
}; |
103 |
} |
} |
104 |
|
|
105 |
|
/** |
106 |
|
* Returns the length of the content. |
107 |
|
* |
108 |
|
* @return the length of the content |
109 |
|
*/ |
110 |
public int length() |
public int length() |
111 |
{ |
{ |
112 |
return buf.length(); |
return buf.length(); |
113 |
} |
} |
114 |
|
|
115 |
|
/** |
116 |
|
* Inserts a string at the specified position. |
117 |
|
* |
118 |
|
* @param where the position where the string is inserted |
119 |
|
* @param str the string that is to be inserted |
120 |
|
* |
121 |
|
* @return an UndoableEdit object (currently not supported, so |
122 |
|
* <code>null</code> is returned) |
123 |
|
* |
124 |
|
* @throws BadLocationException if <code>where</code> is not a valid location |
125 |
|
* in the buffer |
126 |
|
*/ |
127 |
public UndoableEdit insertString(int where, String str) |
public UndoableEdit insertString(int where, String str) |
128 |
throws BadLocationException |
throws BadLocationException |
129 |
{ |
{ |
131 |
return null; |
return null; |
132 |
} |
} |
133 |
|
|
134 |
|
/** |
135 |
|
* Removes a piece of content at th specified position. |
136 |
|
* |
137 |
|
* @param where the position where the content is to be removed |
138 |
|
* @param nitems number of characters to be removed |
139 |
|
* |
140 |
|
* @return an UndoableEdit object (currently not supported, so |
141 |
|
* <code>null</code> is returned) |
142 |
|
* |
143 |
|
* @throws BadLocationException if <code>where</code> is not a valid location |
144 |
|
* in the buffer |
145 |
|
*/ |
146 |
public UndoableEdit remove(int where, int nitems) |
public UndoableEdit remove(int where, int nitems) |
147 |
throws BadLocationException |
throws BadLocationException |
148 |
{ |
{ |
150 |
return null; |
return null; |
151 |
} |
} |
152 |
|
|
153 |
|
/** |
154 |
|
* Returns a piece of content as String. |
155 |
|
* |
156 |
|
* @param where the start location of the fragment |
157 |
|
* @param len the length of the fragment |
158 |
|
* |
159 |
|
* @throws BadLocationException if <code>where</code> or |
160 |
|
* <code>where + len</code> are no valid locations in the buffer |
161 |
|
*/ |
162 |
public String getString(int where, int len) throws BadLocationException |
public String getString(int where, int len) throws BadLocationException |
163 |
{ |
{ |
164 |
return buf.substring(where, where+len); |
return buf.substring(where, where+len); |
165 |
} |
} |
166 |
|
|
167 |
|
/** |
168 |
|
* Fetches a piece of content and stores it in a {@link Segment} object. |
169 |
|
* |
170 |
|
* @param where the start location of the fragment |
171 |
|
* @param len the length of the fragment |
172 |
|
* @param txt the Segment object to store the fragment in |
173 |
|
* |
174 |
|
* @throws BadLocationException if <code>where</code> or |
175 |
|
* <code>where + len</code> are no valid locations in the buffer |
176 |
|
*/ |
177 |
public void getChars(int where, int len, Segment txt) |
public void getChars(int where, int len, Segment txt) |
178 |
throws BadLocationException |
throws BadLocationException |
179 |
{ |
{ |