1 /* StringContent.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.io.Serializable;
42 import java.util.Iterator;
43 import java.util.Vector;
45 import javax.swing.undo.AbstractUndoableEdit;
46 import javax.swing.undo.CannotRedoException;
47 import javax.swing.undo.CannotUndoException;
48 import javax.swing.undo.UndoableEdit;
51 * An implementation of the <code>AbstractDocument.Content</code>
52 * interface useful for small documents or debugging. The character
53 * content is a simple character array. It's not really efficient.
55 * <p>Do not use this class for large size.</p>
57 public final class StringContent implements AbstractDocument.Content, Serializable
59 private char[] content;
63 private Vector positions = new Vector();
65 private class InsertUndo extends AbstractUndoableEdit
71 private String redoContent;
73 public InsertUndo(int start, int length)
85 StringContent.this.checkLocation(this.start, this.length);
86 this.redoContent = new String(StringContent.this.content, this.start, this.length);
87 StringContent.this.remove(this.start, this.length);
89 catch (BadLocationException b)
91 throw new CannotUndoException();
100 StringContent.this.insertString(this.start, this.redoContent);
102 catch (BadLocationException b)
104 throw new CannotRedoException();
109 private class RemoveUndo extends AbstractUndoableEdit
113 private String undoString;
115 public RemoveUndo(int start, String str)
119 this.undoString = str;
127 StringContent.this.insertString(this.start, this.undoString);
129 catch (BadLocationException bad)
131 throw new CannotUndoException();
140 int end = this.undoString.length();
141 StringContent.this.remove(this.start, end);
143 catch (BadLocationException bad)
145 throw new CannotRedoException();
150 private class StickyPosition implements Position
152 private int offset = -1;
154 public StickyPosition(int offset)
156 this.offset = offset;
159 private void setOffset(int offset)
161 this.offset = this.offset >= 0 ? offset : -1;
167 public int getOffset()
169 return offset < 0 ? 0 : offset;
173 public StringContent()
178 public StringContent(int initialLength)
181 if (initialLength < 1)
183 this.content = new char[initialLength];
184 this.content[0] = '\n';
188 protected Vector getPositionsInRange(Vector v,
192 Vector refPos = new Vector();
193 Iterator iter = this.positions.iterator();
194 while(iter.hasNext())
196 Position p = (Position)iter.next();
197 if ((offset <= p.getOffset())
198 && (p.getOffset() <= (offset + length)))
204 public Position createPosition(int offset) throws BadLocationException
206 if (offset < this.count || offset > this.count)
207 checkLocation(offset, 0);
208 StickyPosition sp = new StickyPosition(offset);
209 this.positions.add(sp);
218 public UndoableEdit insertString(int where, String str)
219 throws BadLocationException
221 checkLocation(where, 0);
222 if (where == this.count)
223 throw new BadLocationException("Invalid location", 1);
225 throw new NullPointerException();
226 char[] insert = str.toCharArray();
227 char[] temp = new char[this.content.length + insert.length];
228 this.count += insert.length;
229 // Copy array and insert the string.
231 System.arraycopy(this.content, 0, temp, 0, where);
232 System.arraycopy(insert, 0, temp, where, insert.length);
233 System.arraycopy(this.content, where, temp, (where + insert.length), (temp.length - where - insert.length));
234 if (this.content.length < temp.length)
235 this.content = new char[temp.length];
236 // Copy the result in the original char array.
237 System.arraycopy(temp, 0, this.content, 0, temp.length);
238 // Move all the positions.
239 Vector refPos = getPositionsInRange(this.positions, where, temp.length - where);
240 Iterator iter = refPos.iterator();
241 while (iter.hasNext())
243 StickyPosition p = (StickyPosition)iter.next();
244 p.setOffset(p.getOffset() + str.length());
246 InsertUndo iundo = new InsertUndo(where, insert.length);
250 public UndoableEdit remove(int where, int nitems) throws BadLocationException
252 checkLocation(where, nitems);
253 char[] temp = new char[(this.content.length - nitems)];
254 this.count = this.count - nitems;
255 RemoveUndo rundo = new RemoveUndo(where, new String(this.content, where, nitems));
257 System.arraycopy(this.content, 0, temp, 0, where);
258 System.arraycopy(this.content, where + nitems, temp, where, this.content.length - where - nitems);
259 this.content = new char[temp.length];
260 // Then copy the result in the original char array.
261 System.arraycopy(temp, 0, this.content, 0, this.content.length);
262 // Move all the positions.
263 Vector refPos = getPositionsInRange(this.positions, where, this.content.length + nitems - where);
264 Iterator iter = refPos.iterator();
265 while (iter.hasNext())
267 StickyPosition p = (StickyPosition)iter.next();
268 int result = p.getOffset() - nitems;
271 this.positions.remove(p);
276 public String getString(int where, int len) throws BadLocationException
278 checkLocation(where, len);
279 return new String (this.content, where, len);
282 public void getChars(int where, int len, Segment txt) throws BadLocationException
284 checkLocation(where, len);
287 txt.array = this.content;
293 private void checkLocation(int where, int len) throws BadLocationException
296 throw new BadLocationException("Invalid location", 1);
297 else if (where > this.count)
298 throw new BadLocationException("Invalid location", this.count);
299 else if ((where + len)>this.count)
300 throw new BadLocationException("Invalid range", this.count);