This module contains classes that you can use to add support for bound properties in your classes. This includes being able to add/remove property change listeners, and firing property change events when the attribute values change.
There are two ways to do this:
Inherit from a class that has the necessary addPropertyChangeListener and removePropertyChangeListener methods (like a Swing component, or JavaBeanSupport) and add the properties you want to fire change events as BeanProperty attributes
Inherit from the AutoChangeNotifier mix-in class
Both approaches have their pros and cons, which are detailed below.
This is a descriptor class that should be installed as a class attribute in a new-style object (one that inherits from object). It handles getting and setting the value, and fires a property change event when the value is set. It requires that the containing class supports property change events (via the add/remove methods mentioned above).
from swingutils.beans import JavaBeanSupport, BeanProperty class JustSomeBean(JavaBeanSupport): foo = BeanProperty('foo') bar = BeanProperty('bar', 'the initial value') bean = JustSomeBean() bean.bar = 'the next value' # Would fire a property change event
This is a mix-in class that works by providing a special method, __setattr__, that catches any attempts to set the value of an attribute. It stores the old value, sets the new attribute value, and then fires a property change event. Private and protected attributes (names starting with _) are excluded from this behavior.
from swingutils.beans import AutoChangeNotifier class JustSomeBean(AutoChangeNotifier): foo = None bar = 'the initial value' bean = JustSomeBean() bean.bar = 'the next value' # Would fire a property change event