📄 synthtreeui.java
字号:
/* * @(#)SynthTreeUI.java 1.23 04/05/27 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.synth;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.awt.dnd.*;import java.beans.*;import java.io.*;import java.util.*;import javax.swing.plaf.*;import javax.swing.plaf.basic.*;import javax.swing.tree.*;import javax.swing.text.Position;import sun.swing.plaf.synth.*;/** * Skinnable TreeUI. * * @version 1.23, 05/27/04 * @author Scott Violet */class SynthTreeUI extends BasicTreeUI implements PropertyChangeListener, SynthUI { private SynthStyle style; private SynthStyle cellStyle; private SynthContext paintContext; private boolean drawHorizontalLines; private boolean drawVerticalLines; private int leadRow; private int padding; private boolean useTreeColors; private Icon expandedIconWrapper; public static ComponentUI createUI(JComponent x) { return new SynthTreeUI(); } SynthTreeUI() { expandedIconWrapper = new ExpandedIconWrapper(); } public Icon getExpandedIcon() { return expandedIconWrapper; } protected void installDefaults() { updateStyle(tree); } private void updateStyle(JTree tree) { SynthContext context = getContext(tree, ENABLED); SynthStyle oldStyle = style; style = SynthLookAndFeel.updateStyle(context, this); if (style != oldStyle) { Object value; setExpandedIcon(style.getIcon(context, "Tree.expandedIcon")); setCollapsedIcon(style.getIcon(context, "Tree.collapsedIcon")); setLeftChildIndent(style.getInt(context, "Tree.leftChildIndent", 0)); setRightChildIndent(style.getInt(context, "Tree.rightChildIndent", 0)); drawHorizontalLines = style.getBoolean( context, "Tree.drawHorizontalLines",true); drawVerticalLines = style.getBoolean( context, "Tree.drawVerticalLines", true); value = style.get(context, "Tree.rowHeight"); if (value != null) { LookAndFeel.installProperty(tree, "rowHeight", value); } value = style.get(context, "Tree.scrollsOnExpand"); LookAndFeel.installProperty(tree, "scrollsOnExpand", value != null? value : Boolean.TRUE); padding = style.getInt(context, "Tree.padding", 0); largeModel = (tree.isLargeModel() && tree.getRowHeight() > 0); useTreeColors = style.getBoolean(context, "Tree.rendererUseTreeColors", true); Boolean showsRootHandles = style.getBoolean( context, "Tree.showsRootHandles", Boolean.TRUE); LookAndFeel.installProperty( tree, JTree.SHOWS_ROOT_HANDLES_PROPERTY, showsRootHandles); if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } context.dispose(); context = getContext(tree, Region.TREE_CELL, ENABLED); cellStyle = SynthLookAndFeel.updateStyle(context, this); context.dispose(); } protected void installListeners() { super.installListeners(); tree.addPropertyChangeListener(this); } public SynthContext getContext(JComponent c) { return getContext(c, getComponentState(c)); } private SynthContext getContext(JComponent c, int state) { return SynthContext.getContext(SynthContext.class, c, SynthLookAndFeel.getRegion(c), style, state); } private Region getRegion(JTree c) { return SynthLookAndFeel.getRegion(c); } private int getComponentState(JComponent c) { return SynthLookAndFeel.getComponentState(c); } private SynthContext getContext(JComponent c, Region region) { return getContext(c, region, getComponentState(c, region)); } private SynthContext getContext(JComponent c, Region region, int state) { return SynthContext.getContext(SynthContext.class, c, region, cellStyle, state); } private int getComponentState(JComponent c, Region region) { // Always treat the cell as selected, will be adjusted appropriately // when painted. return ENABLED | SELECTED; } protected TreeCellEditor createDefaultCellEditor() { TreeCellRenderer renderer = tree.getCellRenderer(); DefaultTreeCellEditor editor; if(renderer != null && (renderer instanceof DefaultTreeCellRenderer)) { editor = new SynthTreeCellEditor(tree, (DefaultTreeCellRenderer) renderer); } else { editor = new SynthTreeCellEditor(tree, null); } return editor; } protected TreeCellRenderer createDefaultCellRenderer() { return new SynthTreeCellRenderer(); } protected void uninstallDefaults() { SynthContext context = getContext(tree, ENABLED); style.uninstallDefaults(context); context.dispose(); style = null; context = getContext(tree, Region.TREE_CELL, ENABLED); cellStyle.uninstallDefaults(context); context.dispose(); cellStyle = null; if (tree.getTransferHandler() instanceof UIResource) { tree.setTransferHandler(null); } } protected void uninstallListeners() { super.uninstallListeners(); tree.removePropertyChangeListener(this); } public void update(Graphics g, JComponent c) { SynthContext context = getContext(c); SynthLookAndFeel.update(context, g); context.getPainter().paintTreeBackground(context, g, 0, 0, c.getWidth(), c.getHeight()); paint(context, g); context.dispose(); } public void paintBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { context.getPainter().paintTreeBorder(context, g, x, y, w, h); } public void paint(Graphics g, JComponent c) { SynthContext context = getContext(c); paint(context, g); context.dispose(); } private void adjustCellBounds(JTree tree, Rectangle bounds, Insets i){ if (bounds != null) { if (i == null) { i = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; } bounds.x += i.left; bounds.y += i.top; } } private void updateLeadRow() { leadRow = getRowForPath(tree, tree.getLeadSelectionPath()); } protected void paint(SynthContext context, Graphics g) { paintContext = context; updateLeadRow(); 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; TreeModel treeModel = tree.getModel(); SynthContext cellContext = getContext(tree, Region.TREE_CELL); drawingCache.clear(); setHashColor(context.getStyle().getColor(context, ColorType.FOREGROUND)); if (paintingEnumerator != null) { // First pass, draw the rows boolean done = false; boolean isExpanded; boolean hasBeenExpanded; boolean isLeaf; Rectangle boundsBuffer = new Rectangle(); Rectangle rowBounds = new Rectangle(0, 0, tree.getWidth(),0); Rectangle bounds; TreePath path; TreeCellRenderer renderer = tree.getCellRenderer(); DefaultTreeCellRenderer dtcr = (renderer instanceof DefaultTreeCellRenderer) ? (DefaultTreeCellRenderer) renderer : null; configureRenderer(cellContext); 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 = treeState.getBounds(path, boundsBuffer); adjustCellBounds(tree, bounds, insets); rowBounds.y = bounds.y; rowBounds.height = bounds.height; paintRow(renderer, dtcr, context, cellContext, g, paintBounds, insets, bounds, rowBounds, path, row, isExpanded, hasBeenExpanded, isLeaf); if ((bounds.y + bounds.height) >= endY) { done = true; } } else { done = true; } row++; } // Draw the connecting lines and controls. // Find each parent and have them draw a line to their last child boolean rootVisible = tree.isRootVisible(); TreePath parentPath = initialPath; parentPath = parentPath.getParentPath(); while (parentPath != null) { paintVerticalPartOfLeg(g, paintBounds, insets, parentPath); drawingCache.put(parentPath, Boolean.TRUE); parentPath = parentPath.getParentPath(); } done = false; paintingEnumerator = treeState.getVisiblePathsFrom(initialPath); while (!done && paintingEnumerator.hasMoreElements()) { path = (TreePath)paintingEnumerator.nextElement(); if (path != null) { isLeaf = treeModel.isLeaf(path.getLastPathComponent());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -