001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ExternalKTreeModel.java,v $
023   Revision 1.1  2004/05/31 07:30:26  markl
024   Final cleanup and bugfixes of kiwi.ui.model.
025
026   ----------------------------------------------------------------------------
027*/
028
029package kiwi.ui.model;
030
031import java.util.ArrayList;
032import javax.swing.Icon;
033
034/** An implementation of <code>KTreeModel</code> that obtains its data from
035 * an external data source.
036 *
037 * @author Mark Lindner
038 * @since Kiwi 2.0
039 */
040
041public class ExternalKTreeModel extends DefaultKTreeModel
042  {
043  /** The data source for this model. */
044  protected TreeDataSource source;
045
046  /** Construct a new <code>DefaultKTreeModel</code> with the given
047   * data source.
048   *
049   * @param source The data source that will provide nodes for this model.
050   */
051  
052  public ExternalKTreeModel(TreeDataSource source)
053    {
054    super();
055    
056    this.source = source;
057
058    reload(null);
059    }
060  
061  /** Reload the subtree rooted at the given node from the data source.
062   *
063   * @param node The root node of the subtree to reload, or <b>null</b> to
064   * reload the entire tree.
065   */
066
067  public void reload(Object node)
068    {
069    if(node == null)
070      {
071      rootNode = makeNode(source.getRoot(), null);
072      preloadChildren(rootNode.getObject());
073      
074      support.fireDataChanged();
075      }
076    else
077      {
078      TreeNode n = nodeForObject(node);
079      
080      if(n != null)
081        {
082        n.releaseChildren();
083        preloadChildren(node);
084        support.fireNodeStructureChanged(node);
085        }
086      }
087    }
088
089  /*
090   */
091  
092  public void setRoot(Object root)
093    {
094    throw(new ImmutableModelException());
095    }
096
097  /*
098   */
099
100  public void removeChildren(Object parent)
101    {
102    throw(new ImmutableModelException());
103    }
104
105  /*
106   */
107
108  public void removeChild(Object parent, int index)
109    {
110    throw(new ImmutableModelException());
111    }
112
113  /*
114   */
115
116  public void addChild(Object parent, Object node)
117    {
118    throw(new ImmutableModelException());
119    }
120
121  /*
122   */
123
124  public void addChild(Object parent, Object node, int index)
125    {
126    throw(new ImmutableModelException());
127    }
128
129  /*
130   */
131
132  public void updateNode(Object node)
133    {
134    throw(new ImmutableModelException());
135    }
136
137  /*
138   */
139  
140  public void updateChildren(Object parent)
141    {
142    throw(new ImmutableModelException());
143    }  
144
145  /*
146   */
147
148  public Icon getIcon(Object node, boolean isExpanded)
149    {
150    return(source.getIcon(node, isExpanded));
151    }
152
153  /*
154   */
155
156  public boolean isExpandable(Object node)
157    {
158    return(source.isExpandable(node));
159    }
160
161  /*
162   */
163
164  public String getLabel(Object node)
165    {
166    return(source.getLabel(node));
167    }
168
169  /*
170   */
171
172  public void releaseChildren(Object parent)
173    {
174    TreeNode p = nodeForObject(parent);
175    if(p != null)
176      p.releaseChildren();
177    }
178
179  /*
180   */
181
182  public Object getValueForProperty(Object node, String property)
183    {
184    return(source.getValueForProperty(node, property));
185    }
186
187  /*
188   */
189
190  protected void loadChildren(TreeNode node)
191    {
192    ArrayList list = new ArrayList();
193    
194    Object children[] = source.getChildren(node.getObject());
195    for(int i = 0; i < children.length; i++)
196      list.add(makeNode(children[i], node)); // this is UGLY!
197
198    node.setChildren(list);
199    }  
200  
201  }
202
203/* end of source file */