1 27 package org.htmlparser.parserapplications.filterbuilder; 28 29 import java.util.Vector ; 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 41 public class HtmlTreeModel implements TreeModel 42 { 43 protected Vector mTreeListeners; 44 protected Node mRoot; 45 46 public HtmlTreeModel (NodeList root) 47 { 48 mTreeListeners = new Vector (); 49 mRoot = new Html (); 51 mRoot.setChildren (root); 52 } 53 54 58 public void addTreeModelListener (TreeModelListener l) 60 { 61 synchronized (mTreeListeners) 62 { 63 if (!mTreeListeners.contains(l)) 64 mTreeListeners.addElement(l); 65 } 66 } 67 68 public void removeTreeModelListener(TreeModelListener l) 70 { 71 synchronized (mTreeListeners) 72 { 73 mTreeListeners.removeElement (l); 74 } 75 } 76 77 public Object getChild (Object parent, int index) 79 { 80 Node node; 81 NodeList list; 82 Object ret; 83 84 node = (Node)parent; 85 list = node.getChildren (); 86 if (null == list) 87 throw new IllegalArgumentException ("invalid parent for getChild()"); 88 else 89 ret = list.elementAt (index); 90 91 return (ret); 92 } 93 94 public int getChildCount (Object 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 public int getIndexOfChild (Object parent, Object 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 ("invalid parent for getIndexOfChild()"); 136 137 if (0 > ret) 138 throw new IllegalArgumentException ("child not found in getIndexOfChild()"); 139 140 return (ret); 141 } 142 143 public Object getRoot () 145 { 146 return (mRoot); 147 } 148 149 public boolean isLeaf (Object 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 public void valueForPathChanged (TreePath path, Object newValue) 166 { 167 TreeModelEvent event; 168 Vector v; 169 170 event = new TreeModelEvent (this, path); 171 synchronized (mTreeListeners) 172 { 173 v = (Vector )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 |