📄 basictreeui.java
字号:
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) { Rectangle bounds = null; int row = -1; Object cell = null; if (path != null) { row = getRowForPath(tree, path); cell = path.getLastPathComponent(); bounds = new Rectangle(0, row * getRowHeight(), 0, 0); } return nodeDimensions.getNodeDimensions(cell, row, getLevel(cell), tree.isExpanded(path), bounds); } /** * 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 / getRowHeight()); 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(true, false, false); return !isEditing(tree); } /** * Cancels the current editing session. * * @param tree * is the tree to cancel the editing session on. */ public void cancelEditing(JTree tree) { if (isEditing(tree)) completeEditing(false, true, false); } /** * 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() { return new PropertyChangeHandler(); } /** * Creates the listener responsible for updating the selection based on mouse * events. * * @return the MouseListener responsible for updating. */ protected MouseListener createMouseListener() { return new MouseHandler(); } /** * Creates the listener that is responsible for updating the display when * focus is lost/grained. * * @return the FocusListener responsible for updating. */ protected FocusListener createFocusListener() { return new FocusHandler(); } /** * Creates the listener reponsible for getting key events from the tree. * * @return the KeyListener responsible for getting key events. */ protected KeyListener createKeyListener() { return new KeyHandler(); } /** * Creates the listener responsible for getting property change events from * the selection model. * * @returns the PropertyChangeListener reponsible for getting property change * events from the selection model. */ protected PropertyChangeListener createSelectionModelPropertyChangeListener() { return new SelectionModelPropertyChangeHandler(); } /** * Creates the listener that updates the display based on selection change * methods. * * @return the TreeSelectionListener responsible for updating. */ protected TreeSelectionListener createTreeSelectionListener() { return new TreeSelectionHandler(); } /** * Creates a listener to handle events from the current editor * * @return the CellEditorListener that handles events from the current editor */ protected CellEditorListener createCellEditorListener() { return new CellEditorHandler(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -