Following up on my question yesterday, here's a small fix for the
KeyboardFocusManagerPeer creation. It doesn't affect functionality in
OpenJDK, but it allows outside code to provide a
KeyboardFocusManagerPeer by implementing the appropriate interface. I
also implemented a dummy peer which does nothing at all. This avoids a
NPE for toolkits that don't implement KeyboardFocusManagerPeerProvider
but doesn't nothing useful for itself.

I implemented this as part of my work at the aicas GmbH, so this code is
copyrighted by aicas. aicas already signed the SCA.

Comments? I hope this can go into the tree.

/Roman

-- 
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com   * Tel: +49-721-663 968-0
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
Index: j2se/src/share/classes/java/awt/KeyboardFocusManager.java
===================================================================
--- j2se/src/share/classes/java/awt/KeyboardFocusManager.java	(revision 237)
+++ j2se/src/share/classes/java/awt/KeyboardFocusManager.java	(working copy)
@@ -45,9 +45,12 @@
 import java.util.StringTokenizer;
 import java.util.WeakHashMap;
 import java.util.logging.*;
+
 import sun.awt.AppContext;
 import sun.awt.DebugHelper;
+import sun.awt.DummyKeyboardFocusManagerPeer;
 import sun.awt.HeadlessToolkit;
+import sun.awt.KeyboardFocusManagerPeerProvider;
 import sun.awt.SunToolkit;
 import sun.awt.CausedFocusEvent;
 
@@ -413,12 +416,14 @@
     }
 
     private void initPeer() {
-        if (Toolkit.getDefaultToolkit() instanceof HeadlessToolkit){
-            peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createKeyboardFocusManagerPeer(this);
+        Toolkit tk = Toolkit.getDefaultToolkit();
+        if (tk instanceof KeyboardFocusManagerPeerProvider) {
+            KeyboardFocusManagerPeerProvider kfmp =
+                    (KeyboardFocusManagerPeerProvider) tk;
+            peer = kfmp.createKeyboardFocusManagerPeer(this);
+        } else {
+            peer = new DummyKeyboardFocusManagerPeer();
         }
-        if (Toolkit.getDefaultToolkit() instanceof SunToolkit){        
-            peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createKeyboardFocusManagerPeer(this);                  
-        }
     }
 
     /**
Index: j2se/src/share/classes/sun/awt/SunToolkit.java
===================================================================
--- j2se/src/share/classes/sun/awt/SunToolkit.java	(revision 237)
+++ j2se/src/share/classes/sun/awt/SunToolkit.java	(working copy)
@@ -64,7 +64,7 @@
 
 public abstract class SunToolkit extends Toolkit
     implements WindowClosingSupport, WindowClosingListener,
-    ComponentFactory, InputMethodSupport {
+    ComponentFactory, InputMethodSupport, KeyboardFocusManagerPeerProvider {
 
     private static final Logger log = Logger.getLogger("sun.awt.SunToolkit");
 
Index: j2se/src/share/classes/sun/awt/HeadlessToolkit.java
===================================================================
--- j2se/src/share/classes/sun/awt/HeadlessToolkit.java	(revision 237)
+++ j2se/src/share/classes/sun/awt/HeadlessToolkit.java	(working copy)
@@ -44,7 +44,7 @@
 import sun.awt.image.ImageRepresentation;
 
 public class HeadlessToolkit extends Toolkit
-    implements ComponentFactory {
+    implements ComponentFactory, KeyboardFocusManagerPeerProvider {
 
     private Toolkit tk;
     private ComponentFactory componentFactory;
Index: j2se/src/share/classes/sun/awt/KeyboardFocusManagerPeerProvider.java
===================================================================
--- j2se/src/share/classes/sun/awt/KeyboardFocusManagerPeerProvider.java	(revision 0)
+++ j2se/src/share/classes/sun/awt/KeyboardFocusManagerPeerProvider.java	(revision 0)
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package sun.awt;
+
+import java.awt.KeyboardFocusManager;
+import java.awt.peer.KeyboardFocusManagerPeer;
+
+/**
+ * Provides a [EMAIL PROTECTED] KeyboardFocusManagerPeer}. This has to be implemented by
+ * [EMAIL PROTECTED] java.awt.Toolkit}s that provide a KeyboardFocusManagerPeer.
+ * The method [EMAIL PROTECTED] KeyboardFocusManager#initPeer()} checks the current
+ * toolkit if it implements this interface. If not, a dummy
+ * ([EMAIL PROTECTED] DummyKeyboardFocusManagerPeer} is used, which does nothing.
+ *
+ * @author Roman Kennke ([EMAIL PROTECTED])
+ */
+public interface KeyboardFocusManagerPeerProvider {
+
+    /**
+     * Creates a KeyboardFocusManagerPeer for the specified
+     * KeyboardFocusManager.
+     */
+    KeyboardFocusManagerPeer
+            createKeyboardFocusManagerPeer(KeyboardFocusManager m);
+}
Index: j2se/src/share/classes/sun/awt/DummyKeyboardFocusManagerPeer.java
===================================================================
--- j2se/src/share/classes/sun/awt/DummyKeyboardFocusManagerPeer.java	(revision 0)
+++ j2se/src/share/classes/sun/awt/DummyKeyboardFocusManagerPeer.java	(revision 0)
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package sun.awt;
+
+import java.awt.Component;
+import java.awt.Window;
+import java.awt.peer.KeyboardFocusManagerPeer;
+
+/**
+ * A dummy implementation for [EMAIL PROTECTED] KeyboardFocusManagerPeer} that is used
+ * when the current toolkit can't provide a KeyboardFocusManagerPeer. This
+ * is basically a no-op implementation.
+ *
+ * @author Roman Kennke ([EMAIL PROTECTED])
+ */
+public class DummyKeyboardFocusManagerPeer
+    implements KeyboardFocusManagerPeer {
+
+    public DummyKeyboardFocusManagerPeer() {
+    }
+
+    public void setCurrentFocusedWindow(Window win) {
+        // We do nothing here.
+    }
+
+    public Window getCurrentFocusedWindow() {
+        // We do nothing here.
+        return null;
+    }
+
+    public void setCurrentFocusOwner(Component comp) {
+        // We do nothing here.
+    }
+
+    public Component getCurrentFocusOwner() {
+        // We do nothing here.
+        return null;
+    }
+
+    public void clearGlobalFocusOwner(Window activeWindow) {
+        // We do nothing here.
+    }
+
+}

Reply via email to