Hi,
attached is a patch which fixes some issues with the DefaultCaret.

At first I rewrote the word selection (by mouse) part after seeing that it does
not worked as expected in all cases. Now it reacts in the way the documentation
describes it.

The other fix changes the caret's height to the height of the text line it is
in. In previous versions the height of the component the caret is installed in
was used which caused odd behavior with scrolling in JTextAreas. This is also
described in PR #26809.

The ChangeLog:

2006-03-27  Robert Schuster  <[EMAIL PROTECTED]>

        * javax/swing/text/DefaultCaret.java:
        (mouseClicked): Word selection rewritten.
        (paint): Draw line inside the bounding rectangle.
        (damage): Retrieve caret height from line height.

cya
Robert
Index: javax/swing/text/DefaultCaret.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultCaret.java,v
retrieving revision 1.36
diff -u -r1.36 DefaultCaret.java
--- javax/swing/text/DefaultCaret.java	23 Mar 2006 21:17:22 -0000	1.36
+++ javax/swing/text/DefaultCaret.java	27 Mar 2006 15:20:14 -0000
@@ -399,19 +399,20 @@
               }
             else
               {
-                int nextWord = Utilities.getNextWord(t, newDot);
+                int wordStart = Utilities.getWordStart(t, newDot);
                 
                 // When the mouse points at the offset of the first character
                 // in a word Utilities().getPreviousWord will not return that
                 // word but we want to select that. We have to use
-                // Utilities.nextWord() to get it.
-                if (newDot == nextWord)
+                // Utilities.getWordStart() to get it.
+                if (newDot == wordStart)
                   {
-                    setDot(nextWord);
-                    moveDot(Utilities.getNextWord(t, nextWord));
+                    setDot(wordStart);
+                    moveDot(Utilities.getWordEnd(t, wordStart));
                   }
                 else
                   {
+                    int nextWord = Utilities.getNextWord(t, newDot);
                     int previousWord = Utilities.getPreviousWord(t, newDot);
                     int previousWordEnd = Utilities.getWordEnd(t, previousWord);
                     
@@ -833,7 +834,7 @@
     if (visible)
       {
         g.setColor(textComponent.getCaretColor());
-        g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
+        g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
       }
   }
 
@@ -1100,7 +1101,16 @@
     // must set a valid value here, since otherwise the painting mechanism
     // sets a zero clip and never calls paint.
     if (height <= 0)
-      height = getComponent().getHeight();
+      try
+        {
+          height = textComponent.modelToView(dot).height;
+        }
+      catch (BadLocationException ble)
+        {
+          // Should not happen.
+          throw new InternalError("Caret location not within document range.");
+        }
+      
     repaint();
   }
 

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to