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

Print this page




 156             fireInsertUpdate(evnt);
 157             fireUndoableEditUpdate(new UndoableEditEvent(this, evnt));
 158         } catch (BadLocationException ble) {
 159             throw new StateInvariantError("problem initializing");
 160         } finally {
 161             writeUnlock();
 162         }
 163 
 164     }
 165 
 166     /**
 167      * Inserts new elements in bulk.  This is useful to allow
 168      * parsing with the document in an unlocked state and
 169      * prepare an element structure modification.  This method
 170      * takes an array of tokens that describe how to update an
 171      * element structure so the time within a write lock can
 172      * be greatly reduced in an asynchronous update situation.
 173      * <p>
 174      * This method is thread safe, although most Swing methods
 175      * are not. Please see
 176      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 177      * in Swing</A> for more information.
 178      *
 179      * @param offset the starting offset &gt;= 0
 180      * @param data the element data
 181      * @exception BadLocationException for an invalid starting offset
 182      */
 183     protected void insert(int offset, ElementSpec[] data) throws BadLocationException {
 184         if (data == null || data.length == 0) {
 185             return;
 186         }
 187 
 188         try {
 189             writeLock();
 190 
 191             // install the content
 192             Content c = getContent();
 193             int n = data.length;
 194             StringBuilder sb = new StringBuilder();
 195             for (int i = 0; i < n; i++) {
 196                 ElementSpec es = data[i];


 409 
 410     /**
 411      * Fetches the list of of style names.
 412      *
 413      * @return all the style names
 414      */
 415     public Enumeration<?> getStyleNames() {
 416         return ((StyleContext) getAttributeContext()).getStyleNames();
 417     }
 418 
 419     /**
 420      * Sets the logical style to use for the paragraph at the
 421      * given position.  If attributes aren't explicitly set
 422      * for character and paragraph attributes they will resolve
 423      * through the logical style assigned to the paragraph, which
 424      * in turn may resolve through some hierarchy completely
 425      * independent of the element hierarchy in the document.
 426      * <p>
 427      * This method is thread safe, although most Swing methods
 428      * are not. Please see
 429      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 430      * in Swing</A> for more information.
 431      *
 432      * @param pos the offset from the start of the document &gt;= 0
 433      * @param s  the logical style to assign to the paragraph, null if none
 434      */
 435     public void setLogicalStyle(int pos, Style s) {
 436         Element paragraph = getParagraphElement(pos);
 437         if ((paragraph != null) && (paragraph instanceof AbstractElement)) {
 438             try {
 439                 writeLock();
 440                 StyleChangeUndoableEdit edit = new StyleChangeUndoableEdit((AbstractElement)paragraph, s);
 441                 ((AbstractElement)paragraph).setResolveParent(s);
 442                 int p0 = paragraph.getStartOffset();
 443                 int p1 = paragraph.getEndOffset();
 444                 DefaultDocumentEvent e =
 445                   new DefaultDocumentEvent(p0, p1 - p0, DocumentEvent.EventType.CHANGE);
 446                 e.addEdit(edit);
 447                 e.end();
 448                 fireChangedUpdate(e);
 449                 fireUndoableEditUpdate(new UndoableEditEvent(this, e));


 466         Style s = null;
 467         Element paragraph = getParagraphElement(p);
 468         if (paragraph != null) {
 469             AttributeSet a = paragraph.getAttributes();
 470             AttributeSet parent = a.getResolveParent();
 471             if (parent instanceof Style) {
 472                 s = (Style) parent;
 473             }
 474         }
 475         return s;
 476     }
 477 
 478     /**
 479      * Sets attributes for some part of the document.
 480      * A write lock is held by this operation while changes
 481      * are being made, and a DocumentEvent is sent to the listeners
 482      * after the change has been successfully completed.
 483      * <p>
 484      * This method is thread safe, although most Swing methods
 485      * are not. Please see
 486      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 487      * in Swing</A> for more information.
 488      *
 489      * @param offset the offset in the document &gt;= 0
 490      * @param length the length &gt;= 0
 491      * @param s the attributes
 492      * @param replace true if the previous attributes should be replaced
 493      *  before setting the new attributes
 494      */
 495     public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
 496         if (length == 0) {
 497             return;
 498         }
 499         try {
 500             writeLock();
 501             DefaultDocumentEvent changes =
 502                 new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);
 503 
 504             // split elements that need it
 505             buffer.change(offset, length, changes);
 506 


 519                 changes.addEdit(new AttributeUndoableEdit(run, sCopy, replace));
 520                 if (replace) {
 521                     attr.removeAttributes(attr);
 522                 }
 523                 attr.addAttributes(s);
 524             }
 525             changes.end();
 526             fireChangedUpdate(changes);
 527             fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
 528         } finally {
 529             writeUnlock();
 530         }
 531 
 532     }
 533 
 534     /**
 535      * Sets attributes for a paragraph.
 536      * <p>
 537      * This method is thread safe, although most Swing methods
 538      * are not. Please see
 539      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 540      * in Swing</A> for more information.
 541      *
 542      * @param offset the offset into the paragraph &gt;= 0
 543      * @param length the number of characters affected &gt;= 0
 544      * @param s the attributes
 545      * @param replace whether to replace existing attributes, or merge them
 546      */
 547     public void setParagraphAttributes(int offset, int length, AttributeSet s,
 548                                        boolean replace) {
 549         try {
 550             writeLock();
 551             DefaultDocumentEvent changes =
 552                 new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);
 553 
 554             AttributeSet sCopy = s.copyAttributes();
 555 
 556             // PENDING(prinz) - this assumes a particular element structure
 557             Element section = getDefaultRootElement();
 558             int index0 = section.getElementIndex(offset);
 559             int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));




 156             fireInsertUpdate(evnt);
 157             fireUndoableEditUpdate(new UndoableEditEvent(this, evnt));
 158         } catch (BadLocationException ble) {
 159             throw new StateInvariantError("problem initializing");
 160         } finally {
 161             writeUnlock();
 162         }
 163 
 164     }
 165 
 166     /**
 167      * Inserts new elements in bulk.  This is useful to allow
 168      * parsing with the document in an unlocked state and
 169      * prepare an element structure modification.  This method
 170      * takes an array of tokens that describe how to update an
 171      * element structure so the time within a write lock can
 172      * be greatly reduced in an asynchronous update situation.
 173      * <p>
 174      * This method is thread safe, although most Swing methods
 175      * are not. Please see
 176      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 177      * in Swing</A> for more information.
 178      *
 179      * @param offset the starting offset &gt;= 0
 180      * @param data the element data
 181      * @exception BadLocationException for an invalid starting offset
 182      */
 183     protected void insert(int offset, ElementSpec[] data) throws BadLocationException {
 184         if (data == null || data.length == 0) {
 185             return;
 186         }
 187 
 188         try {
 189             writeLock();
 190 
 191             // install the content
 192             Content c = getContent();
 193             int n = data.length;
 194             StringBuilder sb = new StringBuilder();
 195             for (int i = 0; i < n; i++) {
 196                 ElementSpec es = data[i];


 409 
 410     /**
 411      * Fetches the list of of style names.
 412      *
 413      * @return all the style names
 414      */
 415     public Enumeration<?> getStyleNames() {
 416         return ((StyleContext) getAttributeContext()).getStyleNames();
 417     }
 418 
 419     /**
 420      * Sets the logical style to use for the paragraph at the
 421      * given position.  If attributes aren't explicitly set
 422      * for character and paragraph attributes they will resolve
 423      * through the logical style assigned to the paragraph, which
 424      * in turn may resolve through some hierarchy completely
 425      * independent of the element hierarchy in the document.
 426      * <p>
 427      * This method is thread safe, although most Swing methods
 428      * are not. Please see
 429      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 430      * in Swing</A> for more information.
 431      *
 432      * @param pos the offset from the start of the document &gt;= 0
 433      * @param s  the logical style to assign to the paragraph, null if none
 434      */
 435     public void setLogicalStyle(int pos, Style s) {
 436         Element paragraph = getParagraphElement(pos);
 437         if ((paragraph != null) && (paragraph instanceof AbstractElement)) {
 438             try {
 439                 writeLock();
 440                 StyleChangeUndoableEdit edit = new StyleChangeUndoableEdit((AbstractElement)paragraph, s);
 441                 ((AbstractElement)paragraph).setResolveParent(s);
 442                 int p0 = paragraph.getStartOffset();
 443                 int p1 = paragraph.getEndOffset();
 444                 DefaultDocumentEvent e =
 445                   new DefaultDocumentEvent(p0, p1 - p0, DocumentEvent.EventType.CHANGE);
 446                 e.addEdit(edit);
 447                 e.end();
 448                 fireChangedUpdate(e);
 449                 fireUndoableEditUpdate(new UndoableEditEvent(this, e));


 466         Style s = null;
 467         Element paragraph = getParagraphElement(p);
 468         if (paragraph != null) {
 469             AttributeSet a = paragraph.getAttributes();
 470             AttributeSet parent = a.getResolveParent();
 471             if (parent instanceof Style) {
 472                 s = (Style) parent;
 473             }
 474         }
 475         return s;
 476     }
 477 
 478     /**
 479      * Sets attributes for some part of the document.
 480      * A write lock is held by this operation while changes
 481      * are being made, and a DocumentEvent is sent to the listeners
 482      * after the change has been successfully completed.
 483      * <p>
 484      * This method is thread safe, although most Swing methods
 485      * are not. Please see
 486      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 487      * in Swing</A> for more information.
 488      *
 489      * @param offset the offset in the document &gt;= 0
 490      * @param length the length &gt;= 0
 491      * @param s the attributes
 492      * @param replace true if the previous attributes should be replaced
 493      *  before setting the new attributes
 494      */
 495     public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
 496         if (length == 0) {
 497             return;
 498         }
 499         try {
 500             writeLock();
 501             DefaultDocumentEvent changes =
 502                 new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);
 503 
 504             // split elements that need it
 505             buffer.change(offset, length, changes);
 506 


 519                 changes.addEdit(new AttributeUndoableEdit(run, sCopy, replace));
 520                 if (replace) {
 521                     attr.removeAttributes(attr);
 522                 }
 523                 attr.addAttributes(s);
 524             }
 525             changes.end();
 526             fireChangedUpdate(changes);
 527             fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
 528         } finally {
 529             writeUnlock();
 530         }
 531 
 532     }
 533 
 534     /**
 535      * Sets attributes for a paragraph.
 536      * <p>
 537      * This method is thread safe, although most Swing methods
 538      * are not. Please see
 539      * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 540      * in Swing</A> for more information.
 541      *
 542      * @param offset the offset into the paragraph &gt;= 0
 543      * @param length the number of characters affected &gt;= 0
 544      * @param s the attributes
 545      * @param replace whether to replace existing attributes, or merge them
 546      */
 547     public void setParagraphAttributes(int offset, int length, AttributeSet s,
 548                                        boolean replace) {
 549         try {
 550             writeLock();
 551             DefaultDocumentEvent changes =
 552                 new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);
 553 
 554             AttributeSet sCopy = s.copyAttributes();
 555 
 556             // PENDING(prinz) - this assumes a particular element structure
 557             Element section = getDefaultRootElement();
 558             int index0 = section.getElementIndex(offset);
 559             int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));