All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.managers.focus.FocusManager Maven / Gradle / Ivy

Go to download

WebLaf is a Java Swing Look and Feel and extended components library for cross-platform applications

There is a newer version: 2.2.1
Show newest version
/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see .
 */

package com.alee.managers.focus;

import com.alee.global.GlobalConstants;
import com.alee.managers.log.Log;
import com.alee.utils.CollectionUtils;

import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.List;

/**
 * This manager allows you to track certain component their children focus state by adding your custom FocusTracker or global focus
 * listeners to track component focus state.
 *
 * @author Mikle Garin
 */

public class FocusManager
{
    /**
     * Tracker list and cache lock.
     */
    protected static final Object trackersLock = new Object ();

    /**
     * Focus trackers list.
     */
    protected static final Map> trackers = new WeakHashMap> ();

    /**
     * Global focus listeners lock.
     */
    protected static final Object listenersLock = new Object ();

    /**
     * Global focus listeners list.
     */
    protected static final List globalFocusListeners = new ArrayList ( 2 );

    /**
     * Reference to previously focused component.
     */
    protected static WeakReference oldFocusOwner;

    /**
     * Reference to currently focused component.
     */
    protected static WeakReference focusOwner;

    /**
     * Whether manager is initialized or not.
     */
    protected static boolean initialized = false;

    /**
     * Initializes manager if it wasn't already initialized.
     */
    public static synchronized void initialize ()
    {
        // To avoid more than one initialization
        if ( !initialized )
        {
            // Remember that initialization happened
            initialized = true;

            // Global focus listener
            Toolkit.getDefaultToolkit ().addAWTEventListener ( new AWTEventListener ()
            {
                @Override
                public void eventDispatched ( final AWTEvent event )
                {
                    if ( event instanceof FocusEvent )
                    {
                        if ( globalFocusListeners.size () > 0 )
                        {
                            // Filtering unnecessary events (each focus change within application generates 2 events - lost/gained)
                            final FocusEvent focusEvent = ( FocusEvent ) event;
                            if ( focusEvent.getID () == FocusEvent.FOCUS_LOST && focusEvent.getOppositeComponent () == null )
                            {
                                // Focus moved outside the application
                                fireGlobalFocusChanged ( focusEvent.getComponent (), null );
                            }
                            else if ( focusEvent.getID () == FocusEvent.FOCUS_GAINED )
                            {
                                // Focus changed within the application (or might have just entered the window)
                                fireGlobalFocusChanged ( focusEvent.getOppositeComponent (), focusEvent.getComponent () );
                            }
                        }
                    }
                }
            }, AWTEvent.FOCUS_EVENT_MASK );

            // Adding global focus listener to inform focus trackers
            registerGlobalFocusListener ( new GlobalFocusListener ()
            {
                @Override
                public void focusChanged ( final Component oldFocus, final Component newFocus )
                {
                    oldFocusOwner = new WeakReference ( oldFocus );
                    focusOwner = new WeakReference ( newFocus );

                    // Debug info
                    if ( GlobalConstants.DEBUG )
                    {
                        final String oldName = oldFocus != null ? oldFocus.getClass ().getName () : null;
                        final String newName = newFocus != null ? newFocus.getClass ().getName () : null;
                        Log.debug ( this, "Focus changed: " + oldName + " --> " + newName );
                    }

                    // Checking all added trackers
                    // Iterating through registered components
                    for ( final Map.Entry> entry : getTrackersCopy ().entrySet () )
                    {
                        // Retrieving tracked component
                        final Component tracked = entry.getKey ();
                        if ( tracked != null )
                        {
                            // Iterating through registered component trackers
                            for ( final Map.Entry innerEntry : entry.getValue ().entrySet () )
                            {
                                // Skip if tracker is disabled
                                final FocusTracker focusTracker = innerEntry.getKey ();
                                if ( focusTracker.isTrackingEnabled () )
                                {
                                    // Checking whether or not component is related to this focus change
                                    final boolean isOldFocused = focusTracker.isInvolved ( oldFocus, tracked );
                                    final boolean isNewFocused = focusTracker.isInvolved ( newFocus, tracked );

                                    // Informing object only if it is involved in changes
                                    if ( isOldFocused || isNewFocused )
                                    {
                                        // Informing about focus changes if needed
                                        final Boolean trackerStateCache = innerEntry.getValue ();
                                        if ( trackerStateCache == null || trackerStateCache != isNewFocused )
                                        {
                                            // Informing tracker about focus change
                                            focusTracker.focusChanged ( isNewFocused );

                                            // Caching focus state
                                            synchronized ( trackersLock )
                                            {
                                                final Map ct = trackers.get ( tracked );
                                                if ( ct != null && ct.containsKey ( focusTracker ) )
                                                {
                                                    ct.put ( focusTracker, isNewFocused );
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } );
        }
    }

    /**
     * Returns trackers map copy.
     *
     * @return trackers map copy
     */
    protected static Map> getTrackersCopy ()
    {
        // Checking all added trackers
        synchronized ( trackersLock )
        {
            final Map> copy = new HashMap> ( trackers.size () );
            for ( final Map.Entry> entry : trackers.entrySet () )
            {
                final Map trackers = entry.getValue ();
                final Map trackersCopy = new HashMap ( trackers.size () );
                for ( final Map.Entry innerEntry : trackers.entrySet () )
                {
                    trackersCopy.put ( innerEntry.getKey (), innerEntry.getValue () );
                }
                copy.put ( entry.getKey (), trackersCopy );
            }
            return copy;
        }
    }

    /**
     * Returns currently focused component.
     *
     * @return currently focused component
     */
    public static Component getFocusOwner ()
    {
        return focusOwner != null ? focusOwner.get () : null;
    }

    /**
     * Returns previously focused component.
     *
     * @return previously focused component
     */
    public static Component getOldFocusOwner ()
    {
        return oldFocusOwner.get ();
    }

    /**
     * Registers global focus listener.
     *
     * @param listener new global focus listener
     */
    public static void registerGlobalFocusListener ( final GlobalFocusListener listener )
    {
        synchronized ( listenersLock )
        {
            globalFocusListeners.add ( listener );
        }
    }

    /**
     * Unregisters global focus listener.
     *
     * @param listener global focus listener to unregister
     */
    public static void unregisterGlobalFocusListener ( final GlobalFocusListener listener )
    {
        synchronized ( listenersLock )
        {
            globalFocusListeners.remove ( listener );
        }
    }

    /**
     * Fires about global focus change.
     *
     * @param oldComponent previously focused component
     * @param newComponent currently focused component
     */
    protected static void fireGlobalFocusChanged ( final Component oldComponent, final Component newComponent )
    {
        final List listeners;
        synchronized ( listenersLock )
        {
            listeners = CollectionUtils.copy ( globalFocusListeners );
        }
        for ( final GlobalFocusListener listener : listeners )
        {
            listener.focusChanged ( oldComponent, newComponent );
        }
    }

    /**
     * Registers focus tracker.
     * 

* Be aware that when all links to either component or focus tracker (outside of the FocusManager) are lost all component focus * trackers or specific focus tracker will be disposed. So make sure you keep strong references to whatever you pass here. * * @param focusTracker new focus tracker */ public static void addFocusTracker ( final Component component, final FocusTracker focusTracker ) { synchronized ( trackersLock ) { Map componentTrackers = trackers.get ( component ); if ( componentTrackers == null ) { // Trackers must also be kept in a weak references as they might have links leading to component // That caused most of memory leak issues in previous tracker versions componentTrackers = new WeakHashMap (); trackers.put ( component, componentTrackers ); } componentTrackers.put ( focusTracker, focusTracker.isInvolved ( getFocusOwner (), component ) ); } } /** * Unregisters specified focus tracker. * * @param focusTracker focus tracker to unregister */ public static void removeFocusTracker ( final FocusTracker focusTracker ) { synchronized ( trackersLock ) { final Iterator>> iterator = trackers.entrySet ().iterator (); while ( iterator.hasNext () ) { final Map.Entry> entry = iterator.next (); final Map componentTrackers = entry.getValue (); componentTrackers.remove ( focusTracker ); if ( componentTrackers.size () == 0 ) { iterator.remove (); } } } } /** * Unregisters all focus trackers from the specified component. * * @param component component to unregister all focus trackers from */ public static void removeFocusTrackers ( final Component component ) { synchronized ( trackersLock ) { final Map allTrackers = trackers.get ( component ); if ( allTrackers != null && allTrackers.size () > 0 ) { allTrackers.clear (); } trackers.remove ( component ); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy