home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / SampleTree / SampleTree.jar / src / DynamicTreeNode.java next >
Encoding:
Java Source  |  2002-09-06  |  6.7 KB  |  192 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)DynamicTreeNode.java    1.9 02/06/13
  38.  */
  39.  
  40. import javax.swing.tree.DefaultMutableTreeNode;
  41. import java.awt.Color;
  42. import java.awt.Font;
  43. import java.awt.Toolkit;
  44. import java.util.Random;
  45.  
  46. /**
  47.   * DynamicTreeNode illustrates one of the possible ways in which dynamic
  48.   * loading can be used in tree.  The basic premise behind this is that
  49.   * getChildCount() will be messaged from JTreeModel before any children
  50.   * are asked for.  So, the first time getChildCount() is issued the
  51.   * children are loaded.<p>
  52.   * It should be noted that isLeaf will also be messaged from the model.
  53.   * The default behavior of TreeNode is to message getChildCount to
  54.   * determine this. As such, isLeaf is subclassed to always return false.<p>
  55.   * There are others ways this could be accomplished as well.  Instead of
  56.   * subclassing TreeNode you could subclass JTreeModel and do the same
  57.   * thing in getChildCount().  Or, if you aren't using TreeNode you could
  58.   * write your own TreeModel implementation.
  59.   * Another solution would be to listen for TreeNodeExpansion events and
  60.   * the first time a node has been expanded post the appropriate insertion
  61.   * events.  I would not recommend this approach though, the other two
  62.   * are much simpler and cleaner (and are faster from the perspective of
  63.   * how tree deals with it).
  64.   *
  65.   * NOTE: getAllowsChildren() can be messaged before getChildCount().
  66.   *       For this example the nodes always allow children, so it isn't
  67.   *       a problem, but if you do support true leaf nodes you may want
  68.   *       to check for loading in getAllowsChildren too.
  69.   *
  70.   * @version 1.9 06/13/02
  71.   * @author Scott Violet
  72.   */
  73.  
  74. public class DynamicTreeNode extends DefaultMutableTreeNode
  75. {
  76.     // Class stuff.
  77.     /** Number of names. */
  78.     static protected float                    nameCount;
  79.  
  80.     /** Names to use for children. */
  81.     static protected String[]                 names;
  82.  
  83.     /** Potential fonts used to draw with. */
  84.     static protected Font[]                   fonts;
  85.  
  86.     /** Used to generate the names. */
  87.     static protected Random                   nameGen;
  88.  
  89.     /** Number of children to create for each node. */
  90.     static protected final int                DefaultChildrenCount = 7;
  91.  
  92.     static {
  93.     String[]            fontNames;
  94.  
  95.     try {
  96.         fontNames = Toolkit.getDefaultToolkit().getFontList();
  97.     } catch (Exception e) {
  98.         fontNames = null;
  99.     }
  100.     if(fontNames == null || fontNames.length == 0) {
  101.         names = new String[] {"Mark Andrews", "Tom Ball", "Alan Chung",
  102.                       "Rob Davis", "Jeff Dinkins",
  103.                       "Amy Fowler", "James Gosling",
  104.                       "David Karlton", "Dave Kloba", 
  105.                       "Dave Moore", "Hans Muller",
  106.                       "Rick Levenson", "Tim Prinzing",
  107.                       "Chester Rose", "Ray Ryan",
  108.                       "Georges Saab", "Scott Violet",
  109.                       "Kathy Walrath", "Arnaud Weber" };
  110.     }
  111.     else {
  112.         /* Create the Fonts, creating fonts is slow, much better to
  113.            do it once. */
  114.         int              fontSize = 12;
  115.  
  116.         names = fontNames;
  117.         fonts = new Font[names.length];
  118.         for(int counter = 0, maxCounter = names.length;
  119.         counter < maxCounter; counter++) {
  120.         try {
  121.             fonts[counter] = new Font(fontNames[counter], 0, fontSize);
  122.         }
  123.         catch (Exception e) {
  124.             fonts[counter] = null;
  125.         }
  126.         fontSize = ((fontSize + 2 - 12) % 12) + 12;
  127.         }
  128.     }
  129.     nameCount = (float)names.length;
  130.     nameGen = new Random(System.currentTimeMillis());
  131.     }
  132.  
  133.  
  134.     /** Have the children of this node been loaded yet? */
  135.     protected boolean           hasLoaded;
  136.  
  137.     /**
  138.       * Constructs a new DynamicTreeNode instance with o as the user
  139.       * object.
  140.       */
  141.     public DynamicTreeNode(Object o) {
  142.     super(o);
  143.     }
  144.  
  145.     public boolean isLeaf() {
  146.     return false;
  147.     }
  148.  
  149.     /**
  150.       * If hasLoaded is false, meaning the children have not yet been
  151.       * loaded, loadChildren is messaged and super is messaged for
  152.       * the return value.
  153.       */
  154.     public int getChildCount() {
  155.     if(!hasLoaded) {
  156.         loadChildren();
  157.     }
  158.     return super.getChildCount();
  159.     }
  160.  
  161.     /**
  162.       * Messaged the first time getChildCount is messaged.  Creates
  163.       * children with random names from names.
  164.       */
  165.     protected void loadChildren() {
  166.     DynamicTreeNode             newNode;
  167.     Font                        font;
  168.     int                         randomIndex;
  169.     SampleData                  data;
  170.  
  171.     for(int counter = 0; counter < DynamicTreeNode.DefaultChildrenCount;
  172.         counter++) {
  173.         randomIndex = (int)(nameGen.nextFloat() * nameCount);
  174.         if(fonts != null)
  175.         font = fonts[randomIndex];
  176.         else
  177.         font = null;
  178.         if(counter % 2 == 0)
  179.         data = new SampleData(font, Color.red, names[randomIndex]);
  180.         else
  181.         data = new SampleData(font, Color.blue, names[randomIndex]);
  182.         newNode = new DynamicTreeNode(data);
  183.         /* Don't use add() here, add calls insert(newNode, getChildCount())
  184.            so if you want to use add, just be sure to set hasLoaded = true
  185.            first. */
  186.         insert(newNode, counter);
  187.     }
  188.     /* This node has now been loaded, mark it so. */
  189.     hasLoaded = true;
  190.     }
  191. }
  192.