Package DisplayProject

Source Code of DisplayProject.StatusTextListener

/*
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 DisplayProject;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.IdentityHashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import DisplayProject.actions.StatusText;
import Framework.TextData;
/**
* This is a specialised mouse listener to implement the status text feature from Forte.
*/
public class StatusTextListener extends MouseAdapter {
  // DK:13/10/2008: reimplement the logic
    /**
     * This property is used on the window form and represents the TextData used to store the status text. The value
     * of this textdata will change as the user moves the mouse. It would be better is this was the name of the window,
     * or the data field used to store the text, but the way Forte did its status text precludes this.
     */
    public static final String cSTATUS_WIDGET = "qq_StatusWidget";

  private final static StatusTextListener sharedInstance = new StatusTextListener();
  /**key - data object mapped to status text UI component; value - old text value*/
  private static Map<TextData, String> oldValueCache = new IdentityHashMap<TextData, String>();
 
  /**
   * The listener to catch changes of status text value from outside of this class.
   */
  private final static PropertyChangeListener oldValueChangeListener = new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (!ignoreOldValueCaching) {
          Object newValue = evt.getNewValue();
        oldValueCache.put((TextData)evt.getSource(), newValue != null ? newValue.toString() : null);
      }
      }
    };
   
    /**
     * The listener to clean the cache for closed window.
     */
    private final WindowAdapter windowClosingListener = new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      clearCache(e);
    }
    /**
     * @param e
     */
    private void clearCache(WindowEvent e) {
      StatusTextListener.this.clearCache((JFrame)e.getSource());
    }
    public void windowClosed(WindowEvent e) {
      clearCache(e);
    }
  };

  private void clearCache(JFrame frame) {
    TextData statusTextDataObject = (TextData)UIutils.getForm(frame).getClientProperty(cSTATUS_WIDGET);
      if (statusTextDataObject != null) {
      statusTextDataObject.removePropertyChangeListener("value", oldValueChangeListener);
      oldValueCache.remove(statusTextDataObject);
    }
  }

  /**The flag is used to ignore changes of old value cache when change is initiated internally*/
  private static boolean ignoreOldValueCaching = false;

    private StatusTextListener(){
    }

    public void mouseEntered(MouseEvent e) {
        TextData statusTextValue = (TextData)((JComponent)e.getSource()).getClientProperty(StatusText.cSTATUS_TEXT);
        // TF:01/03/2010:DET-130:Check to see if the value is null
      setStatusTextForComponent((JComponent)e.getSource(), statusTextValue);
    }

    /**
     * Sets the status text to the window of specified component.
     * @param component the component inside of the window.
     * @param statusTextValue the status text value.
     */
  public void setStatusTextForComponent(JComponent component, TextData statusTextValue) {
    JFrame topLevelAncestor = (JFrame)component.getTopLevelAncestor();
        TextData mappedProperty = (TextData)UIutils.getForm(topLevelAncestor).getClientProperty(cSTATUS_WIDGET);
       
    if (mappedProperty != null){
      if (!oldValueCache.containsKey(mappedProperty)) {
        oldValueCache.put(mappedProperty, mappedProperty.toString());
         
        mappedProperty.addPropertyChangeListener("value", oldValueChangeListener);
        // TF:01/03/2010:Ensure we're only in the cache once, even if the status text field is changed
        topLevelAncestor.removeWindowListener(windowClosingListener);
        topLevelAncestor.addWindowListener(windowClosingListener);
      }
      // TF:01/03/2010:Always update the old value in the cache, as the mouse exit handler will expect a valid value
      oldValueCache.put(mappedProperty, mappedProperty.toString());
      if (statusTextValue != null) {
        setStatusText(mappedProperty, statusTextValue.toString());
      }
        }
  }

  /**
   * Sets status text to the mapped object without inner property listener notification.
   * @param mappedProperty status text data object.
   * @param statusText text to be set
   */
  private void setStatusText(TextData mappedProperty, String statusText) {
    ignoreOldValueCaching = true;
    // set the value to the status text
    mappedProperty.setValue(statusText != null ? statusText : "");
    ignoreOldValueCaching = false;
  }

    public void mouseExited(MouseEvent e) {
      resetStatusTextForComponent((JComponent)e.getSource());
    }

    /**
     * Revert status text value to previous one.
     * @param component the component in window with status line.
     */
  public void resetStatusTextForComponent(JComponent component) {
        TextData mappedProperty = (TextData)UIutils.getForm((JFrame)component.getTopLevelAncestor()).getClientProperty(cSTATUS_WIDGET);
        if (mappedProperty != null){
          String oldText = oldValueCache.get(mappedProperty);
          setStatusText(mappedProperty, oldText);
        }
  }

  /**
   * Gets static instance of the listener.
   * @return the singleton instance.
   */
    public static StatusTextListener sharedInstance(){
        return sharedInstance;
    }

  public void setStatusFieldForWindow(JFrame frame, TextData value) {
    // First, remove any old values there from the cache
    clearCache(frame);
        ((JPanel)frame.getContentPane()).putClientProperty(cSTATUS_WIDGET, value);
  }

  public TextData getStatusFieldForWindow(JFrame frame) {
    return (TextData)((JPanel)frame.getContentPane()).getClientProperty(cSTATUS_WIDGET);
  }
}
TOP

Related Classes of DisplayProject.StatusTextListener

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.