classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] FYI: DefaultStyledDocument fixlet


From: Anthony Balkissoon
Subject: Re: [cp-patches] FYI: DefaultStyledDocument fixlet
Date: Mon, 19 Dec 2005 11:39:36 -0500

On Mon, 2005-12-19 at 13:59 +0000, Roman Kennke wrote:
> Hi there,
> 
> this patch fixes DefaultStyledDocument.setParagraphAttributes(), so that
> it doesn't hang in an infinite loop like in the testcase of bug#
> 

Hi Roman,
I worked on a patch on Friday that fixed this issue and also added
ElementEdits to the DocumentEvent.  I was going to commit it but my
internet connection went down.  Also, the writeLock() and writeUnlock()
should be in a try/finally block.  The patch includes several changes to
DefaultStyledDocument that I made as I read through the O'Reilly book
beefing up my knowledge about this class before I try to fix PR 24744.
I'll just paste the body of this method (setParagraphAttributes) here,
and let me know if you're okay with this change, and I'll send the full
patch up when it's ready.


  public void setParagraphAttributes(int offset, int length,
                                     AttributeSet attributes,
                                     boolean replace)
  {
    try
      {
        // Must obtain a write lock for this method.  writeLock() and
        // writeUnlock() should always be in try/finally blocks to make
        // sure that locking occurs in a balanced manner.
        writeLock();
        
        // Create a DocumentEvent to use for changedUpdate().
        DefaultDocumentEvent ev = 
          new DefaultDocumentEvent (
                                    offset, 
                                    length, 
                                    DocumentEvent.EventType.CHANGE);
        
        // Have to iterate through all the _paragraph_ elements that are
        // contained or partially contained in the interval
        // (offset, offset + length).
        Element rootElement = getDefaultRootElement();
        int startElement = rootElement.getElementIndex(offset);
        int endElement = rootElement.getElementIndex(offset + length -
1);
        if (endElement < startElement)
          endElement = startElement;
        
        for (int i = startElement; i <= endElement; i++)
          {
            Element par = rootElement.getElement(i);
            MutableAttributeSet a = (MutableAttributeSet)
par.getAttributes();
            // Add the change to the DocumentEvent.
            ev.addEdit(new AttributeUndoableEdit(par, attributes,
replace));
            // If replace is true remove the old attributes.
            if (replace)
              a.removeAttributes(a);
            // Add the new attributes.
            a.addAttributes(attributes);
          }
        fireChangedUpdate(ev);
        fireUndoableEditUpdate(new UndoableEditEvent(this, ev));
      }
    finally
      {
        writeUnlock();
      }
  }

--Tony





reply via email to

[Prev in Thread] Current Thread [Next in Thread]