8024170: [SwingNode] Implement cursor change
authorpchelko
Fri, 13 Sep 2013 11:58:39 +0400
changeset 20127 a3a34675d847
parent 20126 e09648d4170c
child 20128 25a1c15eea88
8024170: [SwingNode] Implement cursor change Reviewed-by: anthony, ant
jdk/src/macosx/classes/sun/lwawt/LWLightweightFramePeer.java
jdk/src/share/classes/sun/swing/JLightweightFrame.java
jdk/src/share/classes/sun/swing/LightweightContent.java
jdk/src/share/classes/sun/swing/SwingAccessor.java
jdk/src/solaris/classes/sun/awt/X11/XLightweightFramePeer.java
jdk/src/windows/classes/sun/awt/windows/WComponentPeer.java
jdk/src/windows/classes/sun/awt/windows/WLightweightFramePeer.java
--- a/jdk/src/macosx/classes/sun/lwawt/LWLightweightFramePeer.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/LWLightweightFramePeer.java	Fri Sep 13 11:58:39 2013 +0400
@@ -34,6 +34,8 @@
 
 import sun.awt.CausedFocusEvent;
 import sun.awt.LightweightFrame;
+import sun.swing.JLightweightFrame;
+import sun.swing.SwingAccessor;
 
 public class LWLightweightFramePeer extends LWWindowPeer {
 
@@ -91,11 +93,6 @@
     }
 
     @Override
-    public void updateCursorImmediately() {
-        // TODO: tries to switch to the awt/fx toolkit thread and causes a deadlock on macosx
-    }
-
-    @Override
     public void addDropTarget(DropTarget dt) {
     }
 
@@ -112,4 +109,9 @@
     public void ungrab() {
         getLwTarget().ungrabFocus();
     }
+
+    @Override
+    public void updateCursorImmediately() {
+        SwingAccessor.getJLightweightFrameAccessor().updateCursor((JLightweightFrame)getLwTarget());
+    }
 }
--- a/jdk/src/share/classes/sun/swing/JLightweightFrame.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/share/classes/sun/swing/JLightweightFrame.java	Fri Sep 13 11:58:39 2013 +0400
@@ -33,8 +33,9 @@
 import java.awt.EventQueue;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
+import java.awt.MouseInfo;
+import java.awt.Point;
 import java.awt.Rectangle;
-import java.awt.event.ComponentListener;
 import java.awt.event.ContainerEvent;
 import java.awt.event.ContainerListener;
 import java.awt.image.BufferedImage;
@@ -48,6 +49,7 @@
 import javax.swing.JRootPane;
 import javax.swing.LayoutFocusTraversalPolicy;
 import javax.swing.RootPaneContainer;
+import javax.swing.SwingUtilities;
 
 import sun.awt.LightweightFrame;
 import sun.security.action.GetPropertyAction;
@@ -88,6 +90,15 @@
 
     private PropertyChangeListener layoutSizeListener;
 
+    static {
+        SwingAccessor.setJLightweightFrameAccessor(new SwingAccessor.JLightweightFrameAccessor() {
+            @Override
+            public void updateCursor(JLightweightFrame frame) {
+                frame.updateClientCursor();
+            }
+        });
+    }
+
     /**
      * Constructs a new, initially invisible {@code JLightweightFrame}
      * instance.
@@ -358,4 +369,21 @@
     public Component getGlassPane() {
         return getRootPane().getGlassPane();
     }
+
+
+    /*
+     * Notifies client toolkit that it should change a cursor.
+     *
+     * Called from the peer via SwingAccessor, because the
+     * Component.updateCursorImmediately method is final
+     * and could not be overridden.
+     */
+    private void updateClientCursor() {
+        Point p = MouseInfo.getPointerInfo().getLocation();
+        SwingUtilities.convertPointFromScreen(p, this);
+        Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
+        if (target != null) {
+            content.setCursor(target.getCursor());
+        }
+    }
 }
--- a/jdk/src/share/classes/sun/swing/LightweightContent.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/share/classes/sun/swing/LightweightContent.java	Fri Sep 13 11:58:39 2013 +0400
@@ -26,6 +26,7 @@
 package sun.swing;
 
 import javax.swing.JComponent;
+import java.awt.Cursor;
 
 /**
  * The interface by means of which the {@link JLightweightFrame} class
@@ -179,4 +180,11 @@
      * application that the content minimum size has changed.
      */
     public void minimumSizeChanged(int width, int height);
+
+    /**
+     * {@code JLightweightFrame} calls this method to notify the client
+     * application that in needs to set a cursor
+     * @param cursor a cursor to set
+     */
+    default public void setCursor(Cursor cursor) { }
 }
--- a/jdk/src/share/classes/sun/swing/SwingAccessor.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingAccessor.java	Fri Sep 13 11:58:39 2013 +0400
@@ -72,6 +72,16 @@
     }
 
     /**
+     * An accessor for the JLightweightFrame class.
+     */
+    public interface JLightweightFrameAccessor {
+        /**
+         * Notifies the JLightweight frame that it needs to update a cursor
+         */
+        void updateCursor(JLightweightFrame frame);
+    }
+
+    /**
      * The javax.swing.text.JTextComponent class accessor object.
      */
     private static JTextComponentAccessor jtextComponentAccessor;
@@ -93,4 +103,23 @@
 
         return jtextComponentAccessor;
     }
+
+    /**
+     * The JLightweightFrame class accessor object
+     */
+    private static JLightweightFrameAccessor jLightweightFrameAccessor;
+
+    /**
+     * Set an accessor object for the JLightweightFrame class.
+     */
+    public static void setJLightweightFrameAccessor(JLightweightFrameAccessor accessor) {
+        jLightweightFrameAccessor = accessor;
+    }
+
+    /**
+     * Retrieve the accessor object for the JLightweightFrame class
+     */
+    public static JLightweightFrameAccessor getJLightweightFrameAccessor() {
+        return jLightweightFrameAccessor;
+    }
 }
--- a/jdk/src/solaris/classes/sun/awt/X11/XLightweightFramePeer.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/solaris/classes/sun/awt/X11/XLightweightFramePeer.java	Fri Sep 13 11:58:39 2013 +0400
@@ -28,6 +28,8 @@
 import java.awt.Graphics;
 
 import sun.awt.LightweightFrame;
+import sun.swing.JLightweightFrame;
+import sun.swing.SwingAccessor;
 
 public class XLightweightFramePeer extends XFramePeer {
 
@@ -62,4 +64,9 @@
             getLwTarget().ungrabFocus();
         }
     }
+
+    @Override
+    public void updateCursorImmediately() {
+        SwingAccessor.getJLightweightFrameAccessor().updateCursor((JLightweightFrame)getLwTarget());
+    }
 }
--- a/jdk/src/windows/classes/sun/awt/windows/WComponentPeer.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/windows/classes/sun/awt/windows/WComponentPeer.java	Fri Sep 13 11:58:39 2013 +0400
@@ -656,7 +656,7 @@
         _setFont(f);
     }
     public synchronized native void _setFont(Font f);
-    public final void updateCursorImmediately() {
+    public void updateCursorImmediately() {
         WGlobalCursorManager.getCursorManager().updateCursorImmediately();
     }
 
--- a/jdk/src/windows/classes/sun/awt/windows/WLightweightFramePeer.java	Thu Sep 12 18:44:14 2013 +0400
+++ b/jdk/src/windows/classes/sun/awt/windows/WLightweightFramePeer.java	Fri Sep 13 11:58:39 2013 +0400
@@ -31,6 +31,8 @@
 import java.awt.event.MouseEvent;
 
 import sun.awt.LightweightFrame;
+import sun.swing.JLightweightFrame;
+import sun.swing.SwingAccessor;
 
 public class WLightweightFramePeer extends WFramePeer {
 
@@ -83,4 +85,9 @@
     public void ungrab() {
         getLwTarget().ungrabFocus();
     }
+
+    @Override
+    public void updateCursorImmediately() {
+        SwingAccessor.getJLightweightFrameAccessor().updateCursor((JLightweightFrame)getLwTarget());
+    }
 }