Forgot to attach the patch.

--Tony

On Mon, 2005-10-03 at 16:07 -0400, Anthony Balkissoon wrote:
> This fix for PlainView solves one of the problems that was leading to
> bug 24152, but it is not done yet.
> 
> The bug report is viewable here:
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24152
> 
> This caches the length of the longest line in the View so that it
> doesn't have to be calculated each time.
> 
> However, we need to add a way to update this when the Document changes,
> I think it might be through the updateDamaged method and a
> DocumentListener, but I'm not 100% sure, I'm still working through it.
> 
> This change leads to significantly better results than posted in the bug
> report.  However, the very first time the text is shown there is still a
> huge delay, and this will again be fixed if we can update the longest
> line whenever the Document is updated, then we'll have it calculated as
> soon as it was initialized with some text.  So this is still a work in
> progress.
> 
> 
> 2005-10-03  Anthony Balkissoon  <[EMAIL PROTECTED]>
> 
>       * javax/swing/text/PlainView.java:
>       (maxLineLength): New variable to cache the length of the longest line.
>       (determineMaxLength): New implementation method.
>       (getPreferredSpan): Call determine max length instead of calculating
>       it here.
> 
> --Tony
Index: javax/swing/text/PlainView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/PlainView.java,v
retrieving revision 1.12
diff -u -r1.12 PlainView.java
--- javax/swing/text/PlainView.java	8 Sep 2005 13:41:45 -0000	1.12
+++ javax/swing/text/PlainView.java	3 Oct 2005 19:55:53 -0000
@@ -59,6 +59,9 @@
 
   Font font;
   
+  /** The length of the longest line in the Document **/
+  float maxLineLength = -1;
+  
   protected FontMetrics metrics;
 
   public PlainView(Element elem)
@@ -207,6 +210,39 @@
     return (float) (Math.floor(x / tabSizePixels) + 1) * tabSizePixels;
   }
 
+  /**
+   * Returns the length of the longest line, used for getting the span
+   * @return the length of the longest line
+   */
+  float determineMaxLineLength()
+  {
+    // if the longest line is cached, return the cached value
+    if (maxLineLength != -1)
+      return maxLineLength;
+    
+    // otherwise we have to go through all the lines and find it
+    Element el = getElement();
+    Segment seg = new Segment();
+    float span = 0;
+    for (int i = 0; i < el.getElementCount(); i++)
+      {
+        Element child = el.getElement(i);
+        int start = child.getStartOffset();
+        int end = child.getEndOffset();
+        try
+          {
+            el.getDocument().getText(start, start + end, seg);
+          }
+        catch (BadLocationException ex)
+          {
+          }
+        int width = metrics.charsWidth(seg.array, seg.offset, seg.count);
+        span = Math.max(span, width);
+      }
+    maxLineLength = span;
+    return maxLineLength;
+  }
+  
   public float getPreferredSpan(int axis)
   {
     if (axis != X_AXIS && axis != Y_AXIS)
@@ -217,36 +253,16 @@
 
     float span = 0;
     Element el = getElement();
-    Document doc = el.getDocument();
-    Segment seg = new Segment();
 
     switch (axis)
       {
       case X_AXIS:
-        // calculate the maximum of the line's widths
-        for (int i = 0; i < el.getElementCount(); i++)
-          {
-            Element child = el.getElement(i);
-            int start = child.getStartOffset();
-            int end = child.getEndOffset();
-            try {
-              doc.getText(start, start + end, seg);
-            }
-            catch (BadLocationException ex)
-              {
-                // throw new ClasspathAssertionError
-                // ("no BadLocationException should be thrown here");
-              }
-            int width = metrics.charsWidth(seg.array, seg.offset, seg.count);
-            span = Math.max(span, width);
-          }
-        break;
+        span = determineMaxLineLength();
       case Y_AXIS:
       default:
         span = metrics.getHeight() * el.getElementCount();
         break;
       }
-
     return span;
   }
 
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to