1 20 21 package com.methodhead.tree; 22 23 import javax.swing.tree.DefaultMutableTreeNode ; 24 import java.util.Enumeration ; 25 import com.methodhead.MhfException; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.ArrayList ; 30 import java.util.Comparator ; 31 32 36 public class Tree { 37 38 40 42 public static final String POSITION_UNDER = "POSITION_UNDER"; 43 public static final String POSITION_AFTER = "POSITION_AFTER"; 44 public static final String POSITION_BEFORE = "POSITION_BEFORE"; 45 46 public static final String INVALIDMOVE_CANNOTMOVEROOT = 47 "INVALIDMOVE_CANNOTMOVEROOT"; 48 public static final String INVALIDMOVE_CANNOTMOVEBEFOREROOT = 49 "INVALIDMOVE_CANNOTMOVEBEFOREROOT"; 50 public static final String INVALIDMOVE_CANNOTMOVEAFTERROOT = 51 "INVALIDMOVE_CANNOTMOVEAFTERROOT"; 52 public static final String INVALIDMOVE_CANNOTMOVENEARSELF = 53 "INVALIDMOVE_CANNOTMOVENEARSELF"; 54 public static final String INVALIDMOVE_CANNOTMOVEUNDERSELF = 55 "INVALIDMOVE_CANNOTMOVEUNDERSELF"; 56 57 public static final String VALIDMOVE = "VALIDMOVE"; 58 59 61 63 66 public DefaultMutableTreeNode getRoot() { 67 return root_; 68 } 69 70 73 public void setRoot( 74 DefaultMutableTreeNode link ) { 75 root_ = link; 76 } 77 78 81 public void insertUnder( 82 DefaultMutableTreeNode dest, 83 DefaultMutableTreeNode node ) { 84 85 dest.add( node ); 86 } 87 88 92 public void insertAfter( 93 DefaultMutableTreeNode dest, 94 DefaultMutableTreeNode node ) { 95 96 if ( dest == root_ ) 97 throw new MhfException( "Can't insert after the root node." ); 98 99 DefaultMutableTreeNode p = ( DefaultMutableTreeNode )dest.getParent(); 100 p.insert( node, p.getIndex( dest ) + 1 ); 101 } 102 103 107 public void insertBefore( 108 DefaultMutableTreeNode dest, 109 DefaultMutableTreeNode node ) { 110 111 if ( dest == root_ ) 112 throw new MhfException( 113 "Can't insert before the root node." ); 114 115 DefaultMutableTreeNode p = ( DefaultMutableTreeNode )dest.getParent(); 116 p.insert( node, p.getIndex( dest ) ); 117 } 118 119 122 public void remove( 123 DefaultMutableTreeNode node ) { 124 125 if ( node == root_ ) 126 root_ = null; 127 else 128 node.removeFromParent(); 129 130 } 131 132 137 public void removeAndPromoteChildren( 138 DefaultMutableTreeNode node ) { 139 140 DefaultMutableTreeNode tmp = null; 141 DefaultMutableTreeNode tmp2 = null; 142 143 if ( node == root_ ) { 144 if ( root_.getChildCount() == 0 ) 145 root_ = null; 146 147 else if ( root_.getChildCount() == 1 ) { 148 tmp = ( DefaultMutableTreeNode )root_.getChildAt( 0 ); 149 root_.remove( tmp ); 150 root_ = tmp; 151 } 152 else { 153 tmp = ( DefaultMutableTreeNode )root_.getChildAt( 0 ); 154 root_.remove( tmp ); 155 156 for ( int i = root_.getChildCount() - 1; i >= 0; i-- ) { 157 tmp2 = ( DefaultMutableTreeNode )root_.getChildAt( i ); 158 root_.remove( tmp2 ); 159 tmp.insert( tmp2, 0 ); 160 } 161 162 root_ = tmp; 163 } 164 } 165 else { 166 for ( int i = 0; i < node.getChildCount(); i++ ) { 167 ( ( DefaultMutableTreeNode )node.getParent() ).add( 168 ( DefaultMutableTreeNode )node.getChildAt( i ) ); 169 } 170 171 node.removeFromParent(); 172 } 173 } 174 175 181 public String validateMove( 182 DefaultMutableTreeNode node, 183 DefaultMutableTreeNode dest, 184 String position ) { 185 186 if ( node == root_ ) 187 return INVALIDMOVE_CANNOTMOVEROOT; 188 189 if ( ( dest == root_ ) && POSITION_BEFORE.equals( position ) ) 190 return INVALIDMOVE_CANNOTMOVEBEFOREROOT; 191 192 if ( ( dest == root_ ) && POSITION_AFTER.equals( position ) ) 193 return INVALIDMOVE_CANNOTMOVEAFTERROOT; 194 195 if ( dest == node ) 196 return INVALIDMOVE_CANNOTMOVENEARSELF; 197 198 if ( dest.isNodeAncestor( node ) ) 199 return INVALIDMOVE_CANNOTMOVEUNDERSELF; 200 201 return VALIDMOVE; 202 } 203 204 208 public void moveUnder( 209 DefaultMutableTreeNode dest, 210 DefaultMutableTreeNode node ) { 211 212 if ( dest.isNodeAncestor( node ) ) 213 throw new MhfException( "Can't move node under itself." ); 214 215 node.removeFromParent(); 216 dest.add( node ); 217 } 218 219 224 public void moveAfter( 225 DefaultMutableTreeNode dest, 226 DefaultMutableTreeNode node ) { 227 228 if ( dest == root_ ) 229 throw new MhfException( "Can't move node after root." ); 230 231 if ( dest.isNodeAncestor( node ) ) 232 throw new MhfException( "Can't move node under itself." ); 233 234 node.removeFromParent(); 235 insertAfter( dest, node ); 236 } 237 238 243 public void moveBefore( 244 DefaultMutableTreeNode dest, 245 DefaultMutableTreeNode node ) { 246 247 if ( dest == root_ ) 248 throw new MhfException( "Can't move node after root." ); 249 250 if ( dest.isNodeAncestor( node ) ) 251 throw new MhfException( "Can't move node under itself." ); 252 253 node.removeFromParent(); 254 insertBefore( dest, node ); 255 } 256 257 262 public static void sort( 263 DefaultMutableTreeNode node, 264 Comparator comparator ) { 265 266 if ( node.getChildCount() == 0 ) 270 return; 271 272 List list = new ArrayList (); 273 for ( int i = 0; i < node.getChildCount(); i++ ) { 274 DefaultMutableTreeNode n = ( DefaultMutableTreeNode )node.getChildAt( i ); 275 n.removeFromParent(); 276 list.add( n ); 277 } 278 279 Collections.sort( list, comparator ); 280 281 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 282 DefaultMutableTreeNode n = ( DefaultMutableTreeNode )iter.next(); 283 node.add( n ); 284 } 285 } 286 287 291 public static DefaultMutableTreeNode copy( 292 DefaultMutableTreeNode node ) { 293 294 DefaultMutableTreeNode n = ( DefaultMutableTreeNode )node.clone(); 295 296 for ( int i = 0; i < node.getChildCount(); i++ ) 297 n.add( copy( ( DefaultMutableTreeNode )node.getChildAt( i ) ) ); 298 299 return n; 300 } 301 302 304 306 protected DefaultMutableTreeNode root_ = null; 307 } 308 | Popular Tags |