- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathPlainDocument.java
322 lines (306 loc) · 13 KB
/
PlainDocument.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing.text;
importjava.util.Vector;
/**
* A plain document that maintains no character attributes. The
* default element structure for this document is a map of the lines in
* the text. The Element returned by getDefaultRootElement is
* a map of the lines, and each child element represents a line.
* This model does not maintain any character level attributes,
* but each line can be tagged with an arbitrary set of attributes.
* Line to offset, and offset to line translations can be quickly
* performed using the default root element. The structure information
* of the DocumentEvent's fired by edits will indicate the line
* structure changes.
* <p>
* The default content storage management is performed by a
* gapped buffer implementation (GapContent). It supports
* editing reasonably large documents with good efficiency when
* the edits are contiguous or clustered, as is typical.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Timothy Prinzing
* @see Document
* @see AbstractDocument
*/
@SuppressWarnings("serial") // Same-version serialization only
publicclassPlainDocumentextendsAbstractDocument {
/**
* Name of the attribute that specifies the tab
* size for tabs contained in the content. The
* type for the value is Integer.
*/
publicstaticfinalStringtabSizeAttribute = "tabSize";
/**
* Name of the attribute that specifies the maximum
* length of a line, if there is a maximum length.
* The type for the value is Integer.
*/
publicstaticfinalStringlineLimitAttribute = "lineLimit";
/**
* Constructs a plain text document. A default model using
* <code>GapContent</code> is constructed and set.
*/
publicPlainDocument() {
this(newGapContent());
}
/**
* Constructs a plain text document. A default root element is created,
* and the tab size set to 8.
*
* @param c the container for the content
*/
publicPlainDocument(Contentc) {
super(c);
putProperty(tabSizeAttribute, Integer.valueOf(8));
defaultRoot = createDefaultRoot();
}
/**
* Inserts some content into the document.
* Inserting content causes a write lock to be held while the
* actual changes are taking place, followed by notification
* to the observers on the thread that grabbed the write lock.
* <p>
* This method is thread safe, although most Swing methods
* are not. Please see
* <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
* in Swing</A> for more information.
*
* @param offs the starting offset >= 0
* @param str the string to insert; does nothing with null/empty strings
* @param a the attributes for the inserted content
* @throws BadLocationException the given insert position is not a valid
* position within the document
* @see Document#insertString
*/
publicvoidinsertString(intoffs, Stringstr, AttributeSeta) throwsBadLocationException {
// fields don't want to have multiple lines. We may provide a field-specific
// model in the future in which case the filtering logic here will no longer
// be needed.
ObjectfilterNewlines = getProperty("filterNewlines");
if ((filterNewlinesinstanceofBoolean) && filterNewlines.equals(Boolean.TRUE)) {
if ((str != null) && (str.indexOf('\n') >= 0)) {
StringBuilderfiltered = newStringBuilder(str);
intn = filtered.length();
for (inti = 0; i < n; i++) {
if (filtered.charAt(i) == '\n') {
filtered.setCharAt(i, ' ');
}
}
str = filtered.toString();
}
}
super.insertString(offs, str, a);
}
/**
* Gets the default root element for the document model.
*
* @return the root
* @see Document#getDefaultRootElement
*/
publicElementgetDefaultRootElement() {
returndefaultRoot;
}
/**
* Creates the root element to be used to represent the
* default document structure.
*
* @return the element base
*/
protectedAbstractElementcreateDefaultRoot() {
BranchElementmap = (BranchElement) createBranchElement(null, null);
Elementline = createLeafElement(map, null, 0, 1);
Element[] lines = newElement[1];
lines[0] = line;
map.replace(0, 0, lines);
returnmap;
}
/**
* Get the paragraph element containing the given position. Since this
* document only models lines, it returns the line instead.
*/
publicElementgetParagraphElement(intpos){
ElementlineMap = getDefaultRootElement();
returnlineMap.getElement( lineMap.getElementIndex( pos ) );
}
/**
* Updates document structure as a result of text insertion. This
* will happen within a write lock. Since this document simply
* maps out lines, we refresh the line map.
*
* @param chng the change event describing the dit
* @param attr the set of attributes for the inserted text
*/
protectedvoidinsertUpdate(DefaultDocumentEventchng, AttributeSetattr) {
removed.removeAllElements();
added.removeAllElements();
BranchElementlineMap = (BranchElement) getDefaultRootElement();
intoffset = chng.getOffset();
intlength = chng.getLength();
if (offset > 0) {
offset -= 1;
length += 1;
}
intindex = lineMap.getElementIndex(offset);
ElementrmCandidate = lineMap.getElement(index);
intrmOffs0 = rmCandidate.getStartOffset();
intrmOffs1 = rmCandidate.getEndOffset();
intlastOffset = rmOffs0;
try {
if (s == null) {
s = newSegment();
}
getContent().getChars(offset, length, s);
booleanhasBreaks = false;
for (inti = 0; i < length; i++) {
charc = s.array[s.offset + i];
if (c == '\n') {
intbreakOffset = offset + i + 1;
added.addElement(createLeafElement(lineMap, null, lastOffset, breakOffset));
lastOffset = breakOffset;
hasBreaks = true;
}
}
if (hasBreaks) {
removed.addElement(rmCandidate);
if ((offset + length == rmOffs1) && (lastOffset != rmOffs1) &&
((index+1) < lineMap.getElementCount())) {
Elemente = lineMap.getElement(index+1);
removed.addElement(e);
rmOffs1 = e.getEndOffset();
}
if (lastOffset < rmOffs1) {
added.addElement(createLeafElement(lineMap, null, lastOffset, rmOffs1));
}
Element[] aelems = newElement[added.size()];
added.copyInto(aelems);
Element[] relems = newElement[removed.size()];
removed.copyInto(relems);
ElementEditee = newElementEdit(lineMap, index, relems, aelems);
chng.addEdit(ee);
lineMap.replace(index, relems.length, aelems);
}
if (Utilities.isComposedTextAttributeDefined(attr)) {
insertComposedTextUpdate(chng, attr);
}
} catch (BadLocationExceptione) {
thrownewError("Internal error: " + e.toString());
}
super.insertUpdate(chng, attr);
}
/**
* Updates any document structure as a result of text removal.
* This will happen within a write lock. Since the structure
* represents a line map, this just checks to see if the
* removal spans lines. If it does, the two lines outside
* of the removal area are joined together.
*
* @param chng the change event describing the edit
*/
protectedvoidremoveUpdate(DefaultDocumentEventchng) {
removed.removeAllElements();
BranchElementmap = (BranchElement) getDefaultRootElement();
intoffset = chng.getOffset();
intlength = chng.getLength();
intline0 = map.getElementIndex(offset);
intline1 = map.getElementIndex(offset + length);
if (line0 != line1) {
// a line was removed
for (inti = line0; i <= line1; i++) {
removed.addElement(map.getElement(i));
}
intp0 = map.getElement(line0).getStartOffset();
intp1 = map.getElement(line1).getEndOffset();
Element[] aelems = newElement[1];
aelems[0] = createLeafElement(map, null, p0, p1);
Element[] relems = newElement[removed.size()];
removed.copyInto(relems);
ElementEditee = newElementEdit(map, line0, relems, aelems);
chng.addEdit(ee);
map.replace(line0, relems.length, aelems);
} else {
//Check for the composed text element
Elementline = map.getElement(line0);
if (!line.isLeaf()) {
Elementleaf = line.getElement(line.getElementIndex(offset));
if (Utilities.isComposedTextElement(leaf)) {
Element[] aelem = newElement[1];
aelem[0] = createLeafElement(map, null,
line.getStartOffset(), line.getEndOffset());
Element[] relem = newElement[1];
relem[0] = line;
ElementEditee = newElementEdit(map, line0, relem, aelem);
chng.addEdit(ee);
map.replace(line0, 1, aelem);
}
}
}
super.removeUpdate(chng);
}
//
// Inserts the composed text of an input method. The line element
// where the composed text is inserted into becomes an branch element
// which contains leaf elements of the composed text and the text
// backing store.
//
privatevoidinsertComposedTextUpdate(DefaultDocumentEventchng, AttributeSetattr) {
added.removeAllElements();
BranchElementlineMap = (BranchElement) getDefaultRootElement();
intoffset = chng.getOffset();
intlength = chng.getLength();
intindex = lineMap.getElementIndex(offset);
Elementelem = lineMap.getElement(index);
intelemStart = elem.getStartOffset();
intelemEnd = elem.getEndOffset();
BranchElement[] abelem = newBranchElement[1];
abelem[0] = (BranchElement) createBranchElement(lineMap, null);
Element[] relem = newElement[1];
relem[0] = elem;
if (elemStart != offset)
added.addElement(createLeafElement(abelem[0], null, elemStart, offset));
added.addElement(createLeafElement(abelem[0], attr, offset, offset+length));
if (elemEnd != offset+length)
added.addElement(createLeafElement(abelem[0], null, offset+length, elemEnd));
Element[] alelem = newElement[added.size()];
added.copyInto(alelem);
ElementEditee = newElementEdit(lineMap, index, relem, abelem);
chng.addEdit(ee);
abelem[0].replace(0, 0, alelem);
lineMap.replace(index, 1, abelem);
}
privateAbstractElementdefaultRoot;
privateVector<Element> added = newVector<Element>();
privateVector<Element> removed = newVector<Element>();
privatetransientSegments;
}