<AWT Dev> RFC: KeyboardFocusManager patch
Anton V. Tarasov
Anton.Tarasov at Sun.COM
Thu Jun 21 08:53:01 PDT 2007
Hi Roman,
Seems like I'll be responsible for accompanying this patch
on the way to the JDK =)
My comments concern the DummyKFMPeer (KFM is an acronym
for KeyboardFocusManager commonly used in talks here).
I thought that it's not good to leave it blank and that we're
better to concretize the case when a custom Toolkit doesn't
implement KFMPeerProvider interface. Let's consider the following
two approaches:
1. We don't create the DummyKFMPeer at all, instead
we throw some proper exception if the Toolkit doesn't
implement the KFMPeerProvider (i.e. we force it to implement).
For instance, we just throw ClassCastException.
2. We create the DummyKFMPeer and make its every method throw
OperationNotSupportedException.
The both approaches serve to notify the implementator of the Toolkit
that it should probably have to provide some logic for the KFMPeer.
In case he "forgot" to do it but addressed the peer he would implement,
if he didn't need it and he didn't address the peer - nothing happens,
when he didn't need it but still addressed he'd make it silent.
The second approach is less strict and is rather more preferable.
What are your thoughts?
Thanks,
Anton.
Roman Kennke wrote:
> 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
>
>
>
> ------------------------------------------------------------------------
>
> 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 {@link KeyboardFocusManagerPeer}. This has to be implemented by
> + * {@link java.awt.Toolkit}s that provide a KeyboardFocusManagerPeer.
> + * The method {@link KeyboardFocusManager#initPeer()} checks the current
> + * toolkit if it implements this interface. If not, a dummy
> + * ({@link DummyKeyboardFocusManagerPeer} is used, which does nothing.
> + *
> + * @author Roman Kennke (roman.kennke at aicas.com)
> + */
> +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 {@link KeyboardFocusManagerPeer} that is used
> + * when the current toolkit can't provide a KeyboardFocusManagerPeer. This
> + * is basically a no-op implementation.
> + *
> + * @author Roman Kennke (roman.kennke at aicas.com)
> + */
> +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.
> + }
> +
> +}
More information about the awt-dev
mailing list