diff options
Diffstat (limited to 'javax/swing/text/AbstractDocument.java')
-rw-r--r-- | javax/swing/text/AbstractDocument.java | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/javax/swing/text/AbstractDocument.java b/javax/swing/text/AbstractDocument.java index 881b4d7f2..1ef81732f 100644 --- a/javax/swing/text/AbstractDocument.java +++ b/javax/swing/text/AbstractDocument.java @@ -543,6 +543,10 @@ public abstract class AbstractDocument implements Document, Serializable * * <p>If a {@link DocumentFilter} is installed in this document, the * corresponding method of the filter object is called.</p> + * + * <p>The method has no effect when <code>text</code> is <code>null</code> + * or has a length of zero.</p> + * * * @param offset the location at which the string should be inserted * @param text the content to be inserted @@ -554,10 +558,14 @@ public abstract class AbstractDocument implements Document, Serializable public void insertString(int offset, String text, AttributeSet attributes) throws BadLocationException { - if (documentFilter != null) - documentFilter.insertString(getBypass(), offset, text, attributes); - else + // Bail out if we have a bogus insertion (Behavior observed in RI). + if (text == null || text.length() == 0) + return; + + if (documentFilter == null) insertStringImpl(offset, text, attributes); + else + documentFilter.insertString(getBypass(), offset, text, attributes); } void insertStringImpl(int offset, String text, AttributeSet attributes) @@ -706,8 +714,14 @@ public abstract class AbstractDocument implements Document, Serializable * Removes a piece of content from this <code>Document</code>. * * <p>If a {@link DocumentFilter} is installed in this document, the - * corresponding method of the filter object is called.</p> - * + * corresponding method of the filter object is called. The + * <code>DocumentFilter</code> is called even if <code>length</code> + * is zero. This is different from {@link #replace}.</p> + * + * <p>Note: When <code>length</code> is zero or below the call is not + * forwarded to the underlying {@link AbstractDocument.Content} instance + * of this document and no exception is thrown.</p> + * * @param offset the start offset of the fragment to be removed * @param length the length of the fragment to be removed * @@ -717,14 +731,18 @@ public abstract class AbstractDocument implements Document, Serializable */ public void remove(int offset, int length) throws BadLocationException { - if (documentFilter != null) - documentFilter.remove(getBypass(), offset, length); - else + if (documentFilter == null) removeImpl(offset, length); + else + documentFilter.remove(getBypass(), offset, length); } void removeImpl(int offset, int length) throws BadLocationException { + // Prevent some unneccessary method invocation (observed in the RI). + if (length <= 0) + return; + DefaultDocumentEvent event = new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.REMOVE); @@ -752,6 +770,10 @@ public abstract class AbstractDocument implements Document, Serializable * * <p>If a {@link DocumentFilter} is installed in this document, the * corresponding method of the filter object is called.</p> + * + * <p>The method has no effect if <code>length</code> is zero (and + * only zero) and, at the same time, <code>text</code> is + * <code>null</code> or has zero length.</p> * * @param offset the start offset of the fragment to be removed * @param length the length of the fragment to be removed @@ -768,10 +790,25 @@ public abstract class AbstractDocument implements Document, Serializable AttributeSet attributes) throws BadLocationException { - if (documentFilter != null) - documentFilter.replace(getBypass(), offset, length, text, attributes); + // Bail out if we have a bogus replacement (Behavior observed in RI). + if (length == 0 + && (text == null || text.length() == 0)) + return; + + if (documentFilter == null) + { + // It is important to call the methods which again do the checks + // of the arguments and the DocumentFilter because subclasses may + // have overridden these methods and provide crucial behavior + // which would be skipped if we call the non-checking variants. + // An example for this is PlainDocument where insertString can + // provide a filtering of newlines. + remove(offset, length); + insertString(offset, text, attributes); + } else - replaceImpl(offset, length, text, attributes); + documentFilter.replace(getBypass(), offset, length, text, attributes); + } void replaceImpl(int offset, int length, String text, |