basictreeui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,297 行 · 第 1/5 页
JAVA
2,297 行
* is the height to set this.rowHeight to. */ protected void setRowHeight(int rowHeight) { if (rowHeight == 0) rowHeight = Math.max(getMaxHeight(tree), 20); treeState.setRowHeight(rowHeight); } /** * Returns the current row height. * * @return current row height. */ protected int getRowHeight() { return treeState.getRowHeight(); } /** * Sets the TreeCellRenderer to <code>tcr</code>. This invokes * <code>updateRenderer</code>. * * @param tcr * is the new TreeCellRenderer. */ protected void setCellRenderer(TreeCellRenderer tcr) { currentCellRenderer = tcr; updateRenderer(); } /** * Return currentCellRenderer, which will either be the trees renderer, or * defaultCellRenderer, which ever was not null. * * @return the current Cell Renderer */ protected TreeCellRenderer getCellRenderer() { if (currentCellRenderer != null) return currentCellRenderer; return createDefaultCellRenderer(); } /** * Sets the tree's model. * * @param model * to set the treeModel to. */ protected void setModel(TreeModel model) { tree.setModel(model); treeModel = tree.getModel(); } /** * Returns the tree's model * * @return treeModel */ protected TreeModel getModel() { return treeModel; } /** * Sets the root to being visible. * * @param newValue * sets the visibility of the root */ protected void setRootVisible(boolean newValue) { tree.setRootVisible(newValue); } /** * Returns true if the root is visible. * * @return true if the root is visible. */ protected boolean isRootVisible() { return tree.isRootVisible(); } /** * Determines whether the node handles are to be displayed. * * @param newValue * sets whether or not node handles should be displayed. */ protected void setShowsRootHandles(boolean newValue) { tree.setShowsRootHandles(newValue); } /** * Returns true if the node handles are to be displayed. * * @return true if the node handles are to be displayed. */ protected boolean getShowsRootHandles() { return tree.getShowsRootHandles(); } /** * Sets the cell editor. * * @param editor * to set the cellEditor to. */ protected void setCellEditor(TreeCellEditor editor) { cellEditor = editor; createdCellEditor = true; } /** * Returns the <code>TreeCellEditor</code> for this tree. * * @return the cellEditor for this tree. */ protected TreeCellEditor getCellEditor() { return cellEditor; } /** * Configures the receiver to allow, or not allow, editing. * * @param newValue * sets the receiver to allow editing if true. */ protected void setEditable(boolean newValue) { tree.setEditable(newValue); } /** * Returns true if the receiver allows editing. * * @return true if the receiver allows editing. */ protected boolean isEditable() { return tree.isEditable(); } /** * Resets the selection model. The appropriate listeners are installed on the * model. * * @param newLSM * resets the selection model. */ protected void setSelectionModel(TreeSelectionModel newLSM) { if (newLSM != null) { treeSelectionModel = newLSM; tree.setSelectionModel(treeSelectionModel); } } /** * Returns the current selection model. * * @return the current selection model. */ protected TreeSelectionModel getSelectionModel() { return treeSelectionModel; } /** * Returns the Rectangle enclosing the label portion that the last item in * path will be drawn to. Will return null if any component in path is * currently valid. * * @param tree * is the current tree the path will be drawn to. * @param path * is the current path the tree to draw to. * @return the Rectangle enclosing the label portion that the last item in the * path will be drawn to. */ public Rectangle getPathBounds(JTree tree, TreePath path) { int row = -1; Object cell = null; if (path != null) { row = getRowForPath(tree, path); cell = path.getLastPathComponent(); } return nodeDimensions.getNodeDimensions(cell, row, getLevel(cell), tree.isExpanded(path), new Rectangle()); } /** * Returns the max height of all the nodes in the tree. * * @param tree - * the current tree * @return the max height. */ private int getMaxHeight(JTree tree) { if (maxHeight != 0) return maxHeight; Icon e = UIManager.getIcon("Tree.openIcon"); Icon c = UIManager.getIcon("Tree.closedIcon"); Icon l = UIManager.getIcon("Tree.leafIcon"); int rc = getRowCount(tree); int iconHeight = 0; for (int row = 0; row < rc; row++) { if (isLeaf(row)) iconHeight = l.getIconHeight(); else if (tree.isExpanded(row)) iconHeight = e.getIconHeight(); else iconHeight = c.getIconHeight(); maxHeight = Math.max(maxHeight, iconHeight + gap); } return maxHeight; } /** * Returns the path for passed in row. If row is not visible null is returned. * * @param tree * is the current tree to return path for. * @param row * is the row number of the row to return. * @return the path for passed in row. If row is not visible null is returned. */ public TreePath getPathForRow(JTree tree, int row) { if (treeModel != null && currentVisiblePath != null) { Object[] nodes = currentVisiblePath.getPath(); if (row < nodes.length) return new TreePath(getPathToRoot(nodes[row], 0)); } return null; } /** * Returns the row that the last item identified in path is visible at. Will * return -1 if any of the elments in the path are not currently visible. * * @param tree * is the current tree to return the row for. * @param path * is the path used to find the row. * @return the row that the last item identified in path is visible at. Will * return -1 if any of the elments in the path are not currently * visible. */ public int getRowForPath(JTree tree, TreePath path) { int row = 0; Object dest = path.getLastPathComponent(); int rowCount = getRowCount(tree); if (currentVisiblePath != null) { Object[] nodes = currentVisiblePath.getPath(); while (row < rowCount) { if (dest.equals(nodes[row])) return row; row++; } } return -1; } /** * Returns the number of rows that are being displayed. * * @param tree * is the current tree to return the number of rows for. * @return the number of rows being displayed. */ public int getRowCount(JTree tree) { if (currentVisiblePath != null) return currentVisiblePath.getPathCount(); return 0; } /** * Returns the path to the node that is closest to x,y. If there is nothing * currently visible this will return null, otherwise it'll always return a * valid path. If you need to test if the returned object is exactly at x,y * you should get the bounds for the returned path and test x,y against that. * * @param tree * the tree to search for the closest path * @param x * is the x coordinate of the location to search * @param y * is the y coordinate of the location to search * @return the tree path closes to x,y. */ public TreePath getClosestPathForLocation(JTree tree, int x, int y) { int row = Math.round(y / getMaxHeight(tree)); TreePath path = getPathForRow(tree, row); // no row is visible at this node while (row > 0 && path == null) { --row; path = getPathForRow(tree, row); } return path; } /** * Returns true if the tree is being edited. The item that is being edited can * be returned by getEditingPath(). * * @param tree * is the tree to check for editing. * @return true if the tree is being edited. */ public boolean isEditing(JTree tree) { return isEditing; } /** * Stops the current editing session. This has no effect if the tree is not * being edited. Returns true if the editor allows the editing session to * stop. * * @param tree * is the tree to stop the editing on * @return true if the editor allows the editing session to stop. */ public boolean stopEditing(JTree tree) { if (isEditing(tree)) { completeEditing(false, false, true); finish(); } return !isEditing(tree); } /** * Cancels the current editing session. * * @param tree * is the tree to cancel the editing session on. */ public void cancelEditing(JTree tree) { // There is no need to send the cancel message to the editor, // as the cancellation event itself arrives from it. This would // only be necessary when cancelling the editing programatically. completeEditing(false, false, false); finish(); } /** * Selects the last item in path and tries to edit it. Editing will fail if * the CellEditor won't allow it for the selected item. * * @param tree * is the tree to edit on. * @param path * is the path in tree to edit on. */ public void startEditingAtPath(JTree tree, TreePath path) { startEditing(path, null); } /** * Returns the path to the element that is being editted. * * @param tree * is the tree to get the editing path from. * @return the path that is being edited. */ public TreePath getEditingPath(JTree tree) { return editingPath; } /** * Invoked after the tree instance variable has been set, but before any * default/listeners have been installed. */ protected void prepareForUIInstall() { // TODO: Implement this properly. } /** * Invoked from installUI after all the defaults/listeners have been * installed. */ protected void completeUIInstall() { // TODO: Implement this properly. } /** * Invoked from uninstallUI after all the defaults/listeners have been * uninstalled. */ protected void completeUIUninstall() { // TODO: Implement this properly. } /** * Installs the subcomponents of the tree, which is the renderer pane. */ protected void installComponents() { currentCellRenderer = createDefaultCellRenderer(); rendererPane = createCellRendererPane(); createdRenderer = true; setCellRenderer(currentCellRenderer); } /** * Creates an instance of NodeDimensions that is able to determine the size of * a given node in the tree. * * @return the NodeDimensions of a given node in the tree */ protected AbstractLayoutCache.NodeDimensions createNodeDimensions() { return new NodeDimensionsHandler(); } /** * Creates a listener that is reponsible for the updates the UI based on how * the tree changes. * * @return the PropertyChangeListener that is reposnsible for the updates */ protected PropertyChangeListener createPropertyChangeListener()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?