KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > test > NodeTreeModel


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Oct 22, 2005
23  */

24 package org.lobobrowser.html.test;
25
26 import javax.swing.event.TreeModelListener JavaDoc;
27 import javax.swing.tree.*;
28
29 import org.w3c.dom.*;
30
31 class NodeTreeModel implements TreeModel {
32     private final Node rootNode;
33     
34     /**
35      * @param node
36      */

37     public NodeTreeModel(Node node) {
38         super();
39         rootNode = node;
40     }
41
42     public Object JavaDoc getRoot() {
43         return this.rootNode;
44     }
45
46     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
47         Node parentNode = (Node) parent;
48         return parentNode == null ? null : parentNode.getChildNodes().item(index);
49     }
50
51     public int getChildCount(Object JavaDoc parent) {
52         Node parentNode = (Node) parent;
53         return parentNode == null ? 0 : parentNode.getChildNodes().getLength();
54     }
55
56     public boolean isLeaf(Object JavaDoc node) {
57         if(node == this.rootNode) {
58             return false;
59         }
60         Node domNode = (Node) node;
61         return domNode == null ? true : domNode.getChildNodes().getLength() == 0;
62     }
63
64     public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
65     }
66
67     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
68         Node parentNode = (Node) parent;
69         NodeList nodeList = parentNode == null ? null : parentNode.getChildNodes();
70         if(nodeList == null) {
71             return -1;
72         }
73         int length = nodeList.getLength();
74         for(int i = 0; i < length; i++) {
75             if(nodeList.item(i) == child) {
76                 return i;
77             }
78         }
79         return -1;
80     }
81
82     public void addTreeModelListener(TreeModelListener JavaDoc l) {
83         // nop
84
}
85
86     public void removeTreeModelListener(TreeModelListener JavaDoc l) {
87         // nop
88
}
89 }
90
Popular Tags