Class ObservableProperties

  • All Implemented Interfaces:
    Serializable


    public class ObservableProperties
    extends Object
    implements Serializable
    This manages multiple sets of properties, including listeners.

    Eventually we might want to ramp this up to rival the ObservableList in terms of synchronized modification safeguards and RecursiveListenerModificationExceptions.

    Notes this includes parameterized keys, and it is technically possible to have two keys that use the same name but map to different types.

    See Also:
    Serialized Form
    • Field Detail

      • DEFAULT

        public static final String DEFAULT
        The string "default". This is recommended for a group of properties that represent vital elements of an object, and are serializable.
        See Also:
        Constant Field Values
      • TRANSIENT

        public static final String TRANSIENT
        The string "transient". This is recommended for a group of properties that are not meant to be serialized (cached indices, renderings, etc.).
        See Also:
        Constant Field Values
    • Constructor Detail

      • ObservableProperties

        public ObservableProperties()
        Create a new ObservableProperties that will accept any group ID.
      • ObservableProperties

        public ObservableProperties(ObservableProperties p)
        Clone this ObservableProperties.
      • ObservableProperties

        public ObservableProperties(String... groupNames)
        Create a new ObservableProperties with a limited set of possible group IDs.
        Parameters:
        groupNames - the group IDs this object allows. (If null: then all group IDs are allowed.) If non-null: then attempting to access a group ID not in this set will result in an exception.
    • Method Detail

      • beginEdit

        public ObservableProperties.Edit beginEdit()
        Begin recording an edit that suspends all listeners until endEdit(Edit) is called.

        This is needed when it is dangerous for a listener to get an incomplete picture of the state of this object. For example if you are changing the VALUE key to 5 and the MAXIMUM key to 8, if a listener were notified after the first step and the MAXIMUM value was an older value (such as 3), then it might throw an exception because it assumes the VALUE is always less than the MAXIMUM.

        Returns:
        a unique identifier to pass to endEdit() when your changes are complete.
      • endEdit

        public void endEdit(ObservableProperties.Edit endedEdit)
        This must be called after beginEdit() to reactive listeners for this object.
        Parameters:
        endedEdit - a token created by a call to beginEdit(). When there are no unresolved edits: all pending PropertyChangeEvents will be flushed.
      • clearEventQueue

        protected void clearEventQueue()
        Flushes all QueueElements from the eventQueue.
      • toString

        public String toString()
      • getGroupNames

        public SortedSet<String> getGroupNames()
        Returns:
        all the group names currently in use. (This is not related to the Strings passed during construction that limit the possible group names.)
      • addListener

        public void addListener(PropertyChangeListener pcl)
        Add a PropertyChangeListener for DEFAULT properties.
        Parameters:
        pcl - the listener to add.
      • removeListener

        public void removeListener(PropertyChangeListener pcl)
        Remove a PropertyChangeListener for DEFAULT properties.
        Parameters:
        pcl - the listener to remove.
      • addListener

        public void addListener(String propertyGroup,
                                PropertyChangeListener pcl)
        Add a PropertyChangeListener for a given group of properties.
        Parameters:
        propertyGroup - the group of properties this relates to. For example: DEFAULT or TRANSIENT.
        pcl - the listener to add.
      • addListener

        public void addListener(String propertyGroup,
                                ObservableProperties.Key<?> key,
                                PropertyChangeListener pcl)
        Add a PropertyChangeListener for a key in a given group of properties.
        Parameters:
        propertyGroup - the group of properties this relates to. For example: DEFAULT or TRANSIENT.
        key - an optional key that limits what this listener which notified about. If null: then this listener will be notified for all changes.
        pcl - the listener to add.
      • addListener

        public void addListener(String propertyGroup,
                                ObservableProperties.Key<?> key,
                                ChangeListener changeListener)
        Add a ChangeListener for a key in a given group of properties.
        Parameters:
        propertyGroup - the group of properties this relates to. For example: DEFAULT or TRANSIENT.
        key - an optional key that limits what this listener which notified about. If null: then this listener will be notified for all changes.
        changeListener - the ChangeListener to be notified
      • addListener

        public void addListener(String propertyGroup,
                                ObservableProperties.Key<?> key,
                                ActionListener actionListener)
        Add an ActionListener for a key in a given group of properties.
        Parameters:
        propertyGroup - the group of properties this relates to. For example: DEFAULT or TRANSIENT.
        key - an optional key that limits what this listener which notified about. If null: then this listener will be notified for all changes.
        actionListener - the ActionListener to be notified.
      • getPropertyListeners

        public PropertyChangeListener[] getPropertyListeners()
        Return all the PropertyChangeListeners that listen for changes to DEFAULT properties.
      • getPropertyListeners

        public PropertyChangeListener[] getPropertyListeners(String propertyGroup)
        Return all the PropertyChangeListeners that listen for changes to a given group of properties.
        Parameters:
        propertyGroup - the group of properties this relates to. For example: DEFAULT or TRANSIENT.
      • removeListener

        public void removeListener(String propertyGroup,
                                   ObservableProperties.Key<?> key,
                                   ChangeListener changeListener)
        Remove a ChangeListener for a given group of properties.
        Parameters:
        propertyGroup - the group this listener belongs to.
        key - the key this listener relates to.
        changeListener - the listener to remove.
      • removeListener

        public void removeListener(String propertyGroup,
                                   ObservableProperties.Key<?> key,
                                   ActionListener actionListener)
        Remove an ActionListener for a given group of properties.
        Parameters:
        propertyGroup - the group this listener belongs to.
        key - the key this listener relates to.
        actionListener - the listener to remove.
      • removeListener

        public void removeListener(String propertyGroup,
                                   PropertyChangeListener pcl)
        Remove a PropertyChangeListener for a given group of properties.
        Parameters:
        propertyGroup - the group this listener belongs to.
        pcl - the listener to remove.
      • removeListener

        public void removeListener(String propertyGroup,
                                   ObservableProperties.Key<?> key,
                                   PropertyChangeListener pcl)
        Remove a PropertyChangeListener for a given group of properties.
        Parameters:
        propertyGroup - the group this listener belongs to.
        key - the key this listener relates to.
        pcl - the listener to remove.
      • set

        public <T> T set(String propertyGroup,
                         ObservableProperties.Key<T> key,
                         T value)
        Set a property in a given property group.
        Parameters:
        propertyGroup - the property group to consult. (Recommended values are DEFAULT or TRANSIENT.) This cannot be null. Each ObservableProperties object may be constructed with a finite set of acceptable property groups: if that is the case for this object and this group ID is not part of this set, then an exception is thrown.
        key - the key to set.
        value - the value to set. If null: then this key is removed.
        Returns:
        the previous value.
      • get

        public <T> T get(String propertyGroup,
                         ObservableProperties.Key<T> key)
        Return a property from a given property group.
        Parameters:
        propertyGroup - the property group to consult. (Recommended values are DEFAULT or TRANSIENT.) This cannot be null. Each ObservableProperties object may be constructed with a finite set of acceptable property groups: if that is the case for this object and this group ID is not part of this set, then an exception is thrown.
        key - the key to retrieve.
        Returns:
        the current value.
      • getMap

        public Map<String,Object> getMap()
        Returns:
        a condensed map of all group IDs. This will include all group IDs and prepend them to keys.
      • keys

        public Set<ObservableProperties.Key<?>> keys(String propertyGroup)
        Parameters:
        propertyGroup - the group of keys to return.
        Returns:
        all the keys associated with a property group.
      • getMap

        public Map<String,Object> getMap(boolean prependGroupID,
                                         boolean identifyClassNames,
                                         String... groupIDs)
        Parameters:
        prependGroupID - if true, then the groupID will be prepended to all keys. It is highly recommended that this be true, because if this is false and different groups have similar sounding keys: then those groups cannot be safely condensed to a single map and exception will be thrown.
        identifyClassNames - if true then keys will be uniquely identified by including their class name.
        groupIDs - an optional list of group IDs to poll. If null: then all group IDs are included. For example: you might want to only return the DEFAULT group, at which point it would be safe to pass false for prependGroupID.
        Returns:
        a condensed map of all group IDs.
      • get

        public <T> T get(ObservableProperties.Key<T> key)
        Retrieve a property in the DEFAULT property group.
        Parameters:
        key - the key to get.
        Returns:
        the current value.
      • set

        public <T> T set(ObservableProperties.Key<T> key,
                         T value)
        Assign a property to the DEFAULT property group.
        Parameters:
        key - the key to retrieve.
        value - the new value of the key
        Returns:
        the previous value.
      • clear

        public void clear()
        Clear the DEFAULT group.
      • clear

        public void clear(String groupID)
        Clear everything in a particular property group.
        Parameters:
        groupID - the id of the group to clear
      • getMap

        public Map<ObservableProperties.Key<?>,Object> getMap(String groupName)
        Return all the properties in a particular category of this ObservableProperties.
        Parameters:
        groupName - the name of the group (such as DEFAULT or TRANSIENT)
        Returns:
        all the properties in the group provided. This will be an empty map if the group name is unused.