Class SwingConfigurationTreeModel
- java.lang.Object
-
- net.sf.jguiraffe.gui.platform.swing.builder.components.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 aConfiguration
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()
orgetChild()
) are implemented by directly forwarding to methods provided by theConfigurationNode
interface. All of these methods expect that a passed in node (which is of typeObject
in theTreeModel
interface) can be cast into aConfigurationNode
.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 Summary
Constructors Constructor Description SwingConfigurationTreeModel(org.apache.commons.configuration.HierarchicalConfiguration config)
Creates a new instance ofSwingConfigurationTreeModel
and initializes it with the givenHierarchicalConfiguration
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method 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.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.
-
-
-
Constructor Detail
-
SwingConfigurationTreeModel
public SwingConfigurationTreeModel(org.apache.commons.configuration.HierarchicalConfiguration config)
Creates a new instance ofSwingConfigurationTreeModel
and initializes it with the givenHierarchicalConfiguration
object. Behind the scenes, aTreeConfigurationChangeHandler
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
-
addTreeModelListener
public void addTreeModelListener(TreeModelListener l)
Adds an event listener to this model. The listener must not be null.- Specified by:
addTreeModelListener
in interfaceTreeModel
- Parameters:
l
- the listener to add- Throws:
IllegalArgumentException
- if the listener is null
-
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 typeConfigurationNode
.
-
getChildCount
public int getChildCount(Object node)
Returns the number of child nodes of the specified node. This implementation expects that the node is of typeConfigurationNode
.- Specified by:
getChildCount
in interfaceTreeModel
- 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 typeConfigurationNode
. 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 interfaceTreeModel
- Parameters:
parent
- the parent nodechild
- 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.
-
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 aConfigurationNode
. It then checks whether it has children.
-
removeTreeModelListener
public void removeTreeModelListener(TreeModelListener l)
Removes the specified event listener from this model.- Specified by:
removeTreeModelListener
in interfaceTreeModel
- 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 interfaceTreeModel
- Parameters:
path
- the path to the changed nodenewValue
- 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 aTreeConfigurationChangeHandler
and propagated to thetreeModelChanged(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 interfaceorg.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 interfacenet.sf.jguiraffe.gui.builder.components.model.TreeModelChangeListener
- Parameters:
node
- the node in the configuration which has changed- Since:
- 1.3
-
-