src/share/classes/javax/swing/text/AbstractDocument.java

Print this page




 391      * is being executed.  The runnable itself may <em>not</em>
 392      * make any mutations.
 393      * <p>
 394      * This is implemented to acquire a read lock for the duration
 395      * of the runnables execution.  There may be multiple runnables
 396      * executing at the same time, and all writers will be blocked
 397      * while there are active rendering runnables.  If the runnable
 398      * throws an exception, its lock will be safely released.
 399      * There is no protection against a runnable that never exits,
 400      * which will effectively leave the document locked for it's
 401      * lifetime.
 402      * <p>
 403      * If the given runnable attempts to make any mutations in
 404      * this implementation, a deadlock will occur.  There is
 405      * no tracking of individual rendering threads to enable
 406      * detecting this situation, but a subclass could incur
 407      * the overhead of tracking them and throwing an error.
 408      * <p>
 409      * This method is thread safe, although most Swing methods
 410      * are not. Please see
 411      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 412      * in Swing</A> for more information.
 413      *
 414      * @param r the renderer to execute
 415      */
 416     public void render(Runnable r) {
 417         readLock();
 418         try {
 419             r.run();
 420         } finally {
 421             readUnlock();
 422         }
 423     }
 424 
 425     /**
 426      * Returns the length of the data.  This is the number of
 427      * characters of content that represents the users data.
 428      *
 429      * @return the length &gt;= 0
 430      * @see Document#getLength
 431      */


 552             writeLock();
 553             try {
 554                 DefaultDocumentEvent e
 555                     = new DefaultDocumentEvent(0, getLength(),
 556                                                DocumentEvent.EventType.INSERT);
 557                 updateBidi( e );
 558             } finally {
 559                 writeUnlock();
 560             }
 561         }
 562     }
 563 
 564     /**
 565      * Removes some content from the document.
 566      * Removing content causes a write lock to be held while the
 567      * actual changes are taking place.  Observers are notified
 568      * of the change on the thread that called this method.
 569      * <p>
 570      * This method is thread safe, although most Swing methods
 571      * are not. Please see
 572      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 573      * in Swing</A> for more information.
 574      *
 575      * @param offs the starting offset &gt;= 0
 576      * @param len the number of characters to remove &gt;= 0
 577      * @exception BadLocationException  the given remove position is not a valid
 578      *   position within the document
 579      * @see Document#remove
 580      */
 581     public void remove(int offs, int len) throws BadLocationException {
 582         DocumentFilter filter = getDocumentFilter();
 583 
 584         writeLock();
 585         try {
 586             if (filter != null) {
 587                 filter.remove(getFilterBypass(), offs, len);
 588             }
 589             else {
 590                 handleRemove(offs, len);
 591             }
 592         } finally {


 665                 if (length > 0) {
 666                     remove(offset, length);
 667                 }
 668                 if (text != null && text.length() > 0) {
 669                     insertString(offset, text, attrs);
 670                 }
 671             }
 672         } finally {
 673             writeUnlock();
 674         }
 675     }
 676 
 677     /**
 678      * Inserts some content into the document.
 679      * Inserting content causes a write lock to be held while the
 680      * actual changes are taking place, followed by notification
 681      * to the observers on the thread that grabbed the write lock.
 682      * <p>
 683      * This method is thread safe, although most Swing methods
 684      * are not. Please see
 685      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 686      * in Swing</A> for more information.
 687      *
 688      * @param offs the starting offset &gt;= 0
 689      * @param str the string to insert; does nothing with null/empty strings
 690      * @param a the attributes for the inserted content
 691      * @exception BadLocationException  the given insert position is not a valid
 692      *   position within the document
 693      * @see Document#insertString
 694      */
 695     public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
 696         if ((str == null) || (str.length() == 0)) {
 697             return;
 698         }
 699         DocumentFilter filter = getDocumentFilter();
 700 
 701         writeLock();
 702 
 703         try {
 704             if (filter != null) {
 705                 filter.insertString(getFilterBypass(), offs, str, a);


 799      *
 800      * @param offset the starting offset &gt;= 0
 801      * @param length the number of characters to retrieve &gt;= 0
 802      * @param txt the Segment object to retrieve the text into
 803      * @exception BadLocationException  the range given includes a position
 804      *   that is not a valid position within the document
 805      */
 806     public void getText(int offset, int length, Segment txt) throws BadLocationException {
 807         if (length < 0) {
 808             throw new BadLocationException("Length must be positive", length);
 809         }
 810         data.getChars(offset, length, txt);
 811     }
 812 
 813     /**
 814      * Returns a position that will track change as the document
 815      * is altered.
 816      * <p>
 817      * This method is thread safe, although most Swing methods
 818      * are not. Please see
 819      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 820      * in Swing</A> for more information.
 821      *
 822      * @param offs the position in the model &gt;= 0
 823      * @return the position
 824      * @exception BadLocationException  if the given position does not
 825      *   represent a valid location in the associated document
 826      * @see Document#createPosition
 827      */
 828     public synchronized Position createPosition(int offs) throws BadLocationException {
 829         return data.createPosition(offs);
 830     }
 831 
 832     /**
 833      * Returns a position that represents the start of the document.  The
 834      * position returned can be counted on to track change and stay
 835      * located at the beginning of the document.
 836      *
 837      * @return the position
 838      */
 839     public final Position getStartPosition() {




 391      * is being executed.  The runnable itself may <em>not</em>
 392      * make any mutations.
 393      * <p>
 394      * This is implemented to acquire a read lock for the duration
 395      * of the runnables execution.  There may be multiple runnables
 396      * executing at the same time, and all writers will be blocked
 397      * while there are active rendering runnables.  If the runnable
 398      * throws an exception, its lock will be safely released.
 399      * There is no protection against a runnable that never exits,
 400      * which will effectively leave the document locked for it's
 401      * lifetime.
 402      * <p>
 403      * If the given runnable attempts to make any mutations in
 404      * this implementation, a deadlock will occur.  There is
 405      * no tracking of individual rendering threads to enable
 406      * detecting this situation, but a subclass could incur
 407      * the overhead of tracking them and throwing an error.
 408      * <p>
 409      * This method is thread safe, although most Swing methods
 410      * are not. Please see
 411      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 412      * in Swing</A> for more information.
 413      *
 414      * @param r the renderer to execute
 415      */
 416     public void render(Runnable r) {
 417         readLock();
 418         try {
 419             r.run();
 420         } finally {
 421             readUnlock();
 422         }
 423     }
 424 
 425     /**
 426      * Returns the length of the data.  This is the number of
 427      * characters of content that represents the users data.
 428      *
 429      * @return the length &gt;= 0
 430      * @see Document#getLength
 431      */


 552             writeLock();
 553             try {
 554                 DefaultDocumentEvent e
 555                     = new DefaultDocumentEvent(0, getLength(),
 556                                                DocumentEvent.EventType.INSERT);
 557                 updateBidi( e );
 558             } finally {
 559                 writeUnlock();
 560             }
 561         }
 562     }
 563 
 564     /**
 565      * Removes some content from the document.
 566      * Removing content causes a write lock to be held while the
 567      * actual changes are taking place.  Observers are notified
 568      * of the change on the thread that called this method.
 569      * <p>
 570      * This method is thread safe, although most Swing methods
 571      * are not. Please see
 572      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 573      * in Swing</A> for more information.
 574      *
 575      * @param offs the starting offset &gt;= 0
 576      * @param len the number of characters to remove &gt;= 0
 577      * @exception BadLocationException  the given remove position is not a valid
 578      *   position within the document
 579      * @see Document#remove
 580      */
 581     public void remove(int offs, int len) throws BadLocationException {
 582         DocumentFilter filter = getDocumentFilter();
 583 
 584         writeLock();
 585         try {
 586             if (filter != null) {
 587                 filter.remove(getFilterBypass(), offs, len);
 588             }
 589             else {
 590                 handleRemove(offs, len);
 591             }
 592         } finally {


 665                 if (length > 0) {
 666                     remove(offset, length);
 667                 }
 668                 if (text != null && text.length() > 0) {
 669                     insertString(offset, text, attrs);
 670                 }
 671             }
 672         } finally {
 673             writeUnlock();
 674         }
 675     }
 676 
 677     /**
 678      * Inserts some content into the document.
 679      * Inserting content causes a write lock to be held while the
 680      * actual changes are taking place, followed by notification
 681      * to the observers on the thread that grabbed the write lock.
 682      * <p>
 683      * This method is thread safe, although most Swing methods
 684      * are not. Please see
 685      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 686      * in Swing</A> for more information.
 687      *
 688      * @param offs the starting offset &gt;= 0
 689      * @param str the string to insert; does nothing with null/empty strings
 690      * @param a the attributes for the inserted content
 691      * @exception BadLocationException  the given insert position is not a valid
 692      *   position within the document
 693      * @see Document#insertString
 694      */
 695     public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
 696         if ((str == null) || (str.length() == 0)) {
 697             return;
 698         }
 699         DocumentFilter filter = getDocumentFilter();
 700 
 701         writeLock();
 702 
 703         try {
 704             if (filter != null) {
 705                 filter.insertString(getFilterBypass(), offs, str, a);


 799      *
 800      * @param offset the starting offset &gt;= 0
 801      * @param length the number of characters to retrieve &gt;= 0
 802      * @param txt the Segment object to retrieve the text into
 803      * @exception BadLocationException  the range given includes a position
 804      *   that is not a valid position within the document
 805      */
 806     public void getText(int offset, int length, Segment txt) throws BadLocationException {
 807         if (length < 0) {
 808             throw new BadLocationException("Length must be positive", length);
 809         }
 810         data.getChars(offset, length, txt);
 811     }
 812 
 813     /**
 814      * Returns a position that will track change as the document
 815      * is altered.
 816      * <p>
 817      * This method is thread safe, although most Swing methods
 818      * are not. Please see
 819      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 820      * in Swing</A> for more information.
 821      *
 822      * @param offs the position in the model &gt;= 0
 823      * @return the position
 824      * @exception BadLocationException  if the given position does not
 825      *   represent a valid location in the associated document
 826      * @see Document#createPosition
 827      */
 828     public synchronized Position createPosition(int offs) throws BadLocationException {
 829         return data.createPosition(offs);
 830     }
 831 
 832     /**
 833      * Returns a position that represents the start of the document.  The
 834      * position returned can be counted on to track change and stay
 835      * located at the beginning of the document.
 836      *
 837      * @return the position
 838      */
 839     public final Position getStartPosition() {