1 13 package com.tonbeller.wcf.tree; 14 15 import org.apache.log4j.Logger; 16 17 22 public class TestTreeModel extends AbstractTreeModel { 23 private static final Logger logger = Logger.getLogger(TestTreeModel.class); 24 private int[] childCount; 25 Node[] roots; 26 27 public TestTreeModel() { 28 this(new int[] { 3, 4, 4, 4, 4, 4}); 29 } 30 31 public TestTreeModel(int[] childCount) { 32 this.childCount = childCount; 33 34 char c = 'A'; 35 roots = new Node[childCount[0]]; 36 for (int i = 0; i < roots.length; i++) { 37 roots[i] = new Node(null, "" + (char) (c + i), 1); 38 } 39 } 40 41 public class Node { 42 43 Node parent; 44 String name; 45 int level; 46 Node[] children; 47 48 public Node(Node parent, String name, int level) { 49 this.parent = parent; 50 this.name = name; 51 this.level = level; 52 if (level < childCount.length) { 53 children = new Node[childCount[level]]; 54 for (int i = 0; i < children.length; i++) { 55 children[i] = new Node(this, name + "[" + i + "]", level + 1); 56 } 57 } 58 } 59 60 boolean hasChildren() { 61 return children != null; 62 } 63 64 Object [] getChildren() { 65 return children; 66 } 67 68 public int getLevel() { 69 return level; 70 } 71 72 public String getName() { 73 return name; 74 } 75 76 public void setLevel(int level) { 77 this.level = level; 78 } 79 80 public void setName(String name) { 81 this.name = name; 82 } 83 84 public Node getParent() { 85 return parent; 86 } 87 88 public void setParent(Node parent) { 89 this.parent = parent; 90 } 91 92 public String toString() { 93 return name; 94 } 95 } 96 97 public Object [] getRoots() { 98 logger.info("getRoots"); 99 return roots; 100 } 101 102 public boolean hasChildren(Object node) { 103 logger.info("hasChildren: " + node); 104 return ((Node) node).hasChildren(); 105 } 106 107 public Object [] getChildren(Object node) { 108 logger.info("getChildren: " + node); 109 return ((Node) node).getChildren(); 110 } 111 112 public Object getParent(Object node) { 113 logger.info("getParent: " + node); 114 return ((Node) node).getParent(); 115 } 116 117 public DeleteNodeModel getDeleteNodeModel() { 118 NodeFilter nf = new NodeFilter() { 119 public boolean accept(Object node) { 120 return ((Node) node).getName().startsWith("A"); 121 } 122 }; 123 DefaultDeleteNodeModel ddnm = new DefaultDeleteNodeModel(); 124 ddnm.setDeletableFilter(nf); 125 return ddnm; 126 } 127 } | Popular Tags |