classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: update caret position when Document changes


From: Roman Kennke
Subject: [cp-patches] FYI: update caret position when Document changes
Date: Thu, 07 Jul 2005 15:21:37 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

Hi,

when the document of a JTextComponent changes, the caret position must be updated accordingly so that it stays in sync.

This is fixed with the attached patch.

2005-07-07  Roman Kennke  <address@hidden>

       * javax/swing/plaf/basic/BasicTextUI.java
       (DocumentHandler.insertUpdate): Update caret position when document
       changes.
       (DocumentHandler.removeUpdate): Update caret position when document
       changes.

/Roman

Index: javax/swing/plaf/basic/BasicTextUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTextUI.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- javax/swing/plaf/basic/BasicTextUI.java     2 Jul 2005 20:32:50 -0000       
1.25
+++ javax/swing/plaf/basic/BasicTextUI.java     7 Jul 2005 13:16:55 -0000       
1.26
@@ -208,7 +208,7 @@
 
   /**
    * Listens for changes on the underlying model and forwards notifications
-   * to the View.
+   * to the View. This also updates the caret position of the text component.
    *
    * TODO: Maybe this should somehow be handled through EditorKits
    */
@@ -236,6 +236,9 @@
       Dimension size = textComponent.getSize();
       rootView.insertUpdate(ev, new Rectangle(0, 0, size.width, size.height),
                             BasicTextUI.this);
+      int caretPos = textComponent.getCaretPosition();
+      if (caretPos >= ev.getOffset())
+        textComponent.setCaretPosition(caretPos + ev.getLength());
     }
 
     /**
@@ -248,6 +251,9 @@
       Dimension size = textComponent.getSize();
       rootView.removeUpdate(ev, new Rectangle(0, 0, size.width, size.height),
                             BasicTextUI.this);
+      int caretPos = textComponent.getCaretPosition();
+      if (caretPos >= ev.getOffset())
+        textComponent.setCaretPosition(caretPos - ev.getLength());
     }
   }
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]