KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > tree > XmlTreeModel


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package salsa.tree;
24
25 import java.util.*;
26 import javax.swing.*;
27 import javax.swing.event.*;
28 import javax.swing.tree.*;
29 import org.jdom.*;
30 import houston.*;
31
32 public class XmlTreeModel implements TreeModel
33 {
34    static Logger T = Logger.getLogger( XmlTreeModel.class );
35
36    EventListenerList _listener = new EventListenerList();
37
38    private Element _root;
39
40    public XmlTreeModel( Document doc )
41    {
42       _root = doc.getRootElement();
43    }
44
45    public Object JavaDoc getChild( Object JavaDoc parent, int index )
46    {
47       Element el = ( Element ) parent;
48
49       List children = el.getChildren();
50       return children.get( index );
51    }
52
53    public int getChildCount( Object JavaDoc parent )
54    {
55       Element el = ( Element ) parent;
56
57       List children = el.getChildren();
58       return children.size();
59    }
60
61    public int getIndexOfChild( Object JavaDoc parent, Object JavaDoc child )
62    {
63       Element el = ( Element ) parent;
64
65       List children = el.getChildren();
66       return children.indexOf( child );
67    }
68
69    public Object JavaDoc getRoot()
70    {
71       return _root;
72    }
73
74    public boolean isLeaf( Object JavaDoc parent )
75    {
76       Element el = ( Element ) parent;
77
78       List children = el.getChildren();
79       return children.size() == 0;
80    }
81
82    public void addTreeModelListener( TreeModelListener l )
83    {
84       _listener.add( TreeModelListener.class, l );
85    }
86
87    public void removeTreeModelListener( TreeModelListener l )
88    {
89       _listener.remove( TreeModelListener.class, l );
90    }
91
92    public void valueForPathChanged( TreePath path, Object JavaDoc newValue )
93    {
94       // only needed if you edit/change tree nodes in Jtree itself which we don't do/support
95
}
96
97 }
98
Popular Tags