KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserapplications > filterbuilder > HtmlTreeModel


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2005 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserapplications/filterbuilder/HtmlTreeModel.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:43:06 $
10
// $Revision: 1.1 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.parserapplications.filterbuilder;
28
29 import java.util.Vector JavaDoc;
30
31 import javax.swing.tree.*;
32 import javax.swing.event.*;
33
34 import org.htmlparser.Node;
35 import org.htmlparser.tags.Html;
36 import org.htmlparser.util.NodeList;
37
38 /**
39  * Each leaf is a TreeItem or something that does toString().
40  */

41 public class HtmlTreeModel implements TreeModel
42 {
43     protected Vector JavaDoc mTreeListeners;
44     protected Node mRoot;
45     
46     public HtmlTreeModel (NodeList root)
47     {
48         mTreeListeners = new Vector JavaDoc ();
49         // for simplicity we encapsulate the nodelist in a Html tag
50
mRoot = new Html ();
51         mRoot.setChildren (root);
52     }
53
54     //
55
// TreeModel interface
56
//
57

58     // Adds a listener for the TreeModelEvent posted after the tree changes.
59
public void addTreeModelListener (TreeModelListener l)
60     {
61         synchronized (mTreeListeners)
62         {
63             if (!mTreeListeners.contains(l))
64                 mTreeListeners.addElement(l);
65         }
66     }
67
68     // Removes a listener previously added with addTreeModelListener().
69
public void removeTreeModelListener(TreeModelListener l)
70     {
71         synchronized (mTreeListeners)
72         {
73             mTreeListeners.removeElement (l);
74         }
75     }
76
77     // Returns the child of parent at index index in the parent's child array.
78
public Object JavaDoc getChild (Object JavaDoc parent, int index)
79     {
80         Node node;
81         NodeList list;
82         Object JavaDoc ret;
83
84         node = (Node)parent;
85         list = node.getChildren ();
86         if (null == list)
87             throw new IllegalArgumentException JavaDoc ("invalid parent for getChild()");
88         else
89             ret = list.elementAt (index);
90         
91         return (ret);
92     }
93
94     // Returns the number of children of parent.
95
public int getChildCount (Object JavaDoc parent)
96     {
97         Node node;
98         NodeList list;
99         int ret;
100
101         ret = 0;
102
103         node = (Node)parent;
104         list = node.getChildren ();
105         if (null != list)
106             ret = list.size ();
107         
108         return (ret);
109     }
110
111
112     // Returns the index of child in parent.
113
public int getIndexOfChild (Object JavaDoc parent, Object JavaDoc child)
114     {
115         Node node;
116         NodeList list;
117         int count;
118         int ret;
119
120         ret = -1;
121
122         node = (Node)parent;
123         list = node.getChildren ();
124         if (null != list)
125         {
126             count = list.size ();
127             for (int i = 0; i < count; i++)
128                 if (child == list.elementAt (i))
129                 {
130                     ret = i;
131                     break;
132                 }
133         }
134         else
135             throw new IllegalArgumentException JavaDoc ("invalid parent for getIndexOfChild()");
136
137         if (0 > ret)
138             throw new IllegalArgumentException JavaDoc ("child not found in getIndexOfChild()");
139
140         return (ret);
141     }
142
143     // Returns the root of the tree.
144
public Object JavaDoc getRoot ()
145     {
146         return (mRoot);
147     }
148
149     // Returns true if node is a leaf.
150
public boolean isLeaf (Object JavaDoc node)
151     {
152         NodeList list;
153         boolean ret;
154
155         list = ((Node)node).getChildren ();
156         if (null == list)
157             ret = true;
158         else
159             ret = 0 == list.size ();
160
161         return (ret);
162     }
163
164     // Messaged when the user has altered the value for the item identified by path to newValue.
165
public void valueForPathChanged (TreePath path, Object JavaDoc newValue)
166     {
167         TreeModelEvent event;
168         Vector JavaDoc v;
169
170         event = new TreeModelEvent (this, path);
171         synchronized (mTreeListeners)
172         {
173             v = (Vector JavaDoc)mTreeListeners.clone ();
174         }
175         
176         for (int i = 0; i < v.size (); i++)
177         {
178             TreeModelListener listener = (TreeModelListener)v.elementAt (i);
179             listener.treeStructureChanged (event);
180         }
181     }
182 }
183
Popular Tags