DefaultTreeTableModel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DefaultTreeTableModel.java,v 1.2 2005/10/10 18:01:38 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.treetable;
  22. import javax.swing.event.TreeModelEvent;
  23. import javax.swing.event.TreeModelListener;
  24. import javax.swing.tree.TreeNode;
  25. import javax.swing.tree.TreePath;
  26. /**
  27.  * DefaultTreeTableModel is a concrete implementation of <code>AbstractTreeTableModel</code>
  28.  * and is provided purely as a convenience. Applications that use <code>JXTreeTable</code>
  29.  * are expected to provide their own implementation of a <code>TreeTableModel</code>,
  30.  * perhaps by extending this class.
  31.  *
  32.  * @author Ramesh Gupta
  33.  */
  34. public class DefaultTreeTableModel extends AbstractTreeTableModel {
  35.     protected boolean asksAllowsChildren;
  36.     public DefaultTreeTableModel() {
  37.         this(null);
  38.     }
  39.     public DefaultTreeTableModel(TreeNode root) {
  40.         this(root, false);
  41.     }
  42.     public DefaultTreeTableModel(TreeNode root, boolean asksAllowsChildren) {
  43.         super(root);
  44.         this.asksAllowsChildren = asksAllowsChildren;
  45.     }
  46.     public void setRoot(TreeNode root) {
  47.         Object oldRoot = this.root;
  48.         this.root = root;
  49.         if (root == null && oldRoot != null) {
  50.             fireTreeStructureChanged(this, null);
  51.         }
  52.         else {
  53.             nodeStructureChanged(root);
  54.         }
  55.     }
  56.     /*
  57.      * Notifies all listeners that have registered interest for
  58.      * notification on this event type.  The event instance
  59.      * is lazily created using the parameters passed into
  60.      * the fire method.
  61.      *
  62.      * @param source the node where the tree model has changed
  63.      * @param path the path to the root node
  64.      * @see EventListenerList
  65.      */
  66.     private void fireTreeStructureChanged(Object source, TreePath path) {
  67.         // Guaranteed to return a non-null array
  68.         Object[] listeners = listenerList.getListenerList();
  69.         TreeModelEvent e = null;
  70.         // Process the listeners last to first, notifying
  71.         // those that are interested in this event
  72.         for (int i = listeners.length - 2; i >= 0; i -= 2) {
  73.             if (listeners[i] == TreeModelListener.class) {
  74.                 // Lazily create the event:
  75.                 if (e == null)
  76.                     e = new TreeModelEvent(source, path);
  77.                 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e);
  78.             }
  79.         }
  80.     }
  81.     public boolean asksAllowsChildren() {
  82.         return asksAllowsChildren;
  83.     }
  84.     public void setAsksAllowsChildren(boolean newValue) {
  85.         asksAllowsChildren = newValue;
  86.     }
  87.     public Object getValueAt(Object node, int column) {
  88.         /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
  89.         return node + "@column " + column;
  90.     }
  91.     public void setValueAt(Object value, Object node, int column) {
  92.         /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
  93.     }
  94.     public TreeNode[] getPathToRoot(TreeNode node) {
  95.         return getPathToRoot(node, 0);
  96.     }
  97.     protected TreeNode[] getPathToRoot(TreeNode node, int depth) {
  98.         TreeNode[] retNodes;
  99.         // This method recurses, traversing towards the root in order
  100.         // size the array. On the way back, it fills in the nodes,
  101.         // starting from the root and working back to the original node.
  102.         /* Check for null, in case someone passed in a null node, or
  103.            they passed in an element that isn't rooted at root. */
  104.         if (node == null) {
  105.             if (depth == 0)
  106.                 return null;
  107.             else
  108.                 retNodes = new TreeNode[depth];
  109.         }
  110.         else {
  111.             depth++;
  112.             if (node == root)
  113.                 retNodes = new TreeNode[depth];
  114.             else
  115.                 retNodes = getPathToRoot(node.getParent(), depth);
  116.             retNodes[retNodes.length - depth] = node;
  117.         }
  118.         return retNodes;
  119.     }
  120.     /**
  121.      * @param node
  122.      * @return true if the specified node is a leaf node; false otherwise
  123.      */
  124.     public boolean isLeaf(Object node) {
  125.         if (node instanceof TreeNode) {
  126.             if (asksAllowsChildren) {
  127.                 return!((TreeNode) node).getAllowsChildren();
  128.             }
  129.         }
  130.         return super.isLeaf(node);
  131.     }
  132.     public void reload() {
  133.         TreeNode treeNode;
  134.         try {
  135.             treeNode = (TreeNode) root;
  136.         }
  137.         catch (ClassCastException ex) {
  138.             return;
  139.         }
  140.         reload(treeNode);
  141.     }
  142.     public void reload(TreeNode node) {
  143.         if (node != null) {
  144.             fireTreeStructureChanged(this, getPathToRoot(node), null, null);
  145.         }
  146.     }
  147.     /**
  148.      * Invoke this method after you've inserted some TreeNodes into
  149.      * node.  childIndices should be the index of the new elements and
  150.      * must be sorted in ascending order.
  151.      */
  152.     public void nodesWereInserted(TreeNode node, int[] childIndices) {
  153.         if (listenerList != null && node != null && childIndices != null
  154.             && childIndices.length > 0) {
  155.             int cCount = childIndices.length;
  156.             Object[] newChildren = new Object[cCount];
  157.             for (int counter = 0; counter < cCount; counter++)
  158.                 newChildren[counter] = node.getChildAt(childIndices[counter]);
  159.             fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
  160.                                   newChildren);
  161.         }
  162.     }
  163.     /**
  164.      * Invoke this method after you've removed some TreeNodes from
  165.      * node.  childIndices should be the index of the removed elements and
  166.      * must be sorted in ascending order. And removedChildren should be
  167.      * the array of the children objects that were removed.
  168.      */
  169.     public void nodesWereRemoved(TreeNode node, int[] childIndices,
  170.                                  Object[] removedChildren) {
  171.         if (node != null && childIndices != null) {
  172.             fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,
  173.                                  removedChildren);
  174.         }
  175.     }
  176.     /**
  177.      * Invoke this method after you've changed how the children identified by
  178.      * childIndicies are to be represented in the tree.
  179.      */
  180.     public void nodesChanged(TreeNode node, int[] childIndices) {
  181.         if (node != null) {
  182.             if (childIndices != null) {
  183.                 int cCount = childIndices.length;
  184.                 if (cCount > 0) {
  185.                     Object[] cChildren = new Object[cCount];
  186.                     for (int counter = 0; counter < cCount; counter++)
  187.                         cChildren[counter] = node.getChildAt
  188.                             (childIndices[counter]);
  189.                     fireTreeNodesChanged(this, getPathToRoot(node),
  190.                                          childIndices, cChildren);
  191.                 }
  192.             }
  193.             else if (node == getRoot()) {
  194.                 fireTreeNodesChanged(this, getPathToRoot(node), null, null);
  195.             }
  196.         }
  197.     }
  198.     /**
  199.      * Invoke this method if you've totally changed the children of
  200.      * node and its childrens children...  This will post a
  201.      * treeStructureChanged event.
  202.      */
  203.     public void nodeStructureChanged(TreeNode node) {
  204.         if (node != null) {
  205.             fireTreeStructureChanged(this, getPathToRoot(node), null, null);
  206.         }
  207.     }
  208. }