--- old/src/macosx/classes/sun/lwawt/macosx/CEmbeddedFrame.java 2014-07-23 13:04:04.000000000 +0400 +++ new/src/macosx/classes/sun/lwawt/macosx/CEmbeddedFrame.java 2014-07-23 13:04:03.000000000 +0400 @@ -38,7 +38,8 @@ private CPlatformResponder responder; private static final Object classLock = new Object(); - private static volatile CEmbeddedFrame focusedWindow; + private static volatile CEmbeddedFrame globalFocusedWindow; + private CEmbeddedFrame browserWindowFocusedApplet; private boolean parentWindowActive = true; public CEmbeddedFrame() { @@ -111,10 +112,10 @@ synchronized (classLock) { // In some cases an applet may not receive the focus lost event // from the parent window (see 8012330) - focusedWindow = (focused) ? this - : ((focusedWindow == this) ? null : focusedWindow); + globalFocusedWindow = (focused) ? this + : ((globalFocusedWindow == this) ? null : globalFocusedWindow); } - if (focusedWindow == this) { + if (globalFocusedWindow == this) { // see bug 8010925 // we can't put this to handleWindowFocusEvent because // it won't be invoced if focuse is moved to a html element @@ -145,9 +146,23 @@ // non-focused applet. This method can be called from different threads. public void handleWindowFocusEvent(boolean parentWindowActive) { this.parentWindowActive = parentWindowActive; + // If several applets are running in different browser's windows, it is necessary to + // detect the switching between the parent windows and update globalFocusedWindow accordingly. + synchronized (classLock) { + if (!parentWindowActive) { + this.browserWindowFocusedApplet = globalFocusedWindow; + } + if (parentWindowActive && globalFocusedWindow != this && isParentWindowChanged()) { + // It looks like we have switched to another browser window, let's restore focus to + // the previously focused applet in this window. If no applets were focused in the + // window, we will set focus to the first applet in the window. + globalFocusedWindow = (this.browserWindowFocusedApplet != null) ? this.browserWindowFocusedApplet + : this; + } + } // ignore focus "lost" native request as it may mistakenly // deactivate active window (see 8001161) - if (focusedWindow == this && parentWindowActive) { + if (globalFocusedWindow == this && parentWindowActive) { responder.handleWindowFocusEvent(parentWindowActive, null); } } @@ -155,4 +170,10 @@ public boolean isParentWindowActive() { return parentWindowActive; } + + private boolean isParentWindowChanged() { + // If globalFocusedWindow is located at inactive parent window or null, we have swithed to + // another window. + return globalFocusedWindow != null ? !globalFocusedWindow.isParentWindowActive() : true; + } } --- /dev/null 2014-07-23 13:04:05.000000000 +0400 +++ new/test/java/awt/Focus/8044614/TestApplet.java 2014-07-23 13:04:05.000000000 +0400 @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.applet.Applet; +import java.awt.TextField; + +public class TestApplet extends Applet { + TextField textField = null; + + public void init() { + textField = new TextField(25); + add(textField); + } +} --- /dev/null 2014-07-23 13:04:06.000000000 +0400 +++ new/test/java/awt/Focus/8044614/applet1.html 2014-07-23 13:04:06.000000000 +0400 @@ -0,0 +1,37 @@ + + + + +
+ +See the dialog box (usually in upper left corner) for instructions
+ + + + --- /dev/null 2014-07-23 13:04:08.000000000 +0400 +++ new/test/java/awt/Focus/8044614/bug8044614.java 2014-07-23 13:04:08.000000000 +0400 @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8044614 + * @summary Tests focus transfer between applets in different browser windows + * @author Dmitry Markov + * @library ../../regtesthelpers + * @build Sysout + * @run applet/manual=yesno bug8044614.html + */ + +import javax.swing.JApplet; + +import test.java.awt.regtesthelpers.Sysout; + +public class bug8044614 extends JApplet { + public void init() { + String[] instructions = { + "(1) Go to the test directory test/java/awt/Focus/8044614", + "(2) Compile source file: javac TestApplet.java", + "(3) Open the \"main.html\" file in the browser", + "(4) Click the \"Start First Applet\" link to open the first applet window", + "(5) Wait for the applet to start (press \"Run\" to any security alerts that appears)", + "(6) Enter \"Hello\" to the text field", + "(7) Click the \"Start Second Applet)\" link to open the second applet window", + "(8) Wait for the applet to start (press \"Run\" to any security alerts that appears)", + "(9) Enter \"World\" to the text field", + "(10) Go back to the first applet and make sure you can enter some text to the text field" + }; + + Sysout.createDialogWithInstructions(instructions); + } +} + --- /dev/null 2014-07-23 13:04:09.000000000 +0400 +++ new/test/java/awt/Focus/8044614/main.html 2014-07-23 13:04:09.000000000 +0400 @@ -0,0 +1,46 @@ + + + + + + +Start First Applet | +
Start Second Applet | +