Hi,
the title says it all. I saw[0] that painting of the highlight slowed down as
the selected text got longer.

By using pen & paper I found a nice way to minimize the damaged area. Hopefully
that algorithm isnt patented somewhere. :P

the ChangeLog:

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

        * javax/swing/text/DefaultHighlighter.java:
        (changeHighlight): Added code to minimize the damaged area.

This is my last patch for this week I hope it doesn't make classpath go boom. ;)

Cya on Friday!!!
Robert

[0] - http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26414
Index: javax/swing/text/DefaultHighlighter.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultHighlighter.java,v
retrieving revision 1.7
diff -u -r1.7 DefaultHighlighter.java
--- javax/swing/text/DefaultHighlighter.java	21 Feb 2006 10:57:43 -0000	1.7
+++ javax/swing/text/DefaultHighlighter.java	22 Feb 2006 13:32:29 -0000
@@ -45,6 +45,8 @@
 import java.awt.Shape;
 import java.util.ArrayList;
 
+import javax.swing.plaf.TextUI;
+
 public class DefaultHighlighter extends LayeredHighlighter
 {
   public static class DefaultHighlightPainter
@@ -240,26 +242,89 @@
       highlights.toArray(new Highlighter.Highlight[highlights.size()]);
   }
 
-  public void changeHighlight(Object tag, int p0, int p1)
+  public void changeHighlight(Object tag, int n0, int n1)
     throws BadLocationException
   {
     int o0, o1;
     
-    checkPositions(p0, p1);
+    checkPositions(n0, n1);
     HighlightEntry entry = (HighlightEntry) tag;
     o0 = entry.p0;
     o1 = entry.p1;
     
     // Prevent useless write & repaint operations.
-    if (o0 == p0 && o1 == p1)
+    if (o0 == n0 && o1 == n1)
       return;
     
-    entry.p0 = p0;
-    entry.p1 = p1;
+    entry.p0 = n0;
+    entry.p1 = n1;
+    
+    TextUI ui = textComponent.getUI();
+    
+    // Special situation where the old area has to be cleared simply.
+    if (n0 == n1)
+      ui.damageRange(textComponent, o0, o1);
+    // Calculates the areas where a change is really neccessary
+    else if ((o1 > n0 && o1 <= n1)
+        || (n1 > o0 && n1 <= o1))
+      {
+        // [fds, fde) - the first damage region
+        // [sds, sde] - the second damage region
+        int fds, sds;
+        int fde, sde;
+
+        // Calculate first damaged region.
+        if(o0 < n0)
+          {
+            // Damaged region will be cleared as
+            // the old highlight region starts first.
+            fds = o0;
+            fde = n0;
+          }
+        else
+          {
+            // Damaged region will be painted as
+            // the new highlight region starts first.
+            fds = n0;
+            fde = o0;
+          }
+
+        if (o1 < n1)
+          {
+            // Final region will be painted as the
+            // old highlight region finishes first
+            sds = o1;
+            sde = n1;
+          }
+        else
+          {
+            // Final region will be cleared as the
+            // new highlight region finishes first.
+            sds = n1;
+            sde = o1;
+          }
+        
+        // If there is no undamaged region in between
+        // call damageRange only once.
+        if (fde == sds)
+          ui.damageRange(textComponent, fds, sde);
+        else
+          {
+            if (fds != fde)
+              ui.damageRange(textComponent, fds, fde);
+        
+            if (sds != sde)
+              ui.damageRange(textComponent, sds, sde);
+          }
+      }
+    else
+      {
+        // The two regions do not overlap. So mark
+        // both areas as damaged.
+        ui.damageRange(textComponent, o0, o1);
+        ui.damageRange(textComponent, n0, n1);
+      }
     
-    textComponent.getUI().damageRange(textComponent,
-                                      Math.min(p0, o0),
-                                      Math.max(p1, o1));
   }
 
   public void paintLayeredHighlights(Graphics g, int p0, int p1,

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to