Right after committing my last patch I realized that Robert implemented BasicTextUI.damageRange() that way because modelToView() had some problems to get the rectangle right for multiline views. I fixed that in the modelToView() method which should work better now and return the correct rectangles for damageRange to work correctly still.

2006-08-05  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/View.java
        (modelToView): Added special handling for corner case at the end
        of the view and for multiline views.

/Roman
Index: javax/swing/text/View.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/View.java,v
retrieving revision 1.32
diff -u -1 -2 -r1.32 View.java
--- javax/swing/text/View.java	28 Apr 2006 12:13:48 -0000	1.32
+++ javax/swing/text/View.java	5 Aug 2006 14:12:39 -0000
@@ -602,27 +602,64 @@
    *         listed valid values
    */
   public Shape modelToView(int p1, Position.Bias b1,
 			   int p2, Position.Bias b2, Shape a)
     throws BadLocationException
   {
     if (b1 != Position.Bias.Forward && b1 != Position.Bias.Backward)
       throw new IllegalArgumentException
 	("b1 must be either Position.Bias.Forward or Position.Bias.Backward");
     if (b2 != Position.Bias.Forward && b2 != Position.Bias.Backward)
       throw new IllegalArgumentException
 	("b2 must be either Position.Bias.Forward or Position.Bias.Backward");
-    Rectangle s1 = (Rectangle) modelToView(p1, a, b1);
-    Rectangle s2 = (Rectangle) modelToView(p2, a, b2);
-    return SwingUtilities.computeUnion(s1.x, s1.y, s1.width, s1.height, s2);
+
+    Shape s1 = modelToView(p1, a, b1);
+    // Special case for p2 == end index.
+    Shape s2;
+    if (p2 != getEndOffset())
+      {
+        s2 = modelToView(p2, a, b2);
+      }
+    else
+      {
+        try
+          {
+            s2 = modelToView(p2, a, b2);
+          }
+        catch (BadLocationException ex)
+          {
+            // Assume the end rectangle to be at the right edge of the
+            // view.
+            Rectangle aRect = a instanceof Rectangle ? (Rectangle) a
+                                                     : a.getBounds();
+            s2 = new Rectangle(aRect.x + aRect.width - 1, aRect.y, 1,
+                               aRect.height);
+          }
+      }
+
+    // Need to modify the rectangle, so we create a copy in all cases.
+    Rectangle r1 = s1.getBounds();
+    Rectangle r2 = s2 instanceof Rectangle ? (Rectangle) s2
+                                           : s2.getBounds();
+
+    // For multiline view, let the resulting rectangle span the whole view.
+    if (r1.y != r2.y)
+      {
+        Rectangle aRect = a instanceof Rectangle ? (Rectangle) a
+                                                 : a.getBounds();
+        r1.x = aRect.x;
+        r1.width = aRect.width;
+      }
+
+    return SwingUtilities.computeUnion(r2.x, r2.y, r2.width, r2.height, r1);
   }
 
   /**
    * Maps a position in the document into the coordinate space of the View.
    * The output rectangle usually reflects the font height but has a width
    * of zero.
    *
    * This method is deprecated and calls
    * [EMAIL PROTECTED] #modelToView(int, Position.Bias, int, Position.Bias, Shape)} with
    * a bias of [EMAIL PROTECTED] Position.Bias#Forward}.
    *
    * @param pos the position of the character in the model

Reply via email to