1 36 37 40 41 import javax.swing.tree.DefaultMutableTreeNode ; 42 import java.awt.Color ; 43 import java.awt.Font ; 44 import java.awt.Toolkit ; 45 import java.util.Random ; 46 47 74 75 public class DynamicTreeNode extends DefaultMutableTreeNode  76 { 77 79 static protected float nameCount; 80 81 82 static protected String [] names; 83 84 85 static protected Font [] fonts; 86 87 88 static protected Random nameGen; 89 90 91 static protected final int DefaultChildrenCount = 7; 92 93 static { 94 String [] fontNames; 95 96 try { 97 fontNames = Toolkit.getDefaultToolkit().getFontList(); 98 } catch (Exception e) { 99 fontNames = null; 100 } 101 if(fontNames == null || fontNames.length == 0) { 102 names = new String [] {"Mark Andrews", "Tom Ball", "Alan Chung", 103 "Rob Davis", "Jeff Dinkins", 104 "Amy Fowler", "James Gosling", 105 "David Karlton", "Dave Kloba", 106 "Dave Moore", "Hans Muller", 107 "Rick Levenson", "Tim Prinzing", 108 "Chester Rose", "Ray Ryan", 109 "Georges Saab", "Scott Violet", 110 "Kathy Walrath", "Arnaud Weber" }; 111 } 112 else { 113 115 int fontSize = 12; 116 117 names = fontNames; 118 fonts = new Font [names.length]; 119 for(int counter = 0, maxCounter = names.length; 120 counter < maxCounter; counter++) { 121 try { 122 fonts[counter] = new Font (fontNames[counter], 0, fontSize); 123 } 124 catch (Exception e) { 125 fonts[counter] = null; 126 } 127 fontSize = ((fontSize + 2 - 12) % 12) + 12; 128 } 129 } 130 nameCount = (float)names.length; 131 nameGen = new Random (System.currentTimeMillis()); 132 } 133 134 135 136 protected boolean hasLoaded; 137 138 142 public DynamicTreeNode(Object o) { 143 super(o); 144 } 145 146 public boolean isLeaf() { 147 return false; 148 } 149 150 155 public int getChildCount() { 156 if(!hasLoaded) { 157 loadChildren(); 158 } 159 return super.getChildCount(); 160 } 161 162 166 protected void loadChildren() { 167 DynamicTreeNode newNode; 168 Font font; 169 int randomIndex; 170 SampleData data; 171 172 for(int counter = 0; counter < DynamicTreeNode.DefaultChildrenCount; 173 counter++) { 174 randomIndex = (int)(nameGen.nextFloat() * nameCount); 175 if(fonts != null) 176 font = fonts[randomIndex]; 177 else 178 font = null; 179 if(counter % 2 == 0) 180 data = new SampleData(font, Color.red, names[randomIndex]); 181 else 182 data = new SampleData(font, Color.blue, names[randomIndex]); 183 newNode = new DynamicTreeNode(data); 184 187 insert(newNode, counter); 188 } 189 190 hasLoaded = true; 191 } 192 } 193 | Popular Tags |