1 32 33 package it.businesslogic.ireport.util; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 42 public class TreeNode { 43 44 private List childs = new ArrayList (); 45 private Object userObject = null; 46 private TreeNode parent = null; 47 48 49 public TreeNode(Object aUserObject) { 50 setUserObject( aUserObject ); 51 } 52 53 public String getName() 54 { 55 return "" + getUserObject(); 56 } 57 58 public List getChilds() { 59 return childs; 60 } 61 62 public boolean isLeaf() { 63 return getChilds().isEmpty(); 64 } 65 66 public Object getUserObject() { 67 return userObject; 68 } 69 70 public void setUserObject(Object userObject) { 71 this.userObject = userObject; 72 } 73 74 public void addChild(TreeNode node) 75 { 76 node.setParent( this ); 77 getChilds().add(node); 78 } 79 80 public void addChilds(Collection nodes) 81 { 82 Iterator myIterator = nodes.iterator(); 83 while (myIterator.hasNext()) 84 { 85 addChild( (TreeNode)(myIterator.next())); 86 } 87 } 88 89 90 public boolean moveUp( Object userObject) 91 { 92 if ( ((TreeNode)(getChilds().get(0))).getUserObject() == userObject ) return true; 93 else 94 { 95 for (int i=1; i<getChilds().size(); ++i) 96 { 97 TreeNode node = (TreeNode)(getChilds().get(i)); 98 if (node.getUserObject() == userObject) 99 { 100 int oldPosition = i; 101 getChilds().remove( i ); 102 getChilds().add( i-1, node ); 103 return true; 104 } 105 } 106 } 107 108 for (int i=0; i< getChilds().size(); ++i) 110 { 111 TreeNode parent = (TreeNode)getChilds().get(i); 112 if (parent.isLeaf()) continue; 113 else 114 { 115 if (parent.moveUp( userObject )) return true; 116 } 117 } 118 119 return false; 120 } 121 122 public boolean moveDown(Object userObject) 123 { 124 if ( ((TreeNode)(getChilds().get(getChilds().size()-1))).getUserObject() == userObject ) return true; 125 else 126 { 127 for (int i=0; i<getChilds().size()-1; ++i) 128 { 129 TreeNode node = (TreeNode)(getChilds().get(i)); 130 if (node.getUserObject() == userObject) 131 { 132 int oldPosition = i; 133 getChilds().remove( i ); 134 getChilds().add( i+1, node ); 135 return true; 136 } 137 } 138 } 139 140 for (int i=0; i< getChilds().size(); ++i) 142 { 143 TreeNode parent = (TreeNode)getChilds().get(i); 144 if (parent.isLeaf()) continue; 145 else 146 { 147 if (parent.moveDown( userObject )) return true; 148 } 149 } 150 151 return false; 152 } 153 154 public TreeNode getParent() { 155 return parent; 156 } 157 158 public void setParent(TreeNode parent) { 159 this.parent = parent; 160 } 161 162 } 163 | Popular Tags |