src/demo/share/jfc/SampleTree/DynamicTreeNode.java
author coleenp
Wed, 08 Nov 2017 16:03:35 -0500
changeset 47813 fedbf1b866a7
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Font;
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    44
import java.awt.GraphicsEnvironment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Random;
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    46
import javax.swing.tree.DefaultMutableTreeNode;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    47
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
 * DynamicTreeNode illustrates one of the possible ways in which dynamic
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    51
 * loading can be used in tree.  The basic premise behind this is that
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
 * getChildCount() will be messaged from JTreeModel before any children
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    53
 * are asked for.  So, the first time getChildCount() is issued the
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
 * children are loaded.<p>
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
 * It should be noted that isLeaf will also be messaged from the model.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
 * The default behavior of TreeNode is to message getChildCount to
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
 * determine this. As such, isLeaf is subclassed to always return false.<p>
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
 * There are others ways this could be accomplished as well.  Instead of
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
 * subclassing TreeNode you could subclass JTreeModel and do the same
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
 * thing in getChildCount().  Or, if you aren't using TreeNode you could
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
 * write your own TreeModel implementation.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    62
 * Another solution would be to listen for TreeNodeExpansion events and
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    63
 * the first time a node has been expanded post the appropriate insertion
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    64
 * events.  I would not recommend this approach though, the other two
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    65
 * are much simpler and cleaner (and are faster from the perspective of
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    66
 * how tree deals with it).
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
 *
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
 * NOTE: getAllowsChildren() can be messaged before getChildCount().
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    69
 *       For this example the nodes always allow children, so it isn't
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    70
 *       a problem, but if you do support true leaf nodes you may want
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
 *       to check for loading in getAllowsChildren too.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
 *
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    73
 * @author Scott Violet
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    74
 */
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    75
@SuppressWarnings("serial")
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    76
public class DynamicTreeNode extends DefaultMutableTreeNode {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    77
    // Class stuff.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Number of names. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    80
    protected static float nameCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** Names to use for children. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    82
    protected static final String[] NAMES;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /** Potential fonts used to draw with. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    84
    protected static Font[] fonts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** Used to generate the names. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    86
    protected static Random nameGen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** Number of children to create for each node. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    88
    protected static final int DEFAULT_CHILDREN_COUNT = 7;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    91
        String[] fontNames;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    94
            fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    95
                    getAvailableFontFamilyNames();
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    96
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            fontNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   100
        if (fontNames == null || fontNames.length == 0) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   101
            NAMES = new String[] { "Mark Andrews", "Tom Ball", "Alan Chung",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   102
                        "Rob Davis", "Jeff Dinkins",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   103
                        "Amy Fowler", "James Gosling",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   104
                        "David Karlton", "Dave Kloba",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   105
                        "Dave Moore", "Hans Muller",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   106
                        "Rick Levenson", "Tim Prinzing",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   107
                        "Chester Rose", "Ray Ryan",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   108
                        "Georges Saab", "Scott Violet",
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   109
                        "Kathy Walrath", "Arnaud Weber" };
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   110
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            /* Create the Fonts, creating fonts is slow, much better to
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   112
            do it once. */
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   113
            int fontSize = 12;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   115
            NAMES = fontNames;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   116
            fonts = new Font[NAMES.length];
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   117
            for (int counter = 0, maxCounter = NAMES.length;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   118
                    counter < maxCounter; counter++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    fonts[counter] = new Font(fontNames[counter], 0, fontSize);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   121
                } catch (Exception e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    fonts[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                fontSize = ((fontSize + 2 - 12) % 12) + 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   127
        nameCount = (float) NAMES.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        nameGen = new Random(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /** Have the children of this node been loaded yet? */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   131
    protected boolean hasLoaded;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   134
     * Constructs a new DynamicTreeNode instance with o as the user
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   135
     * object.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   136
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public DynamicTreeNode(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        super(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   141
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public boolean isLeaf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   147
     * If hasLoaded is false, meaning the children have not yet been
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   148
     * loaded, loadChildren is messaged and super is messaged for
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   149
     * the return value.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   150
     */
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   151
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public int getChildCount() {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   153
        if (!hasLoaded) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            loadChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return super.getChildCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   160
     * Messaged the first time getChildCount is messaged.  Creates
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   161
     * children with random names from names.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   162
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected void loadChildren() {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   164
        DynamicTreeNode newNode;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   165
        Font font;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   166
        int randomIndex;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   167
        SampleData data;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   169
        for (int counter = 0; counter < DynamicTreeNode.DEFAULT_CHILDREN_COUNT;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   170
                counter++) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   171
            randomIndex = (int) (nameGen.nextFloat() * nameCount);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   172
            String displayString = NAMES[randomIndex];
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   173
            if (fonts == null || fonts[randomIndex].canDisplayUpTo(displayString)
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   174
                    != -1) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   175
                font = null;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   176
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                font = fonts[randomIndex];
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   178
            }
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   179
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   180
            if (counter % 2 == 0) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   181
                data = new SampleData(font, Color.red, displayString);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   182
            } else {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   183
                data = new SampleData(font, Color.blue, displayString);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   184
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            newNode = new DynamicTreeNode(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            /* Don't use add() here, add calls insert(newNode, getChildCount())
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   187
            so if you want to use add, just be sure to set hasLoaded = true
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   188
            first. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            insert(newNode, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /* This node has now been loaded, mark it so. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        hasLoaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}