This fixes the request-focus-on-click behaviour for two obvious types of components: buttons and text components.

2006-07-26  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicButtonListener.java
        (mousePressed): Request focus if appropriate.
        * javax/swing/text/DefaultCaret.java
        (mousePressed): Also handle the focus of the text component
        as specified. Don't consume events.

/Roman
Index: javax/swing/plaf/basic/BasicButtonListener.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicButtonListener.java,v
retrieving revision 1.15
diff -u -1 -2 -r1.15 BasicButtonListener.java
--- javax/swing/plaf/basic/BasicButtonListener.java	17 Jul 2006 08:36:48 -0000	1.15
+++ javax/swing/plaf/basic/BasicButtonListener.java	26 Jul 2006 19:22:20 -0000
@@ -171,29 +171,32 @@
 
   /**
    * Accept a mouse press event and arm the button.
    *
    * @param e The mouse press event to accept
    */
   public void mousePressed(MouseEvent e)
   {
     if (e.getSource() instanceof AbstractButton)
       {
         AbstractButton button = (AbstractButton) e.getSource();
         ButtonModel model = button.getModel();
-        if (e.getButton() == MouseEvent.BUTTON1)
+        if (SwingUtilities.isLeftMouseButton(e))
           {
             // It is important that these transitions happen in this order.
             model.setArmed(true);
             model.setPressed(true);
+
+            if (! button.isFocusOwner() && button.isRequestFocusEnabled())
+              button.requestFocus();
           }
       }
   }
 
   /**
    * Accept a mouse release event and set the button's 
    * "pressed" property to <code>true</code>, if the model
    * is armed. If the model is not armed, ignore the event.
    *
    * @param e The mouse release event to accept
    */
   public void mouseReleased(MouseEvent e)
Index: javax/swing/text/DefaultCaret.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultCaret.java,v
retrieving revision 1.43
diff -u -1 -2 -r1.43 DefaultCaret.java
--- javax/swing/text/DefaultCaret.java	25 Jun 2006 21:37:04 -0000	1.43
+++ javax/swing/text/DefaultCaret.java	26 Jul 2006 19:22:21 -0000
@@ -543,54 +543,67 @@
   }
 
   /**
    * If the button 1 is pressed, the caret position is updated to the
    * position of the mouse click and the text component requests the input
    * focus if it is enabled. If the SHIFT key is held down, the caret will
    * be moved, which might select the text between the old and new location.
    *
    * @param event the <code>MouseEvent</code> describing the press operation
    */
   public void mousePressed(MouseEvent event)
   {
-    int button = event.getButton();
     
     // The implementation assumes that consuming the event makes the AWT event
     // mechanism forget about this event instance and not transfer focus.
     // By observing how the RI reacts the following behavior has been
     // implemented (in regard to text components):
     // - a left-click moves the caret
     // - a left-click when shift is held down expands the selection
     // - a right-click or click with any additional mouse button
     //   on a text component is ignored
     // - a middle-click positions the caret and pastes the clipboard
     //   contents.
     // - a middle-click when shift is held down is ignored
-    
-    if (button == MouseEvent.BUTTON1)
-      if (event.isShiftDown())
-        moveCaret(event);
-      else
-        positionCaret(event);
-      else if(button == MouseEvent.BUTTON2)
-        if (event.isShiftDown())
-          event.consume();
+
+    if (SwingUtilities.isLeftMouseButton(event))
+      {
+        // Handle the caret.
+        if (event.isShiftDown() && getDot() != -1)
+          {
+            moveCaret(event);
+          }
         else
           {
             positionCaret(event);
-            
+          }
+
+        // Handle the focus.
+        if (textComponent != null && textComponent.isEnabled()
+            && textComponent.isRequestFocusEnabled())
+          {
+            textComponent.requestFocus();
+          }
+
+        // TODO: Handle double click for selecting words.
+      }
+    else if(event.getButton() == MouseEvent.BUTTON2)
+      {
+        // Special handling for X11-style pasting.
+        if (! event.isShiftDown())
+          {
+            positionCaret(event);
             textComponent.paste();
           }
-      else
-        event.consume();
+      }
   }
 
   /**
    * Indicates that a mouse button has been released on the text component.
    * Nothing is done here.
    *
    * @param event the <code>MouseEvent</code> describing the mouse operation
    */
   public void mouseReleased(MouseEvent event)
   {
     // Nothing to do here.
   }

Reply via email to