Hi,
I am starting to grok that swing stuff. :)

I added TextAction implementations for selecting the current line (backward and
forward), the whole text from the current position (backward & forward) and
corrected the behavior of single character deletions when the text has a 
selection.

Or textfield and textareas now do what you would expect from them when pressing
shift-home, shift-end, shift-ctrl-home, shift-ctrl-end and del/backspace when
your text has a selection.

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

        * javax/swing/text/DefaultEditorToolkit.java: Changed behavior
        of actions "delete-next" and "delete-previous", added new TextAction
        implementations for "selection-begin", "selection-begin-line",
        "selection-end" and "selection-end-line".

Please comment.

cya
Robert
Index: javax/swing/text/DefaultEditorKit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultEditorKit.java,v
retrieving revision 1.27
diff -u -r1.27 DefaultEditorKit.java
--- javax/swing/text/DefaultEditorKit.java	21 Dec 2005 21:59:30 -0000	1.27
+++ javax/swing/text/DefaultEditorKit.java	7 Feb 2006 15:04:10 -0000
@@ -756,11 +756,15 @@
           {
             try
               {
-                int pos = t.getCaret().getDot();
-                if (pos < t.getDocument().getEndPosition().getOffset())
-                  {
-                    t.getDocument().remove(t.getCaret().getDot(), 1);
-                  }
+                int pos = t.getSelectionStart();
+                int len = t.getSelectionEnd() - pos;
+                
+                if (len > 0)
+                    t.getDocument().remove(pos, len);
+                else if (pos < t.getDocument().getLength())
+                    t.getDocument().remove(pos, 1);
+
+                t.setCaretPosition(pos);
               }
             catch (BadLocationException e)
               {
@@ -778,12 +782,16 @@
           {
             try
               {
-                int pos = t.getCaret().getDot();
-                if (pos > t.getDocument().getStartPosition().getOffset())
+                int pos = t.getSelectionStart();
+                int len = t.getSelectionEnd() - pos;
+                
+                if (len > 0)
                   {
-                    t.getDocument().remove(pos - 1, 1);
-                    t.getCaret().setDot(pos - 1);
+                    t.getDocument().remove(pos, len);
+                    t.setCaretPosition(pos);
                   }
+                else if (pos > 0)
+                    t.getDocument().remove(pos - 1, 1);
               }
             catch (BadLocationException e)
               {
@@ -840,6 +848,74 @@
           }
       }
     },
+    new TextAction(selectionBeginLineAction)
+    {
+      public void actionPerformed(ActionEvent event)
+      {
+        JTextComponent t = getTextComponent(event);
+        
+        try
+        {
+          // TODO: There is a more efficent solution, but
+          // viewToModel doesn't work properly.
+          Point p = t.modelToView(t.getCaret().getDot()).getLocation();
+          
+          int cur = t.getCaretPosition();
+          int y = p.y;
+          
+          while (y == p.y && cur > 0)
+            y = t.modelToView(--cur).getLocation().y;
+          if (cur != 0)
+            cur++;
+          
+          t.getCaret().moveDot(cur);
+        }
+        catch (BadLocationException ble)
+        {
+          // Do nothing here.
+        }
+      }
+    },
+    new TextAction(selectionEndLineAction)
+    {
+      public void actionPerformed(ActionEvent event)
+      {
+        JTextComponent t = getTextComponent(event);
+       try
+       {
+         Point p = t.modelToView(t.getCaret().getDot()).getLocation();
+         int cur = t.getCaretPosition();
+         int y = p.y;
+         int length = t.getDocument().getLength();
+         while (y == p.y && cur < length)
+           y = t.modelToView(++cur).getLocation().y;
+         if (cur != length)
+           cur--;
+
+         t.moveCaretPosition(cur);
+       }
+       catch (BadLocationException ble)
+       {
+         // Nothing to do here
+       }
+      }
+    },
+    new TextAction(selectionEndAction)
+    {
+      public void actionPerformed(ActionEvent event)
+      {
+        JTextComponent t = getTextComponent(event);
+        t.moveCaretPosition(t.getDocument().getLength());
+      }
+    },
+    new TextAction(selectionBeginAction)
+    {
+      public void actionPerformed(ActionEvent event)
+      {
+        JTextComponent t = getTextComponent(event);
+         t.moveCaretPosition(0);
+      }
+    }
   };
 
   /**

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to