7160293: [macosx] FileDialog appears on secondary display
authordcherepanov
Tue, 22 May 2012 12:35:55 +0400
changeset 12816 b4cabffcb0de
parent 12666 0799075de9d6
child 12817 5b0f3d1b8cfe
7160293: [macosx] FileDialog appears on secondary display Reviewed-by: art, bae
jdk/src/macosx/classes/sun/awt/CGraphicsEnvironment.java
jdk/src/macosx/classes/sun/lwawt/LWWindowPeer.java
jdk/src/macosx/classes/sun/lwawt/PlatformWindow.java
jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformEmbeddedFrame.java
jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java
jdk/src/macosx/native/sun/awt/AWTWindow.m
--- a/jdk/src/macosx/classes/sun/awt/CGraphicsEnvironment.java	Tue May 22 01:12:16 2012 +0400
+++ b/jdk/src/macosx/classes/sun/awt/CGraphicsEnvironment.java	Tue May 22 12:35:55 2012 +0400
@@ -181,6 +181,10 @@
         return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);
     }
 
+    public synchronized GraphicsDevice getScreenDevice(int displayID) {
+        return devices.get(displayID);
+    }
+
     @Override
     protected synchronized int getNumScreens() {
         return devices.size();
--- a/jdk/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Tue May 22 01:12:16 2012 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Tue May 22 12:35:55 2012 +0400
@@ -68,7 +68,7 @@
 
     private Insets insets = new Insets(0, 0, 0, 0);
 
-    private int screenOn = -1;
+    private GraphicsDevice graphicsDevice;
     private GraphicsConfiguration graphicsConfig;
 
     private SurfaceData surfaceData;
@@ -868,17 +868,6 @@
         return 0;
     }
 
-    private static GraphicsConfiguration getScreenGraphicsConfig(int screen) {
-        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
-        GraphicsDevice[] gds = ge.getScreenDevices();
-        if (screen >= gds.length) {
-            // This could happen during device addition/removal. Use
-            // the default screen device in this case
-            return ge.getDefaultScreenDevice().getDefaultConfiguration();
-        }
-        return gds[screen].getDefaultConfiguration();
-    }
-
     /*
      * This method is called when window's graphics config is changed from
      * the app code (e.g. when the window is made non-opaque) or when
@@ -893,7 +882,7 @@
             }
             // If window's graphics config is changed from the app code, the
             // config correspond to the same device as before; when the window
-            // is moved by user, screenOn is updated in checkIfOnNewScreen().
+            // is moved by user, graphicsDevice is updated in checkIfOnNewScreen().
             // In either case, there's nothing to do with screenOn here
             graphicsConfig = gc;
         }
@@ -902,16 +891,17 @@
     }
 
     private void checkIfOnNewScreen() {
-        int windowScreen = platformWindow.getScreenImOn();
+        GraphicsDevice newGraphicsDevice = platformWindow.getGraphicsDevice();
         synchronized (getStateLock()) {
-            if (windowScreen == screenOn) {
+            if (graphicsDevice == newGraphicsDevice) {
                 return;
             }
-            screenOn = windowScreen;
+            graphicsDevice = newGraphicsDevice;
         }
 
         // TODO: DisplayChangedListener stuff
-        final GraphicsConfiguration newGC = getScreenGraphicsConfig(windowScreen);
+        final GraphicsConfiguration newGC = newGraphicsDevice.getDefaultConfiguration();
+
         if (!setGraphicsConfig(newGC)) return;
 
         SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
--- a/jdk/src/macosx/classes/sun/lwawt/PlatformWindow.java	Tue May 22 01:12:16 2012 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/PlatformWindow.java	Tue May 22 12:35:55 2012 +0400
@@ -67,9 +67,9 @@
     public void setBounds(int x, int y, int w, int h);
 
     /*
-     * Returns the screen number where the window is.
+     * Returns the graphics device where the window is.
      */
-    public int getScreenImOn();
+    public GraphicsDevice getGraphicsDevice();
 
     /*
      * Returns the location of the window.
--- a/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformEmbeddedFrame.java	Tue May 22 01:12:16 2012 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformEmbeddedFrame.java	Tue May 22 12:35:55 2012 +0400
@@ -86,11 +86,10 @@
     }
 
     @Override
-    public int getScreenImOn() {
+    public GraphicsDevice getGraphicsDevice() {
         // REMIND: return the main screen for the initial implementation
-        CGraphicsConfig gc = (CGraphicsConfig)peer.getGraphicsConfiguration();
-        CGraphicsDevice device = gc.getDevice();
-        return device.getCoreGraphicsScreen();
+        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
+        return ge.getDefaultScreenDevice();
     }
 
     @Override
--- a/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Tue May 22 01:12:16 2012 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Tue May 22 12:35:55 2012 +0400
@@ -64,7 +64,7 @@
     private static native void nativeSetEnabled(long nsWindowPtr, boolean isEnabled);
     private static native void nativeSynthesizeMouseEnteredExitedEvents(long nsWindowPtr);
 
-    private static native int nativeGetScreenNSWindowIsOn_AppKitThread(long nsWindowPtr);
+    private static native int nativeGetNSWindowDisplayID_AppKitThread(long nsWindowPtr);
 
     // Loger to report issues happened during execution but that do not affect functionality
     private static final PlatformLogger logger = PlatformLogger.getLogger("sun.lwawt.macosx.CPlatformWindow");
@@ -452,13 +452,18 @@
         return new Point(nativeBounds.x, nativeBounds.y);
     }
 
-    @Override // PlatformWindow
-    public int getScreenImOn() {
-    // REMIND: we could also acquire screenID from the
-    // graphicsConfig.getDevice().getCoreGraphicsScreen()
-    // which might look a bit less natural but don't
-    // require new native accessor.
-        return nativeGetScreenNSWindowIsOn_AppKitThread(getNSWindowPtr());
+    @Override
+    public GraphicsDevice getGraphicsDevice() {
+        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
+        CGraphicsEnvironment cge = (CGraphicsEnvironment)ge;
+        int displayID = nativeGetNSWindowDisplayID_AppKitThread(getNSWindowPtr());
+        GraphicsDevice gd = cge.getScreenDevice(displayID);
+        if (gd == null) {
+            // this could possibly happen during device removal
+            // use the default screen device in this case
+            gd = ge.getDefaultScreenDevice();
+        }
+        return gd;
     }
 
     @Override // PlatformWindow
--- a/jdk/src/macosx/native/sun/awt/AWTWindow.m	Tue May 22 01:12:16 2012 +0400
+++ b/jdk/src/macosx/native/sun/awt/AWTWindow.m	Tue May 22 12:35:55 2012 +0400
@@ -1080,39 +1080,29 @@
 
 /*
  * Class:     sun_lwawt_macosx_CPlatformWindow
- * Method:    nativeGetScreenNSWindowIsOn_AppKitThread
+ * Method:    nativeGetDisplayID_AppKitThread
  * Signature: (J)I
  */
-JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetScreenNSWindowIsOn_1AppKitThread
+JNIEXPORT jint JNICALL
+Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowDisplayID_1AppKitThread
 (JNIEnv *env, jclass clazz, jlong windowPtr)
 {
-    jint index = -1;
+    jint ret; // CGDirectDisplayID
 
 JNF_COCOA_ENTER(env);
 AWT_ASSERT_APPKIT_THREAD;
 
-    NSWindow *nsWindow = OBJC(windowPtr);
-    NSScreen* screen = [nsWindow screen];
-
-    //+++gdb NOTE: This is using a linear search of the screens. If it should
-    //  prove to be a bottleneck, this can definitely be improved. However,
-    //  many screens should prove to be the exception, rather than the rule.
-    NSArray* screens = [NSScreen screens];
-    NSUInteger i;
-    for (i = 0; i < [screens count]; i++)
-    {
-        if ([[screens objectAtIndex:i] isEqualTo:screen])
-        {
-            index = i;
-            break;
-        }
-    }
+    NSWindow *window = OBJC(windowPtr);
+    NSScreen *screen = [window screen];
+    NSDictionary *deviceDescription = [screen deviceDescription];
+    NSNumber *displayID = [deviceDescription objectForKey:@"NSScreenNumber"];
+    ret = (jint)[displayID intValue];
 
 JNF_COCOA_EXIT(env);
-    return 1;
+
+    return ret;
 }
 
-
 /*
  * Class:     sun_lwawt_macosx_CPlatformWindow
  * Method:    _toggleFullScreenMode