Hi,
as described in PR 26737[0] the focus change behavior for text components is a
bit different than for other swing components.

I implemented that behavior by adjusting DefaultCaret a bit. However the patch
relies on a change in Component.dispatchEventImpl() too, which someone with more
AWT/Swing knowledge should review first.

An important change is the removal of event.consume() from
Component.processMouseEvent. When testing the RI for that behavior I could not
observe that it consumes the event. I decided to take the description of
MouseEvent.consume() for real and changed Component.dispatchEventImpl() in a way
that it does not change the focus of lightweight components when the MouseEvent
is consumed.

Additionally I had to make sure that the specific events (processMouseEvent and
friends) are really handled before the other stuff. This is why that code has
moved some lines up.

Ah yes. The handling of middle-clicks in DefaultCaret will provide X11-style
copy-n-paste behavior with Free Swing text components. It needs a small patch to
BasicTextUI which I already completed but is for a different PR.

Please comment.

Here is the ChangeLog:

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

        * java/awt/Component.java:
        (processMouseEvent): Remove call to consume event.
        (dispatchEventImpl): Handle specific events first, do focus request
        only when mouse event was not yet consumed.
        * javax/swing/text/DefaultCaret.java:
        (mousePressed): Rewritten.

cya
Robert

[0] - http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26737
Index: java/awt/Component.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Component.java,v
retrieving revision 1.107
diff -u -r1.107 Component.java
--- java/awt/Component.java	18 Mar 2006 20:14:55 -0000	1.107
+++ java/awt/Component.java	21 Mar 2006 18:21:26 -0000
@@ -3101,7 +3101,6 @@
           mouseListener.mouseReleased(e);
         break;
       }
-      e.consume();
   }
 
   /**
@@ -4934,6 +4933,10 @@
     
     if (eventTypeEnabled (e.id))
       {
+        if (e.id != PaintEvent.PAINT && e.id != PaintEvent.UPDATE
+            && !ignoreFocus)
+          processEvent(e);
+        
         // the trick we use to communicate between dispatch and redispatch
         // is to have KeyboardFocusManager.redispatch synchronize on the
         // object itself. we then do not redispatch to KeyboardFocusManager
@@ -4954,14 +4957,11 @@
                     .dispatchEvent(e))
                     return;
               case MouseEvent.MOUSE_PRESSED:
-                if (isLightweight())
-                  requestFocus();
+                if (isLightweight() && !e.isConsumed())
+                    requestFocus();
                 break;
               }
           }
-        if (e.id != PaintEvent.PAINT && e.id != PaintEvent.UPDATE
-            && !ignoreFocus)
-          processEvent(e);
       }
 
     if (peer != null)
Index: javax/swing/text/DefaultCaret.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultCaret.java,v
retrieving revision 1.35
diff -u -r1.35 DefaultCaret.java
--- javax/swing/text/DefaultCaret.java	21 Mar 2006 18:02:46 -0000	1.35
+++ javax/swing/text/DefaultCaret.java	21 Mar 2006 18:21:26 -0000
@@ -471,10 +471,35 @@
    */
   public void mousePressed(MouseEvent event)
   {
-    if (event.isShiftDown())
-      moveCaret(event);
-    else
-      positionCaret(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 additionaly 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();
+        else
+          {
+            positionCaret(event);
+            textComponent.paste();
+          }
+      else
+        event.consume();
   }
 
   /**

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to