diff options
author | Mark Wielaard <mark@gcc.gnu.org> | 2006-03-10 21:46:48 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2006-03-10 21:46:48 +0000 |
commit | 8aa540d2f783474d1d2e06f16744bf67b9c1facc (patch) | |
tree | ea38c56431c5d4528fb54254c3f8e50f517bede3 /libjava/classpath/javax/swing/text/AbstractDocument.java | |
parent | 27079765d00123f8e53d0e1ef7f9d46559266e6d (diff) | |
download | gcc-8aa540d2f783474d1d2e06f16744bf67b9c1facc.zip gcc-8aa540d2f783474d1d2e06f16744bf67b9c1facc.tar.gz gcc-8aa540d2f783474d1d2e06f16744bf67b9c1facc.tar.bz2 |
Imported GNU Classpath 0.90
Imported GNU Classpath 0.90
* scripts/makemake.tcl: Set gnu/java/awt/peer/swing to ignore.
* gnu/classpath/jdwp/VMFrame.java (SIZE): New constant.
* java/lang/VMCompiler.java: Use gnu.java.security.hash.MD5.
* java/lang/Math.java: New override file.
* java/lang/Character.java: Merged from Classpath.
(start, end): Now 'int's.
(canonicalName): New field.
(CANONICAL_NAME, NO_SPACES_NAME, CONSTANT_NAME): New constants.
(UnicodeBlock): Added argument.
(of): New overload.
(forName): New method.
Updated unicode blocks.
(sets): Updated.
* sources.am: Regenerated.
* Makefile.in: Likewise.
From-SVN: r111942
Diffstat (limited to 'libjava/classpath/javax/swing/text/AbstractDocument.java')
-rw-r--r-- | libjava/classpath/javax/swing/text/AbstractDocument.java | 167 |
1 files changed, 110 insertions, 57 deletions
diff --git a/libjava/classpath/javax/swing/text/AbstractDocument.java b/libjava/classpath/javax/swing/text/AbstractDocument.java index c735388..a3e8c46 100644 --- a/libjava/classpath/javax/swing/text/AbstractDocument.java +++ b/libjava/classpath/javax/swing/text/AbstractDocument.java @@ -538,18 +538,24 @@ public abstract class AbstractDocument implements Document, Serializable DefaultDocumentEvent event = new DefaultDocumentEvent(offset, text.length(), DocumentEvent.EventType.INSERT); - - writeLock(); - UndoableEdit undo = content.insertString(offset, text); - if (undo != null) - event.addEdit(undo); - insertUpdate(event, attributes); - writeUnlock(); + try + { + writeLock(); + UndoableEdit undo = content.insertString(offset, text); + if (undo != null) + event.addEdit(undo); + + insertUpdate(event, attributes); - fireInsertUpdate(event); - if (undo != null) - fireUndoableEditUpdate(new UndoableEditEvent(this, undo)); + fireInsertUpdate(event); + if (undo != null) + fireUndoableEditUpdate(new UndoableEditEvent(this, undo)); + } + finally + { + writeUnlock(); + } } /** @@ -640,6 +646,12 @@ public abstract class AbstractDocument implements Document, Serializable // more times than you've previously called lock, but it doesn't make // sure that the threads calling unlock were the same ones that called lock + // If the current thread holds the write lock, and attempted to also obtain + // a readLock, then numReaders hasn't been incremented and we don't need + // to unlock it here. + if (currentWriter == Thread.currentThread()) + return; + // FIXME: the reference implementation throws a // javax.swing.text.StateInvariantError here if (numReaders == 0) @@ -675,18 +687,21 @@ public abstract class AbstractDocument implements Document, Serializable new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.REMOVE); - removeUpdate(event); - - boolean shouldFire = content.getString(offset, length).length() != 0; - - writeLock(); - UndoableEdit temp = content.remove(offset, length); - writeUnlock(); - - postRemoveUpdate(event); - - if (shouldFire) - fireRemoveUpdate(event); + try + { + writeLock(); + + // The order of the operations below is critical! + removeUpdate(event); + UndoableEdit temp = content.remove(offset, length); + + postRemoveUpdate(event); + fireRemoveUpdate(event); + } + finally + { + writeUnlock(); + } } /** @@ -841,7 +856,7 @@ public abstract class AbstractDocument implements Document, Serializable */ protected void writeLock() { - if (currentWriter!= null && currentWriter.equals(Thread.currentThread())) + if (currentWriter != null && currentWriter.equals(Thread.currentThread())) return; synchronized (documentCV) { @@ -1330,11 +1345,11 @@ public abstract class AbstractDocument implements Document, Serializable public Object getAttribute(Object key) { Object result = attributes.getAttribute(key); - if (result == null && element_parent != null) + if (result == null) { - AttributeSet parentSet = element_parent.getAttributes(); - if (parentSet != null) - result = parentSet.getAttribute(key); + AttributeSet resParent = getResolveParent(); + if (resParent != null) + result = resParent.getAttribute(key); } return result; } @@ -1371,9 +1386,7 @@ public abstract class AbstractDocument implements Document, Serializable */ public AttributeSet getResolveParent() { - if (attributes.getResolveParent() != null) - return attributes.getResolveParent(); - return element_parent.getAttributes(); + return attributes.getResolveParent(); } /** @@ -1573,6 +1586,18 @@ public abstract class AbstractDocument implements Document, Serializable private Element[] children = new Element[0]; /** + * The cached startOffset value. This is used in the case when a + * BranchElement (temporarily) has no child elements. + */ + private int startOffset; + + /** + * The cached endOffset value. This is used in the case when a + * BranchElement (temporarily) has no child elements. + */ + private int endOffset; + + /** * Creates a new <code>BranchElement</code> with the specified * parent and attributes. * @@ -1583,6 +1608,8 @@ public abstract class AbstractDocument implements Document, Serializable public BranchElement(Element parent, AttributeSet attributes) { super(parent, attributes); + startOffset = -1; + endOffset = -1; } /** @@ -1655,7 +1682,7 @@ public abstract class AbstractDocument implements Document, Serializable // return 0 if (offset < getStartOffset()) return 0; - + // XXX: There is surely a better algorithm // as beginning from first element each time. for (int index = 0; index < children.length - 1; ++index) @@ -1695,9 +1722,15 @@ public abstract class AbstractDocument implements Document, Serializable */ public int getEndOffset() { - if (getElementCount() == 0) - throw new NullPointerException("This BranchElement has no children."); - return children[children.length - 1].getEndOffset(); + if (children.length == 0) + { + if (endOffset == -1) + throw new NullPointerException("BranchElement has no children."); + } + else + endOffset = children[children.length - 1].getEndOffset(); + + return endOffset; } /** @@ -1718,13 +1751,20 @@ public abstract class AbstractDocument implements Document, Serializable * * @return the start offset of this element inside the document model * - * @throws NullPointerException if this branch element has no children + * @throws NullPointerException if this branch element has no children and + * no startOffset value has been cached */ public int getStartOffset() { - if (getElementCount() == 0) - throw new NullPointerException("This BranchElement has no children."); - return children[0].getStartOffset(); + if (children.length == 0) + { + if (startOffset == -1) + throw new NullPointerException("BranchElement has no children."); + } + else + startOffset = children[0].getStartOffset(); + + return startOffset; } /** @@ -2022,13 +2062,29 @@ public abstract class AbstractDocument implements Document, Serializable /** The serialization UID (compatible with JDK1.5). */ private static final long serialVersionUID = -8906306331347768017L; - /** Manages the start offset of this element. */ - Position startPos; + /** + * Manages the start offset of this element. + */ + private Position startPos; + + /** + * Manages the end offset of this element. + */ + private Position endPos; - /** Manages the end offset of this element. */ - Position endPos; + /** + * This gets possible added to the startOffset when a startOffset + * outside the document range is requested. + */ + private int startDelta; /** + * This gets possible added to the endOffset when a endOffset + * outside the document range is requested. + */ + private int endDelta; + + /** * Creates a new <code>LeafElement</code>. * * @param parent the parent of this <code>LeafElement</code> @@ -2040,20 +2096,18 @@ public abstract class AbstractDocument implements Document, Serializable int end) { super(parent, attributes); - { - try + int len = content.length(); + startDelta = 0; + if (start > len) + startDelta = start - len; + endDelta = 0; + if (end > len) + endDelta = end - len; + try { - if (parent != null) - { - startPos = parent.getDocument().createPosition(start); - endPos = parent.getDocument().createPosition(end); - } - else - { - startPos = createPosition(start); - endPos = createPosition(end); + startPos = createPosition(start - startDelta); + endPos = createPosition(end - endDelta); } - } catch (BadLocationException ex) { AssertionError as; @@ -2064,7 +2118,6 @@ public abstract class AbstractDocument implements Document, Serializable as.initCause(ex); throw as; } - } } /** @@ -2136,7 +2189,7 @@ public abstract class AbstractDocument implements Document, Serializable */ public int getEndOffset() { - return endPos.getOffset(); + return endPos.getOffset() + endDelta; } /** @@ -2162,7 +2215,7 @@ public abstract class AbstractDocument implements Document, Serializable */ public int getStartOffset() { - return startPos.getOffset(); + return startPos.getOffset() + startDelta; } /** |