Package net.helipilot50.stocktrade.displayproject.events

Source Code of net.helipilot50.stocktrade.displayproject.events.TraverseListener

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.displayproject.events;

import java.awt.Component;
import java.awt.Container;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Hashtable;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTree;

import net.helipilot50.stocktrade.displayproject.controls.ArrayField;
import net.helipilot50.stocktrade.displayproject.controls.ListView;
import net.helipilot50.stocktrade.displayproject.controls.OutlineField;
import net.helipilot50.stocktrade.displayproject.table.ArrayFieldCellHelper;
import net.helipilot50.stocktrade.framework.ForteKeyboardFocusManager;
import net.helipilot50.stocktrade.framework.ParameterHolder;


/**
* This class implements the Traverse event. The requirements are
* that the event is posted when the focus changes from one of my
* children to another. However, because we're installing this on
* the keyboard focus manager, we must de-install this when the
* window closes. For this reason, this class is not suitable for
* a static implementations in the ClientEventManager.
* @author Tim
*/
public class TraverseListener extends WindowAdapter implements InstallableListener, PropertyChangeListener  {

    private Container container;
    private Component prevOldComp;
    private Component prevNewComp;
    static final String FOCUS_EVENT_NAME = "permanentFocusOwner";

    public void install(Object o, String pEvent) {
        if (o instanceof Container) {
            this.container = (Container)o;
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(FOCUS_EVENT_NAME, this);
        }
       
        // TF:01/05/2008:Added in the window listener so this event gets deregistered at some stage
        if (o instanceof JComponent) {
          Container container = ((JComponent)o).getTopLevelAncestor();
          if (container instanceof Window) {
            ((Window)container).addWindowListener(this);
          }
        }
        else if (o instanceof Window) {
          ((Window)o).addWindowListener(this);
        }
    }

    /**
     * When the window is closed, de-register this adapter.
     */
    public void windowClosed(WindowEvent e) {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removePropertyChangeListener(FOCUS_EVENT_NAME, this);
    }

    /**
     * When the property changes (permanent focus owner) we fire an event. However, we must check that both
     * components are children of the current container.
     *
     * These traverse events seem to come in pairs.  One for the old component, one for the new.  We, however, want them
     * in one event. (JIRA OTA-70).
     */
    public void propertyChange(PropertyChangeEvent evt) {
        Component oldComp = getTrueComponent((Component)evt.getOldValue());
        Component newComp = getTrueComponent((Component)evt.getNewValue());

        // TF: Fixed up the logic so this class worked properly for more than just the first time...
        boolean clearInformation = false;
        if (oldComp != null) {
            if (this.container.isAncestorOf(oldComp)) {
                prevOldComp = oldComp;
                if (newComp == null) {
                    // We assume old components always come before new ones.
                    prevNewComp = null;
                }
            }
            else {
                // We don't own this component, we won't post the event
                clearInformation = true;
            }
        }

        if (newComp != null) {
            if (this.container.isAncestorOf(newComp)) {
                prevNewComp = newComp;
            }
            else {
                // We don't own this component, we won't post the event
                clearInformation = true;
            }
        }

        if (prevNewComp != null && prevOldComp != null) {
            if (prevNewComp != prevOldComp) {
                // TF:12/10/07: We don't post Traverse events if the target is a child editor, because the array field will do this itself.
                if (ArrayFieldCellHelper.getArrayField(prevNewComp) == null && !(prevNewComp instanceof ArrayField) && !(prevOldComp instanceof ArrayField)) {
                    Hashtable<String, Object> params = new Hashtable<String, Object>();
                    params.put("source", new ParameterHolder(prevOldComp));
                    params.put("target", new ParameterHolder(prevNewComp));
                    params.put("reason", new ParameterHolder(ForteKeyboardFocusManager.getTraversalReason()));
                    ClientEventManager.postEvent(this.container, "Traverse", params );
                }
            }           

            // TF:30/9/07:Corrected this so the old and new components are nulled out. Otherwise the events will
            // get paired together, but not necessarily with the right partner!
            clearInformation = true;
        }

        if (clearInformation) {
            prevOldComp = null;
            prevNewComp = null;
        }
    }

    /**
     * In the case of some of the compound widgets such as a ListView, the widget which should say they
     * had the focus is not really the one which is passed in. In these cases we need to return the
     * correct widget such as the listView.
     * @param pComp
     */
    protected static Component getTrueComponent(Component pComp) {
        if ((pComp instanceof JTable || pComp instanceof JTree) && pComp.getParent() != null) {
            Component c = pComp.getParent();
            while (c != null) {
                if (c instanceof ListView) {
                    return c;
                }
                else if (c instanceof OutlineField) {
                    return c;
                }
                c = c.getParent();
            }
            return pComp;
        }
        else if (pComp != null && pComp.getParent() instanceof JComboBox) {
            return pComp.getParent();
        }
        return pComp;
    }
}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.events.TraverseListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.