public class SwingConfigurationTreeModel extends Object implements TreeModel, org.apache.commons.configuration.event.ConfigurationListener, 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.)
Constructor and Description |
---|
SwingConfigurationTreeModel(org.apache.commons.configuration.HierarchicalConfiguration config)
Creates a new instance of
SwingConfigurationTreeModel and
initializes it with the given HierarchicalConfiguration
object. |
Modifier and Type | Method and Description |
---|---|
void |
addTreeModelListener(TreeModelListener l)
Adds an event listener to this model.
|
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. |
Object |
getChild(Object node,
int index)
Returns the child node of the specified node with the given index.
|
int |
getChildCount(Object node)
Returns the number of child nodes of the specified node.
|
org.apache.commons.configuration.HierarchicalConfiguration |
getConfiguration()
Returns the configuration object that stores the data of this model.
|
int |
getIndexOfChild(Object parent,
Object child)
Returns the index of the specified child node relative to its parent
node.
|
Object |
getRoot()
Returns the root node of this tree model.
|
boolean |
isLeaf(Object node)
Tests whether the passed in node is a leaf node.
|
void |
removeTreeModelListener(TreeModelListener l)
Removes the specified event listener from this model.
|
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.
|
void |
valueForPathChanged(TreePath path,
Object newValue)
The value of a node was changed.
|
public SwingConfigurationTreeModel(org.apache.commons.configuration.HierarchicalConfiguration config)
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.config
- the configuration (must not be null)IllegalArgumentException
- if a required parameter is missingpublic org.apache.commons.configuration.HierarchicalConfiguration getConfiguration()
public void addTreeModelListener(TreeModelListener l)
addTreeModelListener
in interface TreeModel
l
- the listener to addIllegalArgumentException
- if the listener is nullpublic Object getChild(Object node, int index)
ConfigurationNode
.public int getChildCount(Object node)
ConfigurationNode
.getChildCount
in interface TreeModel
node
- the nodepublic int getIndexOfChild(Object parent, Object child)
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.getIndexOfChild
in interface TreeModel
parent
- the parent nodechild
- the child nodepublic Object getRoot()
public boolean isLeaf(Object node)
ConfigurationNode
. It
then checks whether it has children.public void removeTreeModelListener(TreeModelListener l)
removeTreeModelListener
in interface TreeModel
l
- the listener to be removedpublic void valueForPathChanged(TreePath path, Object newValue)
valueForPathChanged
in interface TreeModel
path
- the path to the changed nodenewValue
- the new value@Deprecated public void configurationChanged(org.apache.commons.configuration.event.ConfigurationEvent event)
TreeConfigurationChangeHandler
and propagated to the treeModelChanged(ConfigurationNode)
method.configurationChanged
in interface org.apache.commons.configuration.event.ConfigurationListener
event
- the eventpublic void treeModelChanged(org.apache.commons.configuration.tree.ConfigurationNode node)
treeModelChanged
in interface TreeModelChangeListener
node
- the node in the configuration which has changedCopyright © 2016 The JGUIraffe Team. All rights reserved.