1 19 package org.netbeans.tax; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 25 31 public abstract class TreeParentNode extends TreeChild { 32 33 34 public static final String PROP_CHILD_LIST = "childList"; 36 37 private TreeObjectList childList; 38 39 40 44 45 protected TreeParentNode () { 46 super (); 47 48 this.childList = new TreeObjectList (createChildListContentManager ()); 49 } 50 51 52 protected TreeParentNode (TreeParentNode parentNode, boolean deep) { 53 super (parentNode); 54 55 this.childList = new TreeObjectList (createChildListContentManager ()); 56 if (deep) { 57 this.childList.addAll ((TreeObjectList)parentNode.childList.clone ()); 58 } 59 } 60 61 62 66 69 public abstract Object clone (boolean deep); 70 71 72 75 public final Object clone () { 76 return clone (true); 77 } 78 79 81 public boolean equals (Object object, boolean deep) { 82 if (!!! super.equals (object, deep)) 83 return false; 84 85 TreeParentNode peer = (TreeParentNode) object; 86 if (!!! Util.equals (this.childList, peer.childList)) { 87 return false; 88 } 89 90 return true; 91 } 92 93 96 public void merge (TreeObject treeObject) throws CannotMergeException { 97 super.merge (treeObject); 98 99 TreeParentNode peer = (TreeParentNode) treeObject; 100 101 childList.merge (peer.childList); 102 } 103 104 105 109 111 public boolean isAssignableChild (TreeChild child) { 112 return childList.isAssignableObject (child); 113 } 114 115 118 public final TreeObjectList getChildNodes () { 119 return childList; 120 } 121 122 123 127 128 130 protected void setReadOnly (boolean newReadOnly) { 131 super.setReadOnly (newReadOnly); 132 133 childList.setReadOnly (newReadOnly); 134 } 135 136 137 141 143 public final TreeChild getFirstChild () { 144 if ( childList.size () == 0 ) { 145 return null; 146 } 147 return (TreeChild)childList.get (0); 148 } 149 150 152 public final TreeChild getLastChild () { 153 if ( childList.size () == 0 ) { 154 return null; 155 } 156 return (TreeChild)childList.get (childList.size () - 1); 157 } 158 159 160 163 public final void insertBefore (TreeChild newChild, TreeChild refChild) throws ReadOnlyException { 164 171 childList.checkReadOnly (); 172 int index = childList.indexOf (refChild); 173 if (index < 0) { 174 return; 175 } 176 childList.add (index, newChild); 177 } 178 179 182 public final void replaceChild (TreeChild oldChild, TreeChild newChild) throws ReadOnlyException { 183 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\nTreeParentNode::replaceChild: oldChild = " + oldChild); if ( Util.THIS.isLoggable() ) Util.THIS.debug (" ::replaceChild: newChild = " + newChild); 186 childList.checkReadOnly (); 187 int index = childList.indexOf (oldChild); 188 189 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" ::replaceChild: childList [oldChild] = " + index); 191 if (index < 0) { 192 return; 193 } 194 childList.set (index, newChild); 195 } 196 197 200 public final void removeChild (TreeChild oldChild) throws ReadOnlyException { 201 childList.checkReadOnly (); 202 childList.remove (oldChild); 203 } 204 205 208 public final void appendChild (TreeChild newChild) throws ReadOnlyException { 209 childList.checkReadOnly (); 210 childList.add (newChild); 211 } 212 213 214 218 public final void insertChildAt (TreeChild child, int index) throws ReadOnlyException { 219 childList.checkReadOnly (); 220 childList.add (index, child); 221 } 222 223 224 226 public final int indexOf (TreeChild node) { 227 return childList.indexOf (node); 228 } 229 230 231 233 public final TreeChild item (int index) { 234 return (TreeChild)childList.get (index); 235 } 236 237 239 public final int getChildrenNumber () { 240 return (childList.size ()); 241 } 242 243 245 public final boolean hasChildNodes () { 246 return (!!! childList.isEmpty ()); 247 } 248 249 251 public final boolean hasChildNodes (Class childClass) { 252 return hasChildNodes (childClass, false); 253 } 254 255 257 public boolean hasChildNodes (Class childClass, boolean recursive) { 258 Iterator it = getChildNodes ().iterator (); 259 while (it.hasNext ()) { 260 TreeChild child = (TreeChild)it.next (); 261 262 264 if (childClass == null || childClass.isAssignableFrom (child.getClass ())) { 265 return true; 266 } 267 268 270 if ( recursive && (child instanceof TreeParentNode) ) { 271 if ( ((TreeParentNode)child).hasChildNodes (childClass, true) == true ) { 272 return true; 273 } 274 } 275 } 276 return false; 277 } 278 279 282 public final Collection getChildNodes (Class childClass) { 283 return getChildNodes (childClass, false); 284 } 285 286 289 public Collection getChildNodes (Class childClass, boolean recursive) { 290 291 293 Collection allChildNodes = new LinkedList (); 294 Iterator it = getChildNodes ().iterator (); 295 while (it.hasNext ()) { 296 TreeChild child = (TreeChild)it.next (); 297 298 300 if (childClass == null || childClass.isAssignableFrom (child.getClass ())) { 301 allChildNodes.add (child); 302 } 303 304 306 if ( recursive && (child instanceof TreeParentNode) ) { 307 allChildNodes.addAll (((TreeParentNode)child).getChildNodes (childClass, true)); 308 } 309 } 310 return allChildNodes; 311 } 312 313 314 318 320 protected abstract TreeObjectList.ContentManager createChildListContentManager (); 321 322 325 protected abstract class ChildListContentManager extends TreeObjectList.ContentManager { 326 327 329 public void checkAssignableObject (Object obj) { 330 super.checkAssignableObject (obj); 331 checkAssignableClass (TreeChild.class, obj); 332 } 333 334 336 public void objectInserted (TreeObject obj) { 337 ((TreeChild)obj).setParentNode (TreeParentNode.this); 338 TreeParentNode.this.firePropertyChange (TreeParentNode.PROP_CHILD_LIST, TreeParentNode.this.childList, obj); 339 } 340 341 343 public void objectRemoved (TreeObject obj) { 344 ((TreeChild)obj).setParentNode (null); 345 TreeParentNode.this.firePropertyChange (TreeParentNode.PROP_CHILD_LIST, TreeParentNode.this.childList, obj); 346 } 347 348 350 public void orderChanged (int[] permutation) { 351 TreeParentNode.this.firePropertyChange (TreeParentNode.PROP_CHILD_LIST, TreeParentNode.this.childList, permutation); 352 } 353 354 } 356 } 357 | Popular Tags |