Class Bean

  • All Implemented Interfaces:
    ObservableBean, ObservableBean2, java.io.Serializable
    Direct Known Subclasses:
    AbstractUnitConverter

    public abstract class Bean
    extends java.lang.Object
    implements java.io.Serializable, ObservableBean2
    An abstract superclass that minimizes the effort required to provide change support for bound and constrained Bean properties. This class follows the conventions and recommendations as described in the Java Bean Specification.

    This class uses the standard PropertyChangeSupport to notify registered listeners about changes. Subclasses can use different change support implementations by overriding createPropertyChangeSupport, for example to ensure that notifications are sent in the Event dispatch thread, or to compare old and new values with == not equals.

    Author:
    Karsten Lentzsch
    See Also:
    PropertyChangeEvent, PropertyChangeListener, PropertyChangeSupport, VetoableChangeListener, VetoableChangeSupport, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.beans.PropertyChangeSupport changeSupport
      If anyPropertyChangeListeners have been registered, the changeSupport field describes them.
    • Constructor Summary

      Constructors 
      Constructor Description
      Bean()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPropertyChangeListener​(java.beans.PropertyChangeListener listener)
      Adds a PropertyChangeListener to the listener list.
      void addPropertyChangeListener​(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
      Adds a PropertyChangeListener to the listener list for a specific property.
      void addVetoableChangeListener​(java.beans.VetoableChangeListener listener)
      Adds a VetoableChangeListener to the listener list.
      void addVetoableChangeListener​(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
      Adds a VetoableChangeListener to the listener list for a specific property.
      protected java.beans.PropertyChangeSupport createPropertyChangeSupport​(java.lang.Object bean)
      Creates and returns a PropertyChangeSupport for the given bean.
      protected void fireIndexedPropertyChange​(java.lang.String propertyName, int index, boolean oldValue, boolean newValue)
      Report a boolean bound indexed property update to any registered listeners.
      protected void fireIndexedPropertyChange​(java.lang.String propertyName, int index, int oldValue, int newValue)
      Report an int bound indexed property update to any registered listeners.
      protected void fireIndexedPropertyChange​(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)
      Report a bound indexed property update to any registered listeners.
      protected void fireMultiplePropertiesChanged()
      Indicates that an arbitrary set of bound properties have changed.
      protected void firePropertyChange​(java.beans.PropertyChangeEvent event)
      General support for reporting bound property changes.
      protected void firePropertyChange​(java.lang.String propertyName, boolean oldValue, boolean newValue)
      Support for reporting bound property changes for boolean properties.
      protected void firePropertyChange​(java.lang.String propertyName, double oldValue, double newValue)
      Support for reporting bound property changes for integer properties.
      protected void firePropertyChange​(java.lang.String propertyName, float oldValue, float newValue)
      Support for reporting bound property changes for integer properties.
      protected void firePropertyChange​(java.lang.String propertyName, int oldValue, int newValue)
      Support for reporting bound property changes for integer properties.
      protected void firePropertyChange​(java.lang.String propertyName, long oldValue, long newValue)
      Support for reporting bound property changes for integer properties.
      protected void firePropertyChange​(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
      Support for reporting bound property changes for Object properties.
      protected void fireVetoableChange​(java.beans.PropertyChangeEvent event)
      General support for reporting constrained property changes.
      protected void fireVetoableChange​(java.lang.String propertyName, boolean oldValue, boolean newValue)
      Support for reporting changes for constrained boolean properties.
      protected void fireVetoableChange​(java.lang.String propertyName, double oldValue, double newValue)
      Support for reporting changes for constrained integer properties.
      protected void fireVetoableChange​(java.lang.String propertyName, float oldValue, float newValue)
      Support for reporting changes for constrained integer properties.
      protected void fireVetoableChange​(java.lang.String propertyName, int oldValue, int newValue)
      Support for reporting changes for constrained integer properties.
      protected void fireVetoableChange​(java.lang.String propertyName, long oldValue, long newValue)
      Support for reporting changes for constrained integer properties.
      protected void fireVetoableChange​(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
      Support for reporting changes for constrained Object properties.
      java.beans.PropertyChangeListener[] getPropertyChangeListeners()
      Returns an array of all the property change listeners registered on this component.
      java.beans.PropertyChangeListener[] getPropertyChangeListeners​(java.lang.String propertyName)
      Returns an array of all the listeners which have been associated with the named property.
      java.beans.VetoableChangeListener[] getVetoableChangeListeners()
      Returns an array of all the property change listeners registered on this component.
      java.beans.VetoableChangeListener[] getVetoableChangeListeners​(java.lang.String propertyName)
      Returns an array of all the listeners which have been associated with the named property.
      void removePropertyChangeListener​(java.beans.PropertyChangeListener listener)
      Removes a PropertyChangeListener from the listener list.
      void removePropertyChangeListener​(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
      Removes a PropertyChangeListener from the listener list for a specific property.
      void removeVetoableChangeListener​(java.beans.VetoableChangeListener listener)
      Removes a VetoableChangeListener from the listener list.
      void removeVetoableChangeListener​(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
      Removes a VetoableChangeListener from the listener list for a specific property.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Bean

        public Bean()
    • Method Detail

      • addVetoableChangeListener

        public final void addVetoableChangeListener​(java.lang.String propertyName,
                                                    java.beans.VetoableChangeListener listener)
        Adds a VetoableChangeListener to the listener list for a specific property. The specified property may be user-defined.

        Note that if this Model is inheriting a bound property, then no event will be fired in response to a change in the inherited property.

        If listener is null, no exception is thrown and no action is performed.

        Parameters:
        propertyName - one of the property names listed above
        listener - the VetoableChangeListener to be added
        See Also:
        removeVetoableChangeListener(String, VetoableChangeListener), addVetoableChangeListener(String, VetoableChangeListener), getVetoableChangeListeners(String)
      • createPropertyChangeSupport

        protected java.beans.PropertyChangeSupport createPropertyChangeSupport​(java.lang.Object bean)
        Creates and returns a PropertyChangeSupport for the given bean. Invoked by the first call to addPropertyChangeListener(java.beans.PropertyChangeListener) when lazily creating the sole change support instance used throughout this bean.

        This default implementation creates a PropertyChangeSupport. Subclasses may override to return other change support implementations. For example to ensure that listeners are notified in the Event dispatch thread (EDT change support). The JGoodies Binding uses an extended change support that allows to configure whether the old and new value are compared with == or equals.

        Parameters:
        bean - the bean to create a change support for
        Returns:
        the new change support
      • firePropertyChange

        protected final void firePropertyChange​(java.beans.PropertyChangeEvent event)
        General support for reporting bound property changes. Sends the given PropertyChangeEvent to any registered PropertyChangeListener.

        Most bean setters will invoke the fireXXX methods that get a property name and the old and new value. However some frameworks and setters may prefer to use this general method. Also, this method allows to fire IndexedPropertyChangeEvents that have been introduced in Java 5.

        Parameters:
        event - describes the property change
        Since:
        1.3
      • firePropertyChange

        protected final void firePropertyChange​(java.lang.String propertyName,
                                                java.lang.Object oldValue,
                                                java.lang.Object newValue)
        Support for reporting bound property changes for Object properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
      • firePropertyChange

        protected final void firePropertyChange​(java.lang.String propertyName,
                                                boolean oldValue,
                                                boolean newValue)
        Support for reporting bound property changes for boolean properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
      • firePropertyChange

        protected final void firePropertyChange​(java.lang.String propertyName,
                                                double oldValue,
                                                double newValue)
        Support for reporting bound property changes for integer properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
      • firePropertyChange

        protected final void firePropertyChange​(java.lang.String propertyName,
                                                float oldValue,
                                                float newValue)
        Support for reporting bound property changes for integer properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
      • firePropertyChange

        protected final void firePropertyChange​(java.lang.String propertyName,
                                                int oldValue,
                                                int newValue)
        Support for reporting bound property changes for integer properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
      • firePropertyChange

        protected final void firePropertyChange​(java.lang.String propertyName,
                                                long oldValue,
                                                long newValue)
        Support for reporting bound property changes for integer properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
      • fireMultiplePropertiesChanged

        protected final void fireMultiplePropertiesChanged()
        Indicates that an arbitrary set of bound properties have changed. Sends a PropertyChangeEvent with property name, old and new value set to null to any registered PropertyChangeListeners.
        Since:
        1.0.3
        See Also:
        PropertyChangeEvent
      • fireIndexedPropertyChange

        protected final void fireIndexedPropertyChange​(java.lang.String propertyName,
                                                       int index,
                                                       java.lang.Object oldValue,
                                                       java.lang.Object newValue)
        Report a bound indexed property update to any registered listeners.

        No event is fired if old and new values are equal and non-null.

        Parameters:
        propertyName - The programmatic name of the property that was changed.
        index - index of the property element that was changed.
        oldValue - The old value of the property.
        newValue - The new value of the property.
        Since:
        2.0
      • fireIndexedPropertyChange

        protected final void fireIndexedPropertyChange​(java.lang.String propertyName,
                                                       int index,
                                                       int oldValue,
                                                       int newValue)
        Report an int bound indexed property update to any registered listeners.

        No event is fired if old and new values are equal and non-null.

        This is merely a convenience wrapper around the more general fireIndexedPropertyChange method which takes Object values.

        Parameters:
        propertyName - The programmatic name of the property that was changed.
        index - index of the property element that was changed.
        oldValue - The old value of the property.
        newValue - The new value of the property.
        Since:
        2.0
      • fireIndexedPropertyChange

        protected final void fireIndexedPropertyChange​(java.lang.String propertyName,
                                                       int index,
                                                       boolean oldValue,
                                                       boolean newValue)
        Report a boolean bound indexed property update to any registered listeners.

        No event is fired if old and new values are equal and non-null.

        This is merely a convenience wrapper around the more general fireIndexedPropertyChange method which takes Object values.

        Parameters:
        propertyName - The programmatic name of the property that was changed.
        index - index of the property element that was changed.
        oldValue - The old value of the property.
        newValue - The new value of the property.
        Since:
        2.0
      • fireVetoableChange

        protected final void fireVetoableChange​(java.beans.PropertyChangeEvent event)
                                         throws java.beans.PropertyVetoException
        General support for reporting constrained property changes. Sends the given PropertyChangeEvent to any registered PropertyChangeListener.

        Most bean setters will invoke the fireXXX methods that get a property name and the old and new value. However some frameworks and setters may prefer to use this general method. Also, this method allows to fire IndexedPropertyChangeEvents that have been introduced in Java 5.

        Parameters:
        event - describes the property change
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected
        Since:
        1.3
      • fireVetoableChange

        protected final void fireVetoableChange​(java.lang.String propertyName,
                                                java.lang.Object oldValue,
                                                java.lang.Object newValue)
                                         throws java.beans.PropertyVetoException
        Support for reporting changes for constrained Object properties. This method can be called before a constrained property will be changed and it will send the appropriate PropertyChangeEvent to any registered VetoableChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected
      • fireVetoableChange

        protected final void fireVetoableChange​(java.lang.String propertyName,
                                                boolean oldValue,
                                                boolean newValue)
                                         throws java.beans.PropertyVetoException
        Support for reporting changes for constrained boolean properties. This method can be called before a constrained property will be changed and it will send the appropriate PropertyChangeEvent to any registered VetoableChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected
      • fireVetoableChange

        protected final void fireVetoableChange​(java.lang.String propertyName,
                                                double oldValue,
                                                double newValue)
                                         throws java.beans.PropertyVetoException
        Support for reporting changes for constrained integer properties. This method can be called before a constrained property will be changed and it will send the appropriate PropertyChangeEvent to any registered VetoableChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected
      • fireVetoableChange

        protected final void fireVetoableChange​(java.lang.String propertyName,
                                                int oldValue,
                                                int newValue)
                                         throws java.beans.PropertyVetoException
        Support for reporting changes for constrained integer properties. This method can be called before a constrained property will be changed and it will send the appropriate PropertyChangeEvent to any registered VetoableChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected
      • fireVetoableChange

        protected final void fireVetoableChange​(java.lang.String propertyName,
                                                float oldValue,
                                                float newValue)
                                         throws java.beans.PropertyVetoException
        Support for reporting changes for constrained integer properties. This method can be called before a constrained property will be changed and it will send the appropriate PropertyChangeEvent to any registered VetoableChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected
      • fireVetoableChange

        protected final void fireVetoableChange​(java.lang.String propertyName,
                                                long oldValue,
                                                long newValue)
                                         throws java.beans.PropertyVetoException
        Support for reporting changes for constrained integer properties. This method can be called before a constrained property will be changed and it will send the appropriate PropertyChangeEvent to any registered VetoableChangeListeners.
        Parameters:
        propertyName - the property whose value has changed
        oldValue - the property's previous value
        newValue - the property's new value
        Throws:
        java.beans.PropertyVetoException - if a constrained property change is rejected