Installs a WindowFocusListener on the given JComponent 's parent Window . - Java Swing

Java examples for Swing:Window

Description

Installs a WindowFocusListener on the given JComponent 's parent Window .

Demo Code

/*/* w  w  w . j ava2 s .  c  o m*/
 * Copyright (c) 2009 Kathryn Huxtable and Kenneth Orr.
 *
 * This file is part of the Aqvavit Pluggable Look and Feel.
 *
 * Aqvavit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.

 * Aqvavit 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Aqvavit.  If not, see
 *     <http://www.gnu.org/licenses/>.
 * 
 * $Id$
 */
//package com.java2s;

import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.lang.ref.WeakReference;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class Main {
    /**
     * Installs a {@link WindowFocusListener} on the given {@link JComponent}'s parent {@link Window}. If the
     * {@code JComponent} doesn't yet have a parent, then the listener will be installed when the component is added to
     * a container.
     *
     * @param component     the component who's parent frame to listen to focus changes on.
     * @param focusListener the {@code WindowFocusListener} to notify when focus changes.
     */
    public static void installWeakWindowFocusListener(JComponent component,
            WindowFocusListener focusListener) {
        WindowListener weakFocusListener = createWeakWindowFocusListener(focusListener);
        AncestorListener ancestorListener = createAncestorListener(
                component, weakFocusListener);
        component.addAncestorListener(ancestorListener);
    }

    private static WindowListener createWeakWindowFocusListener(
            WindowFocusListener windowFocusListener) {
        final WeakReference<WindowFocusListener> weakReference = new WeakReference<WindowFocusListener>(
                windowFocusListener);
        return new WindowAdapter() {
            public void windowActivated(WindowEvent e) {
                // TODO if the WeakReference's object is null, remove the WeakReference as a FocusListener.
                if (weakReference.get() != null) {
                    weakReference.get().windowGainedFocus(e);
                }
            }

            public void windowDeactivated(WindowEvent e) {
                if (weakReference.get() != null) {
                    weakReference.get().windowLostFocus(e);
                }
            }
        };
    }

    private static AncestorListener createAncestorListener(
            JComponent component, final WindowListener windowListener) {
        final WeakReference<JComponent> weakReference = new WeakReference<JComponent>(
                component);
        return new AncestorListener() {
            public void ancestorAdded(AncestorEvent event) {
                // TODO if the WeakReference's object is null, remove the WeakReference as an AncestorListener.
                Window window = weakReference.get() == null ? null
                        : SwingUtilities.getWindowAncestor(weakReference
                                .get());
                if (window != null) {
                    window.removeWindowListener(windowListener);
                    window.addWindowListener(windowListener);
                }
            }

            public void ancestorRemoved(AncestorEvent event) {
                Window window = weakReference.get() == null ? null
                        : SwingUtilities.getWindowAncestor(weakReference
                                .get());
                if (window != null) {
                    window.removeWindowListener(windowListener);
                }
            }

            public void ancestorMoved(AncestorEvent event) {
                // no implementation.
            }
        };
    }
}

Related Tutorials