📄 basictreeui.java
字号:
return getHandler(); } /** * Creates the listener reponsible for getting key events from * the tree. */ protected KeyListener createKeyListener() { return getHandler(); } /** * Creates the listener responsible for getting property change * events from the selection model. */ protected PropertyChangeListener createSelectionModelPropertyChangeListener() { return getHandler(); } /** * Creates the listener that updates the display based on selection change * methods. */ protected TreeSelectionListener createTreeSelectionListener() { return getHandler(); } /** * Creates a listener to handle events from the current editor. */ protected CellEditorListener createCellEditorListener() { return getHandler(); } /** * Creates and returns a new ComponentHandler. This is used for * the large model to mark the validCachedPreferredSize as invalid * when the component moves. */ protected ComponentListener createComponentListener() { return new ComponentHandler(); } /** * Creates and returns the object responsible for updating the treestate * when nodes expanded state changes. */ protected TreeExpansionListener createTreeExpansionListener() { return getHandler(); } /** * Creates the object responsible for managing what is expanded, as * well as the size of nodes. */ protected AbstractLayoutCache createLayoutCache() { if(isLargeModel() && getRowHeight() > 0) { return new FixedHeightLayoutCache(); } return new VariableHeightLayoutCache(); } /** * Returns the renderer pane that renderer components are placed in. */ protected CellRendererPane createCellRendererPane() { return new CellRendererPane(); } /** * Creates a default cell editor. */ protected TreeCellEditor createDefaultCellEditor() { if(currentCellRenderer != null && (currentCellRenderer instanceof DefaultTreeCellRenderer)) { DefaultTreeCellEditor editor = new DefaultTreeCellEditor (tree, (DefaultTreeCellRenderer)currentCellRenderer); return editor; } return new DefaultTreeCellEditor(tree, null); } /** * Returns the default cell renderer that is used to do the * stamping of each node. */ protected TreeCellRenderer createDefaultCellRenderer() { return new DefaultTreeCellRenderer(); } /** * Returns a listener that can update the tree when the model changes. */ protected TreeModelListener createTreeModelListener() { return getHandler(); } // // Uninstall methods // public void uninstallUI(JComponent c) { completeEditing(); prepareForUIUninstall(); uninstallDefaults(); uninstallListeners(); uninstallKeyboardActions(); uninstallComponents(); completeUIUninstall(); } protected void prepareForUIUninstall() { } protected void completeUIUninstall() { if(createdRenderer) { tree.setCellRenderer(null); } if(createdCellEditor) { tree.setCellEditor(null); } cellEditor = null; currentCellRenderer = null; rendererPane = null; componentListener = null; propertyChangeListener = null; mouseListener = null; focusListener = null; keyListener = null; setSelectionModel(null); treeState = null; drawingCache = null; selectionModelPropertyChangeListener = null; tree = null; treeModel = null; treeSelectionModel = null; treeSelectionListener = null; treeExpansionListener = null; } protected void uninstallDefaults() { if (tree.getTransferHandler() instanceof UIResource) { tree.setTransferHandler(null); } } protected void uninstallListeners() { if(componentListener != null) { tree.removeComponentListener(componentListener); } if (propertyChangeListener != null) { tree.removePropertyChangeListener(propertyChangeListener); } if (mouseListener != null) { tree.removeMouseListener(mouseListener); if (mouseListener instanceof MouseMotionListener) { tree.removeMouseMotionListener((MouseMotionListener)mouseListener); } } if (focusListener != null) { tree.removeFocusListener(focusListener); } if (keyListener != null) { tree.removeKeyListener(keyListener); } if(treeExpansionListener != null) { tree.removeTreeExpansionListener(treeExpansionListener); } if(treeModel != null && treeModelListener != null) { treeModel.removeTreeModelListener(treeModelListener); } if(selectionModelPropertyChangeListener != null && treeSelectionModel != null) { treeSelectionModel.removePropertyChangeListener (selectionModelPropertyChangeListener); } if(treeSelectionListener != null && treeSelectionModel != null) { treeSelectionModel.removeTreeSelectionListener (treeSelectionListener); } handler = null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(tree, null); SwingUtilities.replaceUIInputMap(tree, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, null); } /** * Uninstalls the renderer pane. */ protected void uninstallComponents() { if(rendererPane != null) { tree.remove(rendererPane); } } /** * Recomputes the right margin, and invalidates any tree states */ private void redoTheLayout() { if (treeState != null) { treeState.invalidateSizes(); } } /** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults(); Component renderer = (Component)lafDefaults.get( BASELINE_COMPONENT_KEY); if (renderer == null) { TreeCellRenderer tcr = createDefaultCellRenderer(); renderer = tcr.getTreeCellRendererComponent( tree, "a", false, false, false, -1, false); lafDefaults.put(BASELINE_COMPONENT_KEY, renderer); } int rowHeight = tree.getRowHeight(); int baseline; if (rowHeight > 0) { baseline = renderer.getBaseline(Integer.MAX_VALUE, rowHeight); } else { Dimension pref = renderer.getPreferredSize(); baseline = renderer.getBaseline(pref.width, pref.height); } return baseline + tree.getInsets().top; } /** * Returns an enum indicating how the baseline of the component * changes as the size changes. * * @throws NullPointerException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public Component.BaselineResizeBehavior getBaselineResizeBehavior( JComponent c) { super.getBaselineResizeBehavior(c); return Component.BaselineResizeBehavior.CONSTANT_ASCENT; } // // Painting routines. // public void paint(Graphics g, JComponent c) { if (tree != c) { throw new InternalError("incorrect component"); } // Should never happen if installed for a UI if(treeState == null) { return; } Rectangle paintBounds = g.getClipBounds(); Insets insets = tree.getInsets(); TreePath initialPath = getClosestPathForLocation (tree, 0, paintBounds.y); Enumeration paintingEnumerator = treeState.getVisiblePathsFrom (initialPath); int row = treeState.getRowForPath(initialPath); int endY = paintBounds.y + paintBounds.height; drawingCache.clear(); if(initialPath != null && paintingEnumerator != null) { TreePath parentPath = initialPath; // Draw the lines, knobs, and rows // Find each parent and have them draw a line to their last child parentPath = parentPath.getParentPath(); while(parentPath != null) { paintVerticalPartOfLeg(g, paintBounds, insets, parentPath); drawingCache.put(parentPath, Boolean.TRUE); parentPath = parentPath.getParentPath(); } boolean done = false; // Information for the node being rendered. boolean isExpanded; boolean hasBeenExpanded; boolean isLeaf; Rectangle boundsBuffer = new Rectangle(); Rectangle bounds; TreePath path; boolean rootVisible = isRootVisible(); while(!done && paintingEnumerator.hasMoreElements()) { path = (TreePath)paintingEnumerator.nextElement(); if(path != null) { isLeaf = treeModel.isLeaf(path.getLastPathComponent()); if(isLeaf) isExpanded = hasBeenExpanded = false; else { isExpanded = treeState.getExpandedState(path); hasBeenExpanded = tree.hasBeenExpanded(path); } bounds = getPathBounds(path, insets, boundsBuffer); if(bounds == null) // This will only happen if the model changes out // from under us (usually in another thread). // Swing isn't multithreaded, but I'll put this // check in anyway. return; // See if the vertical line to the parent has been drawn. parentPath = path.getParentPath(); if(parentPath != null) { if(drawingCache.get(parentPath) == null) { paintVerticalPartOfLeg(g, paintBounds, insets, parentPath); drawingCache.put(parentPath, Boolean.TRUE); } paintHorizontalPartOfLeg(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); } else if(rootVisible && row == 0) { paintHorizontalPartOfLeg(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); } if(shouldPaintExpandControl(path, row, isExpanded, hasBeenExpanded, isLeaf)) { paintExpandControl(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); } paintRow(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); if((bounds.y + bounds.height) >= endY) done = true; } else { done = true; } row++; } } paintDropLine(g); // Empty out the renderer pane, allowing renderers to be gc'ed. rendererPane.removeAll(); } private boolean isDropLine(JTree.DropLocation loc) { return loc != null && loc.getPath() != null && loc.getChildIndex() != -1; } private void paintDropLine(Graphics g) { JTree.DropLocation loc = tree.getDropLocation(); if (!isDropLine(loc)) { return; } Color c = UIManager.getColor("Tree.dropLineColor"); if (c != null) { g.setColor(c); Rectangle rect = getDropLineRect(loc); g.fillRect(rect.x, rect.y, rect.width, rect.height); } } private Rectangle getDropLineRect(JTree.DropLocation loc) { Rectangle rect = null; TreePath path = loc.getPath(); int index = loc.getChildIndex(); boolean ltr = leftToRight; Insets insets = tree.getInsets(); if (tree.getRowCount() == 0) { rect = new Rectangle(insets.left, insets.top, tree.getWidth() - insets.left - insets.right, 0); } else { int row = tree.getRowForPath(path); TreeModel model = getModel(); Object root = model.getRoot(); if (path.getLastPathComponent() == root && index >= model.getChildCount(root)) { rect = tree.getRowBounds(tree.getRowCount() - 1); rect.y = rect.y + rect.height; Rectangle xRect; if (!tree.isRootVisible()) { xRect = tree.getRowBounds(0); } else if (model.getChildCount(root) == 0){ xRect = tree.getRowBounds(0); xRect.x += totalChildIndent; xRect.width -= totalChildIndent + totalChildIndent; } else { TreePath lastChildPath = path.pathByAddingChild( model.getChild(root, model.getChildCount(root) - 1)); xRect = tree.getPathBounds(lastChildPath); } rect.x = xRect.x; rect.width = xRect.width; } else { rect = tree.getPathBounds(path.pathByAddingChild( model.getChild(path.getLastPathComponent(), index))); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -