📄 jtree.java
字号:
// Nothing to do here. } /** * Tree Model Node change notification. * * @param e - the event */ public void treeNodesInserted(TreeModelEvent e) { // Nothing to do here. } /** * Tree Model Node change notification. * * @param e - the event */ public void treeNodesRemoved(TreeModelEvent e) { // Nothing to do here. } /** * Tree Model structure change change notification. * * @param e - the event */ public void treeStructureChanged(TreeModelEvent e) { // Nothing to do here. } /** * Tree Selection Listener value change method. * * @param e - the event */ public void valueChanged(TreeSelectionEvent e) { fireValueChanged(e); } } public static class DynamicUtilTreeNode extends DefaultMutableTreeNode { protected Object childValue; protected boolean loadedChildren; /** * Currently not set or used by this class. It might be set and used in * later versions of this class. */ protected boolean hasChildren; public DynamicUtilTreeNode(Object value, Object children) { super(value); childValue = children; loadedChildren = false; } public int getChildCount() { loadChildren(); return super.getChildCount(); } protected void loadChildren() { if (!loadedChildren) { createChildren(this, childValue); loadedChildren = true; } } public Enumeration children() { loadChildren(); return super.children(); } /** * Returns the child node at position <code>pos</code>. Subclassed * here to load the children if necessary. * * @param pos the position of the child node to fetch * * @return the childnode at the specified position */ public TreeNode getChildAt(int pos) { loadChildren(); return super.getChildAt(pos); } public boolean isLeaf() { return (childValue == null || !(childValue instanceof Hashtable || childValue instanceof Vector || childValue.getClass() .isArray())); } public static void createChildren(DefaultMutableTreeNode parent, Object children) { if (children instanceof Hashtable) { Hashtable tab = (Hashtable) children; Enumeration e = tab.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); Object val = tab.get(key); parent.add(new DynamicUtilTreeNode(key, val)); } } else if (children instanceof Vector) { Iterator i = ((Vector) children).iterator(); while (i.hasNext()) { Object n = i.next(); parent.add(new DynamicUtilTreeNode(n, n)); } } else if (children != null && children.getClass().isArray()) { Object[] arr = (Object[]) children; for (int i = 0; i < arr.length; ++i) parent.add(new DynamicUtilTreeNode(arr[i], arr[i])); } } } /** * Listens to the model of the JTree and updates the property * <code>expandedState</code> if nodes are removed or changed. */ protected class TreeModelHandler implements TreeModelListener { /** * Creates a new instance of TreeModelHandler. */ protected TreeModelHandler() { // Nothing to do here. } /** * Notifies when a node has changed in some ways. This does not include * that a node has changed its location or changed it's children. It * only means that some attributes of the node have changed that might * affect its presentation. * * This method is called after the actual change occured. * * @param ev the TreeModelEvent describing the change */ public void treeNodesChanged(TreeModelEvent ev) { // Nothing to do here. } /** * Notifies when a node is inserted into the tree. * * This method is called after the actual change occured. * * @param ev the TreeModelEvent describing the change */ public void treeNodesInserted(TreeModelEvent ev) { // nothing to do here } /** * Notifies when a node is removed from the tree. * * This method is called after the actual change occured. * * @param ev the TreeModelEvent describing the change */ public void treeNodesRemoved(TreeModelEvent ev) { // TODO: The API docs suggest that this method should do something // but I cannot really see what has to be done here ... } /** * Notifies when the structure of the tree is changed. * * This method is called after the actual change occured. * * @param ev the TreeModelEvent describing the change */ public void treeStructureChanged(TreeModelEvent ev) { // Set state of new path. TreePath path = ev.getTreePath(); setExpandedState(path, isExpanded(path)); } } /** * This redirects TreeSelectionEvents and rewrites the source of it to be * this JTree. This is typically done when the tree model generates an * event, but the JTree object associated with that model should be listed * as the actual source of the event. */ protected class TreeSelectionRedirector implements TreeSelectionListener, Serializable { /** The serial version UID. */ private static final long serialVersionUID = -3505069663646241664L; /** * Creates a new instance of TreeSelectionRedirector */ protected TreeSelectionRedirector() { // Nothing to do here. } /** * Notifies when the tree selection changes. * * @param ev the TreeSelectionEvent that describes the change */ public void valueChanged(TreeSelectionEvent ev) { TreeSelectionEvent rewritten = (TreeSelectionEvent) ev.cloneWithSource(JTree.this); fireValueChanged(rewritten); JTree.this.repaint(); } } /** * A TreeModel that does not allow anything to be selected. */ protected static class EmptySelectionModel extends DefaultTreeSelectionModel { /** The serial version UID. */ private static final long serialVersionUID = -5815023306225701477L; /** * The shared instance of this model. */ protected static final EmptySelectionModel sharedInstance = new EmptySelectionModel(); /** * Creates a new instance of EmptySelectionModel. */ protected EmptySelectionModel() { // Nothing to do here. } /** * Returns the shared instance of EmptySelectionModel. * * @return the shared instance of EmptySelectionModel */ public static EmptySelectionModel sharedInstance() { return sharedInstance; } /** * This catches attempts to set a selection and sets nothing instead. * * @param paths not used here */ public void setSelectionPaths(TreePath[] paths) { // We don't allow selections in this class. } /** * This catches attempts to add something to the selection. * * @param paths not used here */ public void addSelectionPaths(TreePath[] paths) { // We don't allow selections in this class. } /** * This catches attempts to remove something from the selection. * * @param paths not used here */ public void removeSelectionPaths(TreePath[] paths) { // We don't allow selections in this class. } } private static final long serialVersionUID = 7559816092864483649L; public static final String CELL_EDITOR_PROPERTY = "cellEditor"; public static final String CELL_RENDERER_PROPERTY = "cellRenderer"; public static final String EDITABLE_PROPERTY = "editable"; public static final String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing"; public static final String LARGE_MODEL_PROPERTY = "largeModel"; public static final String ROOT_VISIBLE_PROPERTY = "rootVisible"; public static final String ROW_HEIGHT_PROPERTY = "rowHeight"; public static final String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand"; public static final String SELECTION_MODEL_PROPERTY = "selectionModel"; public static final String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles"; public static final String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount"; public static final String TREE_MODEL_PROPERTY = "model"; public static final String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount"; /** @since 1.3 */ public static final String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath"; /** @since 1.3 */ public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath"; /** @since 1.3 */ public static final String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths"; private static final Object EXPANDED = new Object(); private static final Object COLLAPSED = new Object(); private boolean dragEnabled; private boolean expandsSelectedPaths; private TreePath anchorSelectionPath; private TreePath leadSelectionPath; /** * This contains the state of all nodes in the tree. Al/ entries map the * TreePath of a note to to its state. Valid states are EXPANDED and * COLLAPSED. Nodes not in this Hashtable are assumed state COLLAPSED. */ private Hashtable nodeStates = new Hashtable(); protected transient TreeCellEditor cellEditor; protected transient TreeCellRenderer cellRenderer; protected boolean editable; protected boolean invokesStopCellEditing; protected boolean largeModel; protected boolean rootVisible; protected int rowHeight; protected boolean scrollsOnExpand; protected transient TreeSelectionModel selectionModel; protected boolean showsRootHandles; protected int toggleClickCount; protected transient TreeModel treeModel; protected int visibleRowCount; /** * Handles TreeModelEvents to update the expandedState. */ protected transient TreeModelListener treeModelListener; /** * Redirects TreeSelectionEvents so that the source is this JTree. */ protected TreeSelectionRedirector selectionRedirector = new TreeSelectionRedirector(); /** * Creates a new <code>JTree</code> object. */ public JTree() { this(createTreeModel(null)); } /** * Creates a new <code>JTree</code> object. * * @param value the initial nodes in the tree */ public JTree(Hashtable value) { this(createTreeModel(value)); } /** * Creates a new <code>JTree</code> object. * * @param value the initial nodes in the tree */ public JTree(Object[] value) { this(createTreeModel(value)); } /** * Creates a new <code>JTree</code> object. * * @param model the model to use */ public JTree(TreeModel model) { updateUI(); setRootVisible(true); setModel(model); setSelectionModel(new EmptySelectionModel()); selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // The root node appears expanded by default. nodeStates.put(new TreePath(model.getRoot()), EXPANDED); } /** * Creates a new <code>JTree</code> object. * * @param root the root node */ public JTree(TreeNode root) { this(root, false); } /** * Creates a new <code>JTree</code> object. * * @param root the root node * @param asksAllowChildren if false, all nodes without children are leaf * nodes. If true, only nodes that do not allow children are leaf * nodes. */ public JTree(TreeNode root, boolean asksAllowChildren) { this(new DefaultTreeModel(root, asksAllowChildren)); } /** * Creates a new <code>JTree</code> object. * * @param value the initial nodes in the tree */ public JTree(Vector value) { this(createTreeModel(value)); } public int getRowForPath(TreePath path) { TreeUI ui = getUI(); if (ui != null) return ui.getRowForPath(this, path); return -1; } public TreePath getPathForRow(int row) { TreeUI ui = getUI(); return ui != null ? ui.getPathForRow(this, row) : null; } protected TreePath[] getPathBetweenRows(int index0, int index1) { TreeUI ui = getUI(); if (ui == null) return null; int minIndex = Math.min(index0, index1); int maxIndex = Math.max(index0, index1); TreePath[] paths = new TreePath[maxIndex - minIndex + 1]; for (int i = minIndex; i <= maxIndex; ++i) paths[i - minIndex] = ui.getPathForRow(this, i); return paths; } /** * Creates a new <code>TreeModel</code> object. * * @param value the values stored in the model */ protected static TreeModel createTreeModel(Object value) { return new DefaultTreeModel(new DynamicUtilTreeNode(value, value)); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -