1 7 package javax.swing.text; 8 9 import java.util.Vector ; 10 import javax.swing.event.*; 11 12 43 public class PlainDocument extends AbstractDocument { 44 45 50 public static final String tabSizeAttribute = "tabSize"; 51 52 57 public static final String lineLimitAttribute = "lineLimit"; 58 59 63 public PlainDocument() { 64 this(new GapContent ()); 65 } 66 67 73 public PlainDocument(Content c) { 74 super(c); 75 putProperty(tabSizeAttribute, new Integer (8)); 76 defaultRoot = createDefaultRoot(); 77 } 78 79 97 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 98 Object filterNewlines = getProperty("filterNewlines"); 102 if ((filterNewlines instanceof Boolean ) && filterNewlines.equals(Boolean.TRUE)) { 103 if ((str != null) && (str.indexOf('\n') >= 0)) { 104 StringBuffer filtered = new StringBuffer (str); 105 int n = filtered.length(); 106 for (int i = 0; i < n; i++) { 107 if (filtered.charAt(i) == '\n') { 108 filtered.setCharAt(i, ' '); 109 } 110 } 111 str = filtered.toString(); 112 } 113 } 114 super.insertString(offs, str, a); 115 } 116 117 123 public Element getDefaultRootElement() { 124 return defaultRoot; 125 } 126 127 133 protected AbstractElement createDefaultRoot() { 134 BranchElement map = (BranchElement) createBranchElement(null, null); 135 Element line = createLeafElement(map, null, 0, 1); 136 Element [] lines = new Element [1]; 137 lines[0] = line; 138 map.replace(0, 0, lines); 139 return map; 140 } 141 142 146 public Element getParagraphElement(int pos){ 147 Element lineMap = getDefaultRootElement(); 148 return lineMap.getElement( lineMap.getElementIndex( pos ) ); 149 } 150 151 159 protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) { 160 removed.removeAllElements(); 161 added.removeAllElements(); 162 BranchElement lineMap = (BranchElement) getDefaultRootElement(); 163 int offset = chng.getOffset(); 164 int length = chng.getLength(); 165 if (offset > 0) { 166 offset -= 1; 167 length += 1; 168 } 169 int index = lineMap.getElementIndex(offset); 170 Element rmCandidate = lineMap.getElement(index); 171 int rmOffs0 = rmCandidate.getStartOffset(); 172 int rmOffs1 = rmCandidate.getEndOffset(); 173 int lastOffset = rmOffs0; 174 try { 175 if (s == null) { 176 s = new Segment (); 177 } 178 getContent().getChars(offset, length, s); 179 boolean hasBreaks = false; 180 for (int i = 0; i < length; i++) { 181 char c = s.array[s.offset + i]; 182 if (c == '\n') { 183 int breakOffset = offset + i + 1; 184 added.addElement(createLeafElement(lineMap, null, lastOffset, breakOffset)); 185 lastOffset = breakOffset; 186 hasBreaks = true; 187 } 188 } 189 if (hasBreaks) { 190 int rmCount = 1; 191 removed.addElement(rmCandidate); 192 if ((offset + length == rmOffs1) && (lastOffset != rmOffs1) && 193 ((index+1) < lineMap.getElementCount())) { 194 rmCount += 1; 195 Element e = lineMap.getElement(index+1); 196 removed.addElement(e); 197 rmOffs1 = e.getEndOffset(); 198 } 199 if (lastOffset < rmOffs1) { 200 added.addElement(createLeafElement(lineMap, null, lastOffset, rmOffs1)); 201 } 202 203 Element [] aelems = new Element [added.size()]; 204 added.copyInto(aelems); 205 Element [] relems = new Element [removed.size()]; 206 removed.copyInto(relems); 207 ElementEdit ee = new ElementEdit(lineMap, index, relems, aelems); 208 chng.addEdit(ee); 209 lineMap.replace(index, relems.length, aelems); 210 } 211 if (Utilities.isComposedTextAttributeDefined(attr)) { 212 insertComposedTextUpdate(chng, attr); 213 } 214 } catch (BadLocationException e) { 215 throw new Error ("Internal error: " + e.toString()); 216 } 217 super.insertUpdate(chng, attr); 218 } 219 220 229 protected void removeUpdate(DefaultDocumentEvent chng) { 230 removed.removeAllElements(); 231 BranchElement map = (BranchElement) getDefaultRootElement(); 232 int offset = chng.getOffset(); 233 int length = chng.getLength(); 234 int line0 = map.getElementIndex(offset); 235 int line1 = map.getElementIndex(offset + length); 236 if (line0 != line1) { 237 for (int i = line0; i <= line1; i++) { 239 removed.addElement(map.getElement(i)); 240 } 241 int p0 = map.getElement(line0).getStartOffset(); 242 int p1 = map.getElement(line1).getEndOffset(); 243 Element [] aelems = new Element [1]; 244 aelems[0] = createLeafElement(map, null, p0, p1); 245 Element [] relems = new Element [removed.size()]; 246 removed.copyInto(relems); 247 ElementEdit ee = new ElementEdit(map, line0, relems, aelems); 248 chng.addEdit(ee); 249 map.replace(line0, relems.length, aelems); 250 } else { 251 Element line = map.getElement(line0); 253 if (!line.isLeaf()) { 254 Element leaf = line.getElement(line.getElementIndex(offset)); 255 if (Utilities.isComposedTextElement(leaf)) { 256 Element [] aelem = new Element [1]; 257 aelem[0] = createLeafElement(map, null, 258 line.getStartOffset(), line.getEndOffset()); 259 Element [] relem = new Element [1]; 260 relem[0] = line; 261 ElementEdit ee = new ElementEdit(map, line0, relem, aelem); 262 chng.addEdit(ee); 263 map.replace(line0, 1, aelem); 264 } 265 } 266 } 267 super.removeUpdate(chng); 268 } 269 270 private void insertComposedTextUpdate(DefaultDocumentEvent chng, AttributeSet attr) { 277 added.removeAllElements(); 278 BranchElement lineMap = (BranchElement) getDefaultRootElement(); 279 int offset = chng.getOffset(); 280 int length = chng.getLength(); 281 int index = lineMap.getElementIndex(offset); 282 Element elem = lineMap.getElement(index); 283 int elemStart = elem.getStartOffset(); 284 int elemEnd = elem.getEndOffset(); 285 BranchElement[] abelem = new BranchElement[1]; 286 abelem[0] = (BranchElement) createBranchElement(lineMap, null); 287 Element [] relem = new Element [1]; 288 relem[0] = elem; 289 if (elemStart != offset) 290 added.addElement(createLeafElement(abelem[0], null, elemStart, offset)); 291 added.addElement(createLeafElement(abelem[0], attr, offset, offset+length)); 292 if (elemEnd != offset+length) 293 added.addElement(createLeafElement(abelem[0], null, offset+length, elemEnd)); 294 Element [] alelem = new Element [added.size()]; 295 added.copyInto(alelem); 296 ElementEdit ee = new ElementEdit(lineMap, index, relem, abelem); 297 chng.addEdit(ee); 298 299 abelem[0].replace(0, 0, alelem); 300 lineMap.replace(index, 1, abelem); 301 } 302 303 private AbstractElement defaultRoot; 304 private Vector added = new Vector (); private Vector removed = new Vector (); private transient Segment s; 307 } 308 | Popular Tags |