KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DynamicTreeNode


1 /*
2  * @(#)DynamicTreeNode.java 1.13 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)DynamicTreeNode.java 1.13 05/11/17
39  */

40
41 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
42 import java.awt.Color JavaDoc;
43 import java.awt.Font JavaDoc;
44 import java.awt.Toolkit JavaDoc;
45 import java.util.Random JavaDoc;
46
47 /**
48   * DynamicTreeNode illustrates one of the possible ways in which dynamic
49   * loading can be used in tree. The basic premise behind this is that
50   * getChildCount() will be messaged from JTreeModel before any children
51   * are asked for. So, the first time getChildCount() is issued the
52   * children are loaded.<p>
53   * It should be noted that isLeaf will also be messaged from the model.
54   * The default behavior of TreeNode is to message getChildCount to
55   * determine this. As such, isLeaf is subclassed to always return false.<p>
56   * There are others ways this could be accomplished as well. Instead of
57   * subclassing TreeNode you could subclass JTreeModel and do the same
58   * thing in getChildCount(). Or, if you aren't using TreeNode you could
59   * write your own TreeModel implementation.
60   * Another solution would be to listen for TreeNodeExpansion events and
61   * the first time a node has been expanded post the appropriate insertion
62   * events. I would not recommend this approach though, the other two
63   * are much simpler and cleaner (and are faster from the perspective of
64   * how tree deals with it).
65   *
66   * NOTE: getAllowsChildren() can be messaged before getChildCount().
67   * For this example the nodes always allow children, so it isn't
68   * a problem, but if you do support true leaf nodes you may want
69   * to check for loading in getAllowsChildren too.
70   *
71   * @version 1.13 11/17/05
72   * @author Scott Violet
73   */

74
75 public class DynamicTreeNode extends DefaultMutableTreeNode JavaDoc
76 {
77     // Class stuff.
78
/** Number of names. */
79     static protected float nameCount;
80
81     /** Names to use for children. */
82     static protected String JavaDoc[] names;
83
84     /** Potential fonts used to draw with. */
85     static protected Font JavaDoc[] fonts;
86
87     /** Used to generate the names. */
88     static protected Random JavaDoc nameGen;
89
90     /** Number of children to create for each node. */
91     static protected final int DefaultChildrenCount = 7;
92
93     static {
94     String JavaDoc[] fontNames;
95
96     try {
97         fontNames = Toolkit.getDefaultToolkit().getFontList();
98     } catch (Exception JavaDoc e) {
99         fontNames = null;
100     }
101     if(fontNames == null || fontNames.length == 0) {
102         names = new String JavaDoc[] {"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         /* Create the Fonts, creating fonts is slow, much better to
114            do it once. */

115         int fontSize = 12;
116
117         names = fontNames;
118         fonts = new Font JavaDoc[names.length];
119         for(int counter = 0, maxCounter = names.length;
120         counter < maxCounter; counter++) {
121         try {
122             fonts[counter] = new Font JavaDoc(fontNames[counter], 0, fontSize);
123         }
124         catch (Exception JavaDoc e) {
125             fonts[counter] = null;
126         }
127         fontSize = ((fontSize + 2 - 12) % 12) + 12;
128         }
129     }
130     nameCount = (float)names.length;
131     nameGen = new Random JavaDoc(System.currentTimeMillis());
132     }
133
134
135     /** Have the children of this node been loaded yet? */
136     protected boolean hasLoaded;
137
138     /**
139       * Constructs a new DynamicTreeNode instance with o as the user
140       * object.
141       */

142     public DynamicTreeNode(Object JavaDoc o) {
143     super(o);
144     }
145
146     public boolean isLeaf() {
147     return false;
148     }
149
150     /**
151       * If hasLoaded is false, meaning the children have not yet been
152       * loaded, loadChildren is messaged and super is messaged for
153       * the return value.
154       */

155     public int getChildCount() {
156     if(!hasLoaded) {
157         loadChildren();
158     }
159     return super.getChildCount();
160     }
161
162     /**
163       * Messaged the first time getChildCount is messaged. Creates
164       * children with random names from names.
165       */

166     protected void loadChildren() {
167     DynamicTreeNode newNode;
168     Font JavaDoc 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         /* Don't use add() here, add calls insert(newNode, getChildCount())
185            so if you want to use add, just be sure to set hasLoaded = true
186            first. */

187         insert(newNode, counter);
188     }
189     /* This node has now been loaded, mark it so. */
190     hasLoaded = true;
191     }
192 }
193
Popular Tags