Hi,
this patch fixes a problem which occured to JTextAreas which wrap at word
boundaries after applying my patches from last night. An exception was thrown
because the loop in WrappedPlainView.WrappedLine.determineNumLines() increased
the loop variable when a utility function returned the same value. I changed the
logic and now it works.

Furthermore some drawing problem re-appeared and I found a slight mistake in the
implementation of WrappedPlainView.WrappedLine.updateDamage: The test whether
numLines and the currently calculated value of determineLines() is different
could never succeed because the method overwrites that variable. Changing the
logic here fixes the issue.

Finally I removed some repaint() calls which I think are unneeded as they would
cause to much repainting and there are specialized child views with a smaller
allocation area which will do that instead.

Here is the ChangeLog:

2006-04-02  Robert Schuster  <[EMAIL PROTECTED]>

        * javax/swing/text/Segment.java:
        (setPosition): Make exception message more verbose.
        * javax/swing/text/WrappedPlainView.java:
        (insertUpdate): Removed unneeded repaint call.
        (changeUpdate): Dito.
        (removeUpdate): Dito.
        (WrappedLine.determineNumLines): Do not return numLines, break
        from loop if no new break point has been calculated.
        (WrappedLine.updateDamage): Rewritten.
        (WrappedLine.insertUpdate): Removed unneeded update code.
        (WrappedLine.removeUpdate): Removed unneeded update code, added
        comment.

cya
Robert
Index: javax/swing/text/Segment.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/Segment.java,v
retrieving revision 1.11
diff -u -r1.11 Segment.java
--- javax/swing/text/Segment.java	1 Apr 2006 22:42:32 -0000	1.11
+++ javax/swing/text/Segment.java	2 Apr 2006 20:32:38 -0000
@@ -245,7 +245,8 @@
 	|| position > getEndIndex())
       throw new IllegalArgumentException("position: " + position
                                          + ", beginIndex: " + getBeginIndex()
-                                         + ", endIndex: " + getEndIndex());
+                                         + ", endIndex: " + getEndIndex()
+                                         + ", text: " + toString());
 
     current = position;
 
Index: javax/swing/text/WrappedPlainView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/WrappedPlainView.java,v
retrieving revision 1.15
diff -u -r1.15 WrappedPlainView.java
--- javax/swing/text/WrappedPlainView.java	1 Apr 2006 22:58:26 -0000	1.15
+++ javax/swing/text/WrappedPlainView.java	2 Apr 2006 20:32:38 -0000
@@ -276,6 +276,7 @@
   {
     Container c = getContainer();
     Rectangle alloc = new Rectangle(0, 0, getWidth(), getHeight());
+    
     updateMetrics();
     
     try
@@ -340,8 +341,8 @@
   public void insertUpdate (DocumentEvent e, Shape a, ViewFactory f)
   {
     super.insertUpdate(e, a, viewFactory);
-    // FIXME: could improve performance by repainting only the necessary area
-    getContainer().repaint();
+
+    // No repaint needed, as this is done by the WrappedLine instances.
   }
   
   /**
@@ -351,8 +352,8 @@
   public void removeUpdate (DocumentEvent e, Shape a, ViewFactory f)
   {
     super.removeUpdate(e, a, viewFactory);
-    // FIXME: could improve performance by repainting only the necessary area
-    getContainer().repaint();
+    
+    // No repaint needed, as this is done by the WrappedLine instances.
   }
   
   /**
@@ -363,8 +364,8 @@
   public void changedUpdate (DocumentEvent e, Shape a, ViewFactory f)
   {
     super.changedUpdate(e, a, viewFactory);
-    // FIXME: could improve performance by repainting only the necessary area
-    getContainer().repaint();
+    
+    // No repaint needed, as this is done by the WrappedLine instances.
   }
     
   class WrappedLineCreator implements ViewFactory
@@ -450,30 +451,32 @@
     }
     
     /**
-     * Determines the number of logical lines that the Element
-     * needs to be displayed
-     * @return the number of lines needed to display the Element
+     * Calculates the number of logical lines that the Element
+     * needs to be displayed and updates the variable numLines
+     * accordingly.
      */
-    int determineNumLines()
+    void determineNumLines()
     {      
       numLines = 0;
       int end = getEndOffset();
       if (end == 0)
-        return 0;
+        return;
             
-      int breakPoint;
+      int breakPoint = -1;
       for (int i = getStartOffset(); i < end;)
         {
           numLines ++;
           // careful: check that there's no off-by-one problem here
           // depending on which position calculateBreakPosition returns
           breakPoint = calculateBreakPosition(i, end);
+          
+          // If breakPoint is equal to the current index no further
+          // line is needed and we can end the loop.
           if (breakPoint == i)
-            i ++;
+            break;
           else
             i = breakPoint;
         }
-      return numLines;
     }
     
     /**
@@ -629,12 +632,11 @@
      */
     void updateDamage (Rectangle a)
     {
-      int newNumLines = determineNumLines();
-      if (numLines != newNumLines)
-        {
-          numLines = newNumLines;
-          getContainer().repaint();
-        }
+      int oldNumLines = numLines;
+      determineNumLines();
+      
+      if (numLines != oldNumLines)
+        preferenceChanged(this, false, true);
       else
         getContainer().repaint(a.x, a.y, a.width, a.height);
     }
@@ -649,11 +651,6 @@
      */
     public void insertUpdate (DocumentEvent changes, Shape a, ViewFactory f)
     {
-      int oldNumLines = numLines;
-      determineNumLines();
-      if (oldNumLines != numLines)
-        getParent().preferenceChanged(this, false, true);
-      
       updateDamage((Rectangle)a); 
     }
     
@@ -667,10 +664,14 @@
      */
     public void removeUpdate (DocumentEvent changes, Shape a, ViewFactory f)
     {
-      int oldNumLines = numLines;
-      determineNumLines();
-      if (oldNumLines != numLines)
-        getParent().preferenceChanged(this, false, true);
+      // Note: This method is not called when characters from the
+      // end of the document are removed. The reason for this
+      // can be found in the implementation of View.forwardUpdate:
+      // The document event will denote offsets which do not exist
+      // any more, getViewIndex() will therefore return -1 and this
+      // makes View.forwardUpdate() skip this method call.
+      // However this seems to cause no trouble and as it reduces the
+      // number of method calls it can stay this way.
       
       updateDamage((Rectangle)a); 
     }

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to