📄 basictreeui.java
字号:
/** * Creates and returns a new ComponentHandler. This is used for the large * model to mark the validCachedPreferredSize as invalid when the component * moves. * * @return a new ComponentHandler. */ protected ComponentListener createComponentListener() { return new ComponentHandler(); } /** * Creates and returns the object responsible for updating the treestate when * a nodes expanded state changes. * * @return the TreeExpansionListener responsible for updating the treestate */ protected TreeExpansionListener createTreeExpansionListener() { return new TreeExpansionHandler(); } /** * Creates the object responsible for managing what is expanded, as well as * the size of nodes. * * @return the object responsible for managing what is expanded. */ protected AbstractLayoutCache createLayoutCache() { return new FixedHeightLayoutCache(); } /** * Returns the renderer pane that renderer components are placed in. * * @return the rendererpane that render components are placed in. */ protected CellRendererPane createCellRendererPane() { return new CellRendererPane(); } /** * Creates a default cell editor. * * @return the default cell editor. */ protected TreeCellEditor createDefaultCellEditor() { if (currentCellRenderer != null) return new DefaultTreeCellEditor(tree, (DefaultTreeCellRenderer) currentCellRenderer, cellEditor); return new DefaultTreeCellEditor(tree, (DefaultTreeCellRenderer) createDefaultCellRenderer(), cellEditor); } /** * Returns the default cell renderer that is used to do the stamping of each * node. * * @return 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. * * @return a listener that can update the tree when the model changes. */ protected TreeModelListener createTreeModelListener() { return new TreeModelHandler(); } /** * Uninstall all registered listeners */ protected void uninstallListeners() { tree.removePropertyChangeListener(propertyChangeListener); tree.removeFocusListener(focusListener); tree.removeTreeSelectionListener(treeSelectionListener); tree.removeMouseListener(mouseListener); tree.removeKeyListener(keyListener); tree.removePropertyChangeListener(selectionModelPropertyChangeListener); tree.removeComponentListener(componentListener); tree.removeTreeExpansionListener(treeExpansionListener); TreeCellEditor tce = tree.getCellEditor(); if (tce != null) tce.removeCellEditorListener(cellEditorListener); if (treeModel != null) treeModel.removeTreeModelListener(treeModelListener); } /** * Uninstall all keyboard actions. */ protected void uninstallKeyboardActions() { action = null; tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).setParent(null); tree.getActionMap().setParent(null); } /** * Uninstall the rendererPane. */ protected void uninstallComponents() { currentCellRenderer = null; rendererPane = null; createdRenderer = false; setCellRenderer(currentCellRenderer); } /** * The vertical element of legs between nodes starts at the bottom of the * parent node by default. This method makes the leg start below that. * * @return the vertical leg buffer */ protected int getVerticalLegBuffer() { return getRowHeight() / 2; } /** * The horizontal element of legs between nodes starts at the right of the * left-hand side of the child node by default. This method makes the leg end * before that. * * @return the horizontal leg buffer */ protected int getHorizontalLegBuffer() { return rightChildIndent / 2; } /** * Make all the nodes that are expanded in JTree expanded in LayoutCache. This * invokes updateExpandedDescendants with the root path. */ protected void updateLayoutCacheExpandedNodes() { if (treeModel != null) updateExpandedDescendants(new TreePath(treeModel.getRoot())); } /** * Updates the expanded state of all the descendants of the <code>path</code> * by getting the expanded descendants from the tree and forwarding to the * tree state. * * @param path * the path used to update the expanded states */ protected void updateExpandedDescendants(TreePath path) { Enumeration expanded = tree.getExpandedDescendants(path); while (expanded.hasMoreElements()) treeState.setExpandedState(((TreePath) expanded.nextElement()), true); } /** * Returns a path to the last child of <code>parent</code> * * @param parent * is the topmost path to specified * @return a path to the last child of parent */ protected TreePath getLastChildPath(TreePath parent) { return ((TreePath) parent.getLastPathComponent()); } /** * Updates how much each depth should be offset by. */ protected void updateDepthOffset() { depthOffset += getVerticalLegBuffer(); } /** * Updates the cellEditor based on editability of the JTree that we're * contained in. If the tree is editable but doesn't have a cellEditor, a * basic one will be used. */ protected void updateCellEditor() { if (tree.isEditable() && cellEditor == null) setCellEditor(createDefaultCellEditor()); createdCellEditor = true; } /** * Messaged from the tree we're in when the renderer has changed. */ protected void updateRenderer() { if (tree != null) { if(tree.getCellRenderer() == null) { if(currentCellRenderer == null) currentCellRenderer = createDefaultCellRenderer(); tree.setCellRenderer(currentCellRenderer); } } } /** * Resets the treeState instance based on the tree we're providing the look * and feel for. */ protected void configureLayoutCache() { treeState = createLayoutCache(); } /** * Marks the cached size as being invalid, and messages the tree with * <code>treeDidChange</code>. */ protected void updateSize() { preferredSize = null; updateCachedPreferredSize(); tree.treeDidChange(); } /** * Updates the <code>preferredSize</code> instance variable, which is * returned from <code>getPreferredSize()</code>. */ protected void updateCachedPreferredSize() { int maxWidth = 0; boolean isLeaf = false; if (currentVisiblePath != null) { Object[] path = currentVisiblePath.getPath(); for (int i = 0; i < path.length; i++) { TreePath curr = new TreePath(getPathToRoot(path[i], 0)); Rectangle bounds = getPathBounds(tree, curr); if (treeModel != null) isLeaf = treeModel.isLeaf(path[i]); if (!isLeaf && hasControlIcons()) bounds.width += getCurrentControlIcon(curr).getIconWidth(); maxWidth = Math.max(maxWidth, bounds.x + bounds.width); } preferredSize = new Dimension(maxWidth, (getRowHeight() * path.length)); } else preferredSize = new Dimension(0, 0); validCachedPreferredSize = true; } /** * Messaged from the VisibleTreeNode after it has been expanded. * * @param path * is the path that has been expanded. */ protected void pathWasExpanded(TreePath path) { validCachedPreferredSize = false; tree.revalidate(); tree.repaint(); } /** * Messaged from the VisibleTreeNode after it has collapsed */ protected void pathWasCollapsed(TreePath path) { validCachedPreferredSize = false; tree.revalidate(); tree.repaint(); } /** * Install all defaults for the tree. */ protected void installDefaults() { LookAndFeel.installColorsAndFont(tree, "Tree.background", "Tree.foreground", "Tree.font"); tree.setOpaque(true); rightChildIndent = UIManager.getInt("Tree.rightChildIndent"); leftChildIndent = UIManager.getInt("Tree.leftChildIndent"); setRowHeight(UIManager.getInt("Tree.rowHeight")); tree.setRowHeight(getRowHeight()); tree.requestFocusInWindow(false); tree.setScrollsOnExpand(UIManager.getBoolean("Tree.scrollsOnExpand")); setExpandedIcon(UIManager.getIcon("Tree.expandedIcon")); setCollapsedIcon(UIManager.getIcon("Tree.collapsedIcon")); } /** * Install all keyboard actions for this */ protected void installKeyboardActions() { InputMap focusInputMap = (InputMap) UIManager.get("Tree.focusInputMap"); InputMapUIResource parentInputMap = new InputMapUIResource(); ActionMap parentActionMap = new ActionMapUIResource(); action = new TreeAction(); Object keys[] = focusInputMap.allKeys(); for (int i = 0; i < keys.length; i++) { parentInputMap.put( KeyStroke.getKeyStroke( ((KeyStroke) keys[i]).getKeyCode(), convertModifiers(((KeyStroke) keys[i]).getModifiers())), (String) focusInputMap.get((KeyStroke) keys[i])); parentInputMap.put( KeyStroke.getKeyStroke( ((KeyStroke) keys[i]).getKeyCode(), ((KeyStroke) keys[i]).getModifiers()), (String) focusInputMap.get((KeyStroke) keys[i])); parentActionMap.put( (String) focusInputMap.get((KeyStroke) keys[i]), new ActionListenerProxy( action, (String) focusInputMap.get((KeyStroke) keys[i]))); } parentInputMap.setParent(tree.getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent()); parentActionMap.setParent(tree.getActionMap().getParent()); tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).setParent( parentInputMap); tree.getActionMap().setParent(parentActionMap); } /** * Converts the modifiers. * * @param mod - * modifier to convert * @returns the new modifier */ private int convertModifiers(int mod) { if ((mod & KeyEvent.SHIFT_DOWN_MASK) != 0) { mod |= KeyEvent.SHIFT_MASK; mod &= ~KeyEvent.SHIFT_DOWN_MASK; } if ((mod & KeyEvent.CTRL_DOWN_MASK) != 0) { mod |= KeyEvent.CTRL_MASK; mod &= ~KeyEvent.CTRL_DOWN_MASK; } if ((mod & KeyEvent.META_DOWN_MASK) != 0) { mod |= KeyEvent.META_MASK; mod &= ~KeyEvent.META_DOWN_MASK; } if ((mod & KeyEvent.ALT_DOWN_MASK) != 0) { mod |= KeyEvent.ALT_MASK; mod &= ~KeyEvent.ALT_DOWN_MASK; } if ((mod & KeyEvent.ALT_GRAPH_DOWN_MASK) != 0) { mod |= KeyEvent.ALT_GRAPH_MASK; mod &= ~KeyEvent.ALT_GRAPH_DOWN_MASK; } return mod; } /** * Install all listeners for this */ protected void installListeners() { tree.addPropertyChangeListener(propertyChangeListener); tree.addFocusListener(focusListener); tree.addTreeSelectionListener(treeSelectionListener); tree.addMouseListener(mouseListener); tree.addKeyListener(keyListener); tree.addPropertyChangeListener(selectionModelPropertyChangeListener); tree.addComponentListener(componentListener); tree.addTreeExpansionListener(treeExpansionListener); if (treeModel != null) treeModel.addTreeModelListener(treeModelListener); } /** * Install the UI for the component * * @param c * the component to install UI for */ public void installUI(JComponent c) { tree = (JTree) c; prepareForUIInstall(); super.installUI(c); installDefaults(); installComponents(); installKeyboardActions(); installListeners(); setCellEditor(createDefaultCellEditor()); createdCellEditor = true; isEditing = false; TreeModel mod = tree.getModel(); setModel(mod); if (mod != null) { TreePath path = new TreePath(mod.getRoot()); if (!tree.isExpanded(path)) toggleExpandState(path); } treeSelectionModel = tree.getSelectionModel(); completeUIInstall(); } /** * Uninstall the defaults for the tree */ protected void uninstallDefaults() { tree.setFont(null); tree.setForeground(null); tree.setBackground(null); } /** * Uninstall the UI for the component * * @param c * the component to uninstall UI for */ public void uninstallUI(JComponent c) { prepareForUIUninstall(); uninstallDefaults(); uninstallKeyboardActions(); uninstallListeners(); tree = null; uninstallComponents();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -