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

📄 jreepadtreemodel.java

📁 一个简单好用的java语言实现的个人日志管理系统
💻 JAVA
字号:
package jreepad;import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.event.*;  public class JreepadTreeModel implements TreeModel  {    private JreepadNode root;    protected EventListenerList listenerList = new EventListenerList();    public JreepadTreeModel(JreepadNode root)    {      this.root = root;    }    public JreepadTreeModel()    {      this(new JreepadNode());    }    public Object getChild(Object parent, int index)    {      return ((JreepadNode)parent).getChildAt(index);    }    public int getChildCount(Object parent)    {      return ((JreepadNode)parent).getChildCount();    }    public int getIndexOfChild(Object parent, Object child)    {      return ((JreepadNode)parent).getIndex((JreepadNode)child);    }    public Object getRoot()    {      return root;    }    public boolean isLeaf(Object node)    {      return ((JreepadNode)node).isLeaf();    }    public void addTreeModelListener(TreeModelListener l) {        listenerList.add(TreeModelListener.class, l);    }    public void removeTreeModelListener(TreeModelListener l) {        listenerList.remove(TreeModelListener.class, l);    }    public void valueForPathChanged(TreePath path, Object newValue)    {      ((JreepadNode)path.getLastPathComponent()).setTitle((String)newValue);    }        public void setRoot(JreepadNode root)    {      this.root = root;    }    // Fire notifications - copied directly from DefaultTreeModel    /**     * Invoke this method if you've modified the TreeNodes upon which this     * model depends.  The model will notify all of its listeners that the     * model has changed below the node <code>node</code> (PENDING).     */    public void reload(TreeNode node) {        if(node != null) {            fireTreeStructureChanged(this, getPathToRoot(node), null, null);        }    }    /**      * Invoke this method after you've changed how node is to be      * represented in the tree.      */    public void nodeChanged(TreeNode node) {        if(listenerList != null && node != null) {            TreeNode         parent = node.getParent();            if(parent != null) {                int        anIndex = parent.getIndex(node);                if(anIndex != -1) {                    int[]        cIndexs = new int[1];                    cIndexs[0] = anIndex;                    nodesChanged(parent, cIndexs);                }            }	    else if (node == getRoot()) {		nodesChanged(node, null);	    }        }    }    /**      * Invoke this method after you've inserted some TreeNodes into      * node.  childIndices should be the index of the new elements and      * must be sorted in ascending order.      */    public void nodesWereInserted(TreeNode node, int[] childIndices) {        if(listenerList != null && node != null && childIndices != null           && childIndices.length > 0) {            int               cCount = childIndices.length;            Object[]          newChildren = new Object[cCount];            for(int counter = 0; counter < cCount; counter++)                newChildren[counter] = node.getChildAt(childIndices[counter]);            fireTreeNodesInserted(this, getPathToRoot(node), childIndices,                                   newChildren);        }    }        /**      * Invoke this method after you've removed some TreeNodes from      * node.  childIndices should be the index of the removed elements and      * must be sorted in ascending order. And removedChildren should be      * the array of the children objects that were removed.      */    public void nodesWereRemoved(TreeNode node, int[] childIndices,                                 Object[] removedChildren) {        if(node != null && childIndices != null) {            fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,                                  removedChildren);        }    }    /**      * Invoke this method after you've changed how the children identified by      * childIndicies are to be represented in the tree.      */    public void nodesChanged(TreeNode node, int[] childIndices) {        if(node != null) {	    if (childIndices != null) {		int            cCount = childIndices.length;		if(cCount > 0) {		    Object[]       cChildren = new Object[cCount];		    for(int counter = 0; counter < cCount; counter++)			cChildren[counter] = node.getChildAt			    (childIndices[counter]);		    fireTreeNodesChanged(this, getPathToRoot(node),					 childIndices, cChildren);		}	    }	    else if (node == getRoot()) {		fireTreeNodesChanged(this, getPathToRoot(node), null, null);	    }        }    }    protected void fireTreeNodesChanged(Object source, Object[] path,                                         int[] childIndices,                                         Object[] children) {        // Guaranteed to return a non-null array        Object[] listeners = listenerList.getListenerList();        TreeModelEvent e = null;        // Process the listeners last to first, notifying        // those that are interested in this event        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==TreeModelListener.class) {                // Lazily create the event:                if (e == null)                    e = new TreeModelEvent(source, path,                                            childIndices, children);                ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);            }                  }    }    protected void fireTreeNodesInserted(Object source, Object[] path,                                         int[] childIndices,                                         Object[] children) {        // Guaranteed to return a non-null array        Object[] listeners = listenerList.getListenerList();        TreeModelEvent e = null;        // Process the listeners last to first, notifying        // those that are interested in this event        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==TreeModelListener.class) {                // Lazily create the event:                if (e == null)                    e = new TreeModelEvent(source, path,                                            childIndices, children);                ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);            }                  }    }    protected void fireTreeNodesRemoved(Object source, Object[] path,                                         int[] childIndices,                                         Object[] children) {        // Guaranteed to return a non-null array        Object[] listeners = listenerList.getListenerList();        TreeModelEvent e = null;        // Process the listeners last to first, notifying        // those that are interested in this event        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==TreeModelListener.class) {                // Lazily create the event:                if (e == null)                    e = new TreeModelEvent(source, path,                                            childIndices, children);                ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);            }                  }    }    protected void fireTreeStructureChanged(Object source, Object[] path,                                         int[] childIndices,                                         Object[] children) {        // Guaranteed to return a non-null array        Object[] listeners = listenerList.getListenerList();        TreeModelEvent e = null;        // Process the listeners last to first, notifying        // those that are interested in this event        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==TreeModelListener.class) {                // Lazily create the event:                if (e == null)                    e = new TreeModelEvent(source, path,                                            childIndices, children);                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);            }                  }    }    /**     * Builds the parents of node up to and including the root node,     * where the original node is the last element in the returned array.     * The length of the returned array gives the node's depth in the     * tree.     *      * @param aNode the TreeNode to get the path for     * @param an array of TreeNodes giving the path from the root to the     *        specified node.      */    public TreeNode[] getPathToRoot(TreeNode aNode) {        return getPathToRoot(aNode, 0);    }    /**     * Builds the parents of node up to and including the root node,     * where the original node is the last element in the returned array.     * The length of the returned array gives the node's depth in the     * tree.     *      * @param aNode  the TreeNode to get the path for     * @param depth  an int giving the number of steps already taken towards     *        the root (on recursive calls), used to size the returned array     * @return an array of TreeNodes giving the path from the root to the     *         specified node      */    protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {        TreeNode[]              retNodes;	// This method recurses, traversing towards the root in order	// size the array. On the way back, it fills in the nodes,	// starting from the root and working back to the original node.        /* Check for null, in case someone passed in a null node, or           they passed in an element that isn't rooted at root. */        if(aNode == null) {            if(depth == 0)                return null;            else                retNodes = new TreeNode[depth];        }        else {            depth++;            if(aNode == root)                retNodes = new TreeNode[depth];            else                retNodes = getPathToRoot(aNode.getParent(), depth);            retNodes[retNodes.length - depth] = aNode;        }        return retNodes;    }        public void insertNodeInto(JreepadNode child, JreepadNode parent, int index)    {      // Does this function need to remove the child from its prior location?    //  parent.    }  } // End of: class JreepadTreeModel

⌨️ 快捷键说明

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