⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treemodelmaker.java

📁 Java实例入门
💻 JAVA
字号:
/*
 * Copyright (c) 1996-2001 Borland Software Corporation. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by Borland Software as part
 * of an Borland Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland Software.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */
//------------------------------------------------------------------------------
// Copyright (c) 1996-2001 Borland Software Corporation.  All Rights Reserved.
//------------------------------------------------------------------------------

package com.borland.samples.dbswing.htmlviewer;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.tree.*;
import com.borland.dbswing.*;

/*
   TreeModelMaker creates Swing DefaultTreeModel from a two-dimensional array
   of data called nodeData. It's contents are something like this:
    { { "<root>",    "/"             },
      { "/",         "Child_1"       },
      { "/Child_1",  "Grandchild_1A" },
      { "/Child_1",  "Grandchild_1B" },
      { "/",         "Child_2"       };
   Each two-element array here defines a node.  We call the first element the
   node's path and the second its caption (assuming that there will be a
   renderer that displays just that information in the tree.  The node's path
   and caption, concatenated together (with a backslash between, if necessary),
   make up the node's text.  The text is the node's user data object.

   Nodes are added to the model in the order they appear in nodeData.  For the
   tree to be built successfully, each node's path must be the node text of a
   node already added to the tree.  For example, the node with the caption
   "Child_1" has node text of "/Child_1"; since this matches the path of the
   node with caption "Grandchild_1A", the latter will be added to the model as
   a child of the former.  If no parent is found, no error condition is raised
   but no node is inserted.  This allows us to produce a tree in spite or
   errors in the data - but there is the risk that a large subtree might be
   omitted.

   The first row of data in nodeData represents the tree's root. Its path in
   nodeData is ignored.  We assume you'll set the tree's rootHidden property to
   true and never select it. If it has only one child, that child will look
   like the root when the tree is displayed.

   Nodes are DefaultMutableTreeNodes.

   Not designable because DefaultTreeModel doesn't have a parameter-less
   constructor.  Can fix this by putting it inside the constructors,but
   then we have to return a tree instead of a model.  Returning a model is
   better because it can be used for a JdbTree or a JdbNavTree.
*/
public class TreeModelMaker {
  String[][] nodeData = { { "<none>", "Root", "<unknown URL>" } };
  DefaultTreeModel model;

  // make up a default model
  public TreeModelMaker() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(nodeData[0][1]);
    model = new DefaultTreeModel(root);
  }

  public TreeModelMaker(String[][] nodeDataParm) {
    DefaultMutableTreeNode currentNode;
    Enumeration nodes;

    // Only use nodeDataParm if its elements are the right length.  If the model
    // is to be built successfully, the data must also satisfy the condition
    // that the path to each node must be a node already in the model.  This is
    // much harder to check.
    if (nodeDataParm[0].length >= 2) {
      nodeData = nodeDataParm;
    }

    // Create root node from first element of nodeData, then create model containing it
    currentNode = new DefaultMutableTreeNode(nodeData[0][1]);
    model = new DefaultTreeModel(currentNode);

    // For each remaining element of nodeData, if its parent is found in the
    // tree, add a child node.  We use a  simple and inefficient way to find parent:
    // for each node, get an enumeration of all nodes already in tree, and search
    // through it until we find a match.  Breaking the new node's path into pieces
    // and searching only among children of the current node for the next match
    // would be more efficient but also more complicated.
    for (int i = 1; i < nodeData.length; ++i) {
      currentNode = (DefaultMutableTreeNode) model.getRoot();
      nodes = ((DefaultMutableTreeNode) model.getRoot()).breadthFirstEnumeration();
      while (nodes.hasMoreElements()) {
        currentNode = ((DefaultMutableTreeNode) nodes.nextElement());
        if (currentNode.getUserObject().equals(nodeData[i][0])) {
          currentNode.add(new DefaultMutableTreeNode((nodeData[i][0].equals("/") ? "" : nodeData[i][0]) + "/" + nodeData[i][1]));
          break;
        }
      }
    }
  }

  public void setnodeData(String[][] nodeDataParm) {
    nodeData = nodeDataParm;
  }

  public String[][] getnodeData() {
    return nodeData;
  }

  public TreeModel getModel() {
    return model;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -