Package DisplayProject.events

Source Code of DisplayProject.events.AfterValueChangeListener$AftterValueChangeFocusListener

/*
Copyright (c) 2003-2008 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 DisplayProject.events;

import java.awt.Component;
import java.awt.Container;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JScrollBar;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;

import DisplayProject.DropListModel;
import DisplayProject.FocusHelper;
import DisplayProject.GridField;
import DisplayProject.UIutils;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.AutoResizingComboBox;
import DisplayProject.controls.FillInField;
import DisplayProject.controls.Panel;
import DisplayProject.table.ArrayFieldCellHelper;
import Framework.EventManager;

public class AfterValueChangeListener implements AdjustmentListener, InstallableListener,ListSelectionListener, ItemListener, ActionListener  {

    public void install(Object o, String pEvent) {
        if (o instanceof JScrollBar) {
            ((JScrollBar)o).addAdjustmentListener(this);
        }
        else if (o instanceof JComboBox) {
            ((JComboBox)o).addActionListener(this);
        }
        else if (o instanceof JList) {
            ((JList)o).addListSelectionListener(this);
        }
        // TF:5/8/07:Added check box afterValueChangeListeners
        else if (o instanceof JCheckBox) {
            ((JCheckBox)o).addActionListener(this);
        }
        // TF:14/01/2009:Extended this to cater for GridFields and Panels. Will probably work for other compound fields too.
        else if (o instanceof GridField || o instanceof Panel) {
          Container c = (Container)o;
          PropertyChangeListener[] pcls = c.getPropertyChangeListeners();
          for (PropertyChangeListener fl : pcls) {
            if (fl instanceof AftterValueChangeFocusListener) {
              // We've already been registered, do nothing
              return;
            }
          }
          new AftterValueChangeFocusListener(c);
        }
    }

    public void adjustmentValueChanged(AdjustmentEvent e) {
        if (e.getValueIsAdjusting() &&
                (e.getSource() instanceof JScrollBar))
            return;
        postEvent((JComponent)e.getSource());
    }

    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()){
            // TF:14/7/07: Only post the event if the value is not adjusting
            // TF:5/8/07: Refactored posting logic into a common method
            postEvent((JComponent)e.getSource());
        }
    }

    public void itemStateChanged(ItemEvent e) {
        postEvent((JComponent)e.getSource());
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof AutoResizingComboBox) {
            AutoResizingComboBox cb = (AutoResizingComboBox)e.getSource();
            DropListModel model = (DropListModel)cb.getModel();

            if (model.isAdjusting() || !cb.isEnabled()) {
                return;
            }
            // TF:28/3/07: Editable comboboxes will post an afterValueChange event when they lose the
            // focus, because their value is not necessarily equal to their selected value. However,
            // in this case we'll have a data field that will post the AfterValueChange event for us.184157
            // TF:18/7/07: If we rely on the data field having their value set, we'll miss the after value change
            // event that should be fired immediately on changing the value with the drop-down with the MOUSE.
            // (We'll still get the keyboard one). Hence ignore all but comboBoxChanged with modifiers = Button1 etc.
//            if (cb.isEditable() && (!(e.getActionCommand().equals("comboBoxChanged") && e.getModifiers() > 0))) {
//                return;
//            }
           
            // TF:26/06/2008:If we're a table editor, we must commit the value first as the model hasn't updated at this point in time
            ArrayField af = ArrayFieldCellHelper.getArrayField(cb);
            if (af != null && ArrayFieldCellHelper.getArrayEditorComponent(af) == cb) {
              af.setValueAt(af.getCellEditor().getCellEditorValue(), af.getEditingRow(), af.getEditingColumn());
            }
            EventManager.startEventChain();
            FocusHelper.addSetFocusPurgeAction(cb);
            Logger.getLogger(this.getClass()).debug("ComboBox Action: " + e.getActionCommand());
            if ((e.getActionCommand() == "comboBoxChanged") ||
                    (e.getActionCommand() == "comboBoxEdited")) {
                postEvent((JComponent)e.getSource());
            }
            EventManager.endEventChain();
        }
        else if (e.getSource() instanceof JCheckBox) {
            // TF:5/8/07:Added check box afterValueChangeListeners
            postEvent((JComponent)e.getSource());
        }
    }

    public void postEvent(JComponent c) {
      // CraigM:22/01/2009 - Use the FillInField AfterValueChange method
      if (c instanceof FillInField) {
        ((FillInField)c).postAfterValueChange();
      }
      else {
          EventManager.startEventChain();
          ClientEventManager.postEvent( c, "AfterValueChange" );
          // TF:14/7/07: Added the child after value change events
          UIutils.setDataChangedFlag(c);
          // TF:27/9/07:Revamped to use new event poster
          ChildEventHelper.postEventToAllParents(c, "ChildAfterValueChange");
          EventManager.endEventChain();
      }
    }

    /**
     * This class enables the posting of AfterValueChange events on compound field widgets such as Panels and GridFields.
     * In order to do this, we need to detect when any of our children have changed, and when focus leaves this container.
     * To perform the first part, we subscribe for the hasDataChanged event which is fired when data on a widget changes, and
     * is fired to that widget and all parent widgets of the changing widget<p>
     * <p>
     * To cater for the focus change, as containers are inherently unfocusable themselves (they focus on their children and
     * not themselves in an alturistic manner) we have to listen for the change in the permanent focus from the
     * KeyboardFocusManager
     * @author Tim
     *
     */
    private class AftterValueChangeFocusListener extends WindowAdapter implements PropertyChangeListener, WindowListener {
        private Container container;
        private Component prevOldComp;
        private Component prevNewComp;
        static final String FOCUS_EVENT_NAME = "permanentFocusOwner";
        static final String DATA_CHANGED_EVENT_NAME = "hasDataChanged";

      boolean hasChanged = false;
      public AftterValueChangeFocusListener(Container comp) {
        this.container = comp;
        comp.addPropertyChangeListener(DATA_CHANGED_EVENT_NAME, this);

        if (comp instanceof JComponent) {
          Container container = ((JComponent)comp).getTopLevelAncestor();
            if (container instanceof Window) {
              ((Window)container).addWindowListener(this);
            }
        }
        else if (comp instanceof Window) {
          ((Window)comp).addWindowListener(this);
        }
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(FOCUS_EVENT_NAME, this);
      }
     
      /**
       * Handle the property change event. This listens for 2 events:
       * <li>hasDataChanged - fired from a component to all ancestors when a component changes
       * <li>permanentFocusOwner - fired by the KeyboardFocusManager when focus changes to a new component<p>
       * The handling of the permanentFocusOwner is very similar to the handling of the TraverseListener
       */
    public void propertyChange(PropertyChangeEvent evt) {
      if (evt.getPropertyName().equals(DATA_CHANGED_EVENT_NAME) && evt.getNewValue() instanceof Boolean && ((Boolean)evt.getNewValue()).booleanValue()) {
        hasChanged = true;
      }
      else if (evt.getPropertyName().equals(FOCUS_EVENT_NAME)) {
            Component oldComp = TraverseListener.getTrueComponent((Component)evt.getOldValue());
            Component newComp = TraverseListener.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;
                    hasChanged = false;
                }
            }

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

            if (prevNewComp != null && prevOldComp != null) {
                if (prevNewComp != prevOldComp) {
                  if (hasChanged) {
                    postEvent((JComponent)this.container);
                  }
                }           
                clearInformation = true;
                hasChanged = false;
            }

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

Related Classes of DisplayProject.events.AfterValueChangeListener$AftterValueChangeFocusListener

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.