001 /*
002 * This file is part of the Echo Point Project. This project is a
003 * collection of Components that have extended the Echo Web Application
004 * Framework Version 3.
005 *
006 * Version: MPL 1.1
007 *
008 * The contents of this file are subject to the Mozilla Public License Version
009 * 1.1 (the "License"); you may not use this file except in compliance with
010 * the License. You may obtain a copy of the License at
011 * http://www.mozilla.org/MPL/
012 *
013 * Software distributed under the License is distributed on an "AS IS" basis,
014 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015 * for the specific language governing rights and limitations under the
016 * License.
017 */
018
019 package echopoint.tree;
020
021 import nextapp.echo.extras.app.tree.AbstractTreeModel;
022
023 /**
024 * A simple tree model that uses {@link TreeNode}s.
025 *
026 * @author Brad Baker, Rakesh 2009-05-29
027 * @version $Id: DefaultTreeModel.java 248 2009-10-19 14:20:53Z sptrakesh $
028 * @since 3.0.0b3
029 */
030 public class DefaultTreeModel extends AbstractTreeModel
031 {
032 private static final long serialVersionUID = 1l;
033
034 /** Root of the tree. */
035 protected TreeNode root;
036
037 /**
038 * Creates a tree with the specified root node.
039 *
040 * @param root a TreeNode object that is the root of the tree
041 */
042 public DefaultTreeModel( final TreeNode root )
043 {
044 this.root = root;
045 }
046
047 /**
048 * Returns the child of <I>parent </I> at index <I>index </I> in the
049 * parent's child array. <I>parent </I> must be a node previously obtained
050 * from this data source. This should not return null if <i>index </i> is a
051 * valid index for <i>parent </i> (that is <i>index </i>>= 0 && <i>index
052 * </i>< getChildCount( <i>parent </i>)).
053 *
054 * @param parent a node in the tree, obtained from this data source
055 * @return the child of <I>parent </I> at index <I>index </I>
056 */
057 public Object getChild( final Object parent, final int index )
058 {
059 return ( (TreeNode) parent ).getChildAt( index );
060 }
061
062 /**
063 * Returns the number of children of <I>parent </I>. Returns 0 if the node
064 * is a leaf or if it has no children. <I>parent </I> must be a node
065 * previously obtained from this data source.
066 *
067 * @param parent a node in the tree, obtained from this data source
068 * @return the number of children of the node <I>parent </I>
069 */
070 public int getChildCount( final Object parent )
071 {
072 return ( (TreeNode) parent ).getChildCount();
073 }
074
075 /** {@inheritDoc} */
076 public int getColumnCount()
077 {
078 // TODO Auto-generated method stub
079 return 1;
080 }
081
082 /** Returns the index of child in parent. */
083 public int getIndexOfChild( final Object parent, final Object child )
084 {
085 if ( parent == null || child == null ) return 0;
086 return ( (TreeNode) parent ).getIndex( (TreeNode) child );
087 }
088
089 /**
090 * Returns the root of the tree. Returns null only if the tree has no
091 * nodes.
092 *
093 * @return the root of the tree
094 */
095 public Object getRoot()
096 {
097 return root;
098 }
099
100 /** {@inheritDoc} */
101 public Object getValueAt( final Object node, final int column )
102 {
103 return node.toString();
104 }
105
106 /**
107 * Returns whether the specified node is a leaf node. The way the test is
108 * performed depends on the <code>askAllowsChildren</code> setting.
109 */
110 public boolean isLeaf( final Object node )
111 {
112 return ( (TreeNode) node ).isLeaf();
113 }
114
115 public void addChild( final DefaultMutableTreeNode parent,
116 DefaultMutableTreeNode child )
117 {
118 parent.add( child );
119 fireTreeNodesInserted( parent, parent.getPath(),
120 new int[] { parent.getIndex( child ) }, new Object[] { child } );
121 }
122
123 public void removeChild( final DefaultMutableTreeNode parent,
124 DefaultMutableTreeNode child )
125 {
126 final int index = parent.getIndex( child );
127 if ( index == -1 ) return;
128
129 parent.remove( child );
130 fireTreeNodesRemoved( parent, parent.getPath(),
131 new int[] { index }, new Object[] { child } );
132 }
133 }