1 7 package com.tonbeller.wcf.tree; 8 9 class TestModel extends AbstractTreeModel { 10 N root; 11 TestModel(N root) { 12 this.root = root; 13 } 14 public Object [] getRoots() { 15 return new Object [] { root }; 16 } 17 public boolean hasChildren(Object node) { 18 return ((N) node).children.size() > 0; 19 } 20 public Object [] getChildren(Object node) { 21 return ((N) node).children.toArray(); 22 } 23 public Object getParent(Object node) { 24 return ((N) node).parent; 25 } 26 27 public static String print(TreeModel tm) { 28 StringBuffer sb = new StringBuffer (); 29 Object [] roots = tm.getRoots(); 30 print(tm, sb, roots); 31 return sb.toString(); 32 } 33 34 public void fireModelChanged() { 35 super.fireModelChanged(); 36 } 37 38 private static void print(TreeModel tm, StringBuffer sb, Object [] nodes) { 39 for (int i = 0; i < nodes.length; i++) { 40 N n = (N) nodes[i]; 41 sb.append(n.getLabel()); 42 if (tm.hasChildren(n)) { 43 sb.append("["); 44 print(tm, sb, tm.getChildren(n)); 45 sb.append("]"); 46 } 47 if (i < nodes.length - 1) 48 sb.append(","); 49 } 50 } 51 52 public N getRoot() { 53 return root; 54 } 55 56 public void setRoot(N n) { 57 root = n; 58 fireModelChanged(); 59 } 60 61 } | Popular Tags |