Class SwingConfigurationTreeModel

  • All Implemented Interfaces:
    EventListener, TreeModel, net.sf.jguiraffe.gui.builder.components.model.TreeModelChangeListener, org.apache.commons.configuration.event.ConfigurationListener

    public class SwingConfigurationTreeModel
    extends Object
    implements TreeModel, org.apache.commons.configuration.event.ConfigurationListener, net.sf.jguiraffe.gui.builder.components.model.TreeModelChangeListener

    A specialized implementation of TreeModel that obtains its data from a Configuration object.

    This is a fully functional implementation of Swing's TreeModel interface. The content of the model is obtained from the nodes stored in a hierarchical configuration. The tree will display the keys of the configuration properties, i.e. the names of the nodes.

    The structure-related methods of the TreeModel interface (e.g. getRoot() or getChild()) are implemented by directly forwarding to methods provided by the ConfigurationNode interface. All of these methods expect that a passed in node (which is of type Object in the TreeModel interface) can be cast into a ConfigurationNode.

    The model also registers itself as event listener at the underlying configuration and tries to map configuration change events to corresponding model change events. This is not always possible because configuration change events often do not contain enough information for such a mapping. If the mapping is not possible, a very generic structure changed event is fired.

    As is true for most Swing objects, this class is not thread-safe. It is possible to manipulate the underlying configuration in a separate thread, which will cause change events received by this model. These events are then propagated to registered listeners in the event dispatch thread. However, this implementation relies on the fact that only a single configuration change event can be received at a time. (Because typical configuration implementations cannot be updated concurrently this should not be a limitation.)

    Version:
    $Id: SwingConfigurationTreeModel.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • SwingConfigurationTreeModel

        public SwingConfigurationTreeModel​(org.apache.commons.configuration.HierarchicalConfiguration config)
        Creates a new instance of SwingConfigurationTreeModel and initializes it with the given HierarchicalConfiguration object. Behind the scenes, a TreeConfigurationChangeHandler is created and registered at the configuration so that change events can be correctly processed.
        Parameters:
        config - the configuration (must not be null)
        Throws:
        IllegalArgumentException - if a required parameter is missing
    • Method Detail

      • getConfiguration

        public org.apache.commons.configuration.HierarchicalConfiguration getConfiguration()
        Returns the configuration object that stores the data of this model.
        Returns:
        the underlying configuration object
      • getChild

        public Object getChild​(Object node,
                               int index)
        Returns the child node of the specified node with the given index. This implementation expects that the node is of type ConfigurationNode.
        Specified by:
        getChild in interface TreeModel
        Parameters:
        node - the node
        index - the index
        Returns:
        the child node with this index
      • getChildCount

        public int getChildCount​(Object node)
        Returns the number of child nodes of the specified node. This implementation expects that the node is of type ConfigurationNode.
        Specified by:
        getChildCount in interface TreeModel
        Parameters:
        node - the node
        Returns:
        the number of child nodes of this node
      • getIndexOfChild

        public int getIndexOfChild​(Object parent,
                                   Object child)
        Returns the index of the specified child node relative to its parent node. This implementation expects that the node is of type ConfigurationNode. If the node is no child of the specified parent, -1 is returned. The parent and the child node can both be null; then -1 is returned, too.
        Specified by:
        getIndexOfChild in interface TreeModel
        Parameters:
        parent - the parent node
        child - the child node
        Returns:
        the index of this child node or -1
      • getRoot

        public Object getRoot()
        Returns the root node of this tree model. This is the root node of the underlying configuration.
        Specified by:
        getRoot in interface TreeModel
        Returns:
        the root node of this tree model
      • isLeaf

        public boolean isLeaf​(Object node)
        Tests whether the passed in node is a leaf node. This implementation expects that the passed in object is a ConfigurationNode. It then checks whether it has children.
        Specified by:
        isLeaf in interface TreeModel
        Parameters:
        node - the node
        Returns:
        a flag whether this is a leaf node
      • removeTreeModelListener

        public void removeTreeModelListener​(TreeModelListener l)
        Removes the specified event listener from this model.
        Specified by:
        removeTreeModelListener in interface TreeModel
        Parameters:
        l - the listener to be removed
      • valueForPathChanged

        public void valueForPathChanged​(TreePath path,
                                        Object newValue)
        The value of a node was changed. This method is called if the user edited a node in the tree control. This implementation will update the value of the corresponding configuration node. Then it will fire a change event.
        Specified by:
        valueForPathChanged in interface TreeModel
        Parameters:
        path - the path to the changed node
        newValue - the new value
      • configurationChanged

        @Deprecated
        public void configurationChanged​(org.apache.commons.configuration.event.ConfigurationEvent event)
        Deprecated.
        This method is no longer used or called. Configuration change events are now processed by a TreeConfigurationChangeHandler and propagated to the treeModelChanged(ConfigurationNode) method.
        The underlying configuration has changed. This method tries to translate the configuration event into a tree model event. This involves finding the highest configuration node in the hierarchy affected by this event. In most cases this will not be possible because the configuration event typically won't contain enough information. Then a generic structure changed event for the root node is fired.
        Specified by:
        configurationChanged in interface org.apache.commons.configuration.event.ConfigurationListener
        Parameters:
        event - the event
      • treeModelChanged

        public void treeModelChanged​(org.apache.commons.configuration.tree.ConfigurationNode node)
        The configuration serving as tree model was changed in the sub tree referenced by the passed in node. This implementation fires a tree structure change event to all registered listeners.
        Specified by:
        treeModelChanged in interface net.sf.jguiraffe.gui.builder.components.model.TreeModelChangeListener
        Parameters:
        node - the node in the configuration which has changed
        Since:
        1.3