src/macosx/classes/sun/lwawt/LWWindowPeer.java

Print this page

        

@@ -733,10 +733,18 @@
                     mouseClickButtons = eventButtonMask;
                 } else {
                     mouseClickButtons |= eventButtonMask;
                 }
 
+                // The window should be focused on mouse click. If it gets activated by the native platform,
+                // this request will be no op. It will take effect when:
+                // 1. A simple not focused window is clicked.
+                // 2. An active but not focused owner frame/dialog is clicked.
+                // The mouse event then will trigger a focus request "in window" to the component, so the window
+                // should gain focus before.
+                requestWindowFocus(CausedFocusEvent.Cause.MOUSE_EVENT);
+
                 mouseDownTarget[targetIdx] = targetPeer;
             } else if (id == MouseEvent.MOUSE_DRAGGED) {
                 // Cocoa dragged event has the information about which mouse
                 // button is being dragged. Use it to determine the peer that
                 // should receive the dragged event.

@@ -824,24 +832,23 @@
      * Called by the delegate when a key is pressed.
      */
     public void dispatchKeyEvent(int id, long when, int modifiers,
                                  int keyCode, char keyChar, int keyLocation)
     {
-        LWComponentPeer focusOwner =
-            LWKeyboardFocusManagerPeer.getInstance(getAppContext()).
-                getFocusOwner();
-
-        // Null focus owner may receive key event when
-        // application hides the focused window upon ESC press
-        // (AWT transfers/clears the focus owner) and pending ESC release
-        // may come to already hidden window. This check eliminates NPE.
-        if (focusOwner != null) {
-            KeyEvent event =
-                new KeyEvent(focusOwner.getTarget(), id, when, modifiers,
-                             keyCode, keyChar, keyLocation);
-            focusOwner.postEvent(event);
+        LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance(getAppContext());
+
+        LWComponentPeer focusOwner = kfmPeer.getFocusOwner();
+        
+        if (focusOwner == null) {
+            focusOwner = kfmPeer.getFocusedWindow();
+            
+            if (focusOwner == null) {
+                focusOwner = this;
+            }
         }
+        focusOwner.postEvent(new KeyEvent(focusOwner.getTarget(), id, when, modifiers,
+                             keyCode, keyChar, keyLocation));
     }
 
 
     // ---- UTILITY METHODS ---- //
 

@@ -1173,11 +1180,11 @@
         }
 
         manager.setFocusedWindow(becomesFocused ? LWWindowPeer.this : null);
 
         int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS;
-        WindowEvent windowEvent = new WindowEvent(getTarget(), eventID, oppositeWindow);
+        WindowEvent windowEvent = new TimedWindowEvent(getTarget(), eventID, oppositeWindow, System.currentTimeMillis());
 
         // TODO: wrap in SequencedEvent
         postEvent(windowEvent);
     }