📄 synthtreeui.java
字号:
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))); } } if (rect.y != 0) { rect.y--; } if (!ltr) { rect.x = rect.x + rect.width - 100; } rect.width = 100; rect.height = 2; return rect; } private void configureRenderer(SynthContext context) { TreeCellRenderer renderer = tree.getCellRenderer(); if (renderer instanceof DefaultTreeCellRenderer) { DefaultTreeCellRenderer r = (DefaultTreeCellRenderer)renderer; SynthStyle style = context.getStyle(); context.setComponentState(ENABLED | SELECTED); Color color = r.getTextSelectionColor(); if (color == null || (color instanceof UIResource)) { r.setTextSelectionColor(style.getColor( context, ColorType.TEXT_FOREGROUND)); } color = r.getBackgroundSelectionColor(); if (color == null || (color instanceof UIResource)) { r.setBackgroundSelectionColor(style.getColor( context, ColorType.TEXT_BACKGROUND)); } context.setComponentState(ENABLED); color = r.getTextNonSelectionColor(); if (color == null || color instanceof UIResource) { r.setTextNonSelectionColor(style.getColorForState( context, ColorType.TEXT_FOREGROUND)); } color = r.getBackgroundNonSelectionColor(); if (color == null || color instanceof UIResource) { r.setBackgroundNonSelectionColor(style.getColorForState( context, ColorType.TEXT_BACKGROUND)); } } } @Override protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) { if (drawHorizontalLines) { super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); } } @Override protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) { paintContext.getStyle().getGraphicsUtils(paintContext).drawLine( paintContext, "Tree.horizontalLine", g, left, y, right, y, linesStyle); } @Override protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) { if (drawVerticalLines) { super.paintVerticalPartOfLeg(g, clipBounds, insets, path); } } @Override protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) { paintContext.getStyle().getGraphicsUtils(paintContext).drawLine( paintContext, "Tree.verticalLine", g, x, top, x, bottom, linesStyle); } protected void paintRow(TreeCellRenderer renderer, DefaultTreeCellRenderer dtcr, SynthContext treeContext, SynthContext cellContext, Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, Rectangle rowBounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) { // Don't paint the renderer if editing this row. boolean selected = tree.isRowSelected(row); JTree.DropLocation dropLocation = (JTree.DropLocation)tree.getDropLocation(); boolean isDrop = dropLocation != null && dropLocation.getChildIndex() == -1 && path == dropLocation.getPath(); int state = ENABLED; if (selected || isDrop) { state |= SELECTED; } if (tree.isFocusOwner() && row == leadRow) { state |= FOCUSED; } cellContext.setComponentState(state); if (dtcr != null && (dtcr.getBorderSelectionColor() instanceof UIResource)) { dtcr.setBorderSelectionColor(style.getColor( cellContext, ColorType.FOCUS)); } SynthLookAndFeel.updateSubregion(cellContext, g, rowBounds); cellContext.getPainter().paintTreeCellBackground(cellContext, g, rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height); cellContext.getPainter().paintTreeCellBorder(cellContext, g, rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height); if (editingComponent != null && editingRow == row) { return; } int leadIndex; if (tree.hasFocus()) { leadIndex = leadRow; } else { leadIndex = -1; } Component component = renderer.getTreeCellRendererComponent( tree, path.getLastPathComponent(), selected, isExpanded, isLeaf, row, (leadIndex == row)); rendererPane.paintComponent(g, component, tree, bounds.x, bounds.y, bounds.width, bounds.height, true); } private int findCenteredX(int x, int iconWidth) { return tree.getComponentOrientation().isLeftToRight() ? x - (int)Math.ceil(iconWidth / 2.0) : x - (int)Math.floor(iconWidth / 2.0); } /** * @inheritDoc */ @Override protected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) { //modify the paintContext's state to match the state for the row //this is a hack in that it requires knowledge of the subsequent //method calls. The point is, the context used in drawCentered //should reflect the state of the row, not of the tree. boolean isSelected = tree.getSelectionModel().isPathSelected(path); int state = paintContext.getComponentState(); if (isSelected) { paintContext.setComponentState(state | SynthConstants.SELECTED); } super.paintExpandControl(g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); paintContext.setComponentState(state); } @Override protected void drawCentered(Component c, Graphics graphics, Icon icon, int x, int y) { int w = SynthIcon.getIconWidth(icon, paintContext); int h = SynthIcon.getIconHeight(icon, paintContext); SynthIcon.paintIcon(icon, paintContext, graphics, findCenteredX(x, w), y - h/2, w, h); } public void propertyChange(PropertyChangeEvent event) { if (SynthLookAndFeel.shouldUpdateStyle(event)) { updateStyle((JTree)event.getSource()); } if ("dropLocation" == event.getPropertyName()) { JTree.DropLocation oldValue = (JTree.DropLocation)event.getOldValue(); repaintDropLocation(oldValue); repaintDropLocation(tree.getDropLocation()); } } private void repaintDropLocation(JTree.DropLocation loc) { if (loc == null) { return; } Rectangle r; if (isDropLine(loc)) { r = getDropLineRect(loc); } else { r = tree.getPathBounds(loc.getPath()); if (r != null) { r.x = 0; r.width = tree.getWidth(); } } if (r != null) { tree.repaint(r); } } @Override protected int getRowX(int row, int depth) { return super.getRowX(row, depth) + padding; } private class SynthTreeCellRenderer extends DefaultTreeCellRenderer implements UIResource { SynthTreeCellRenderer() { } @Override public String getName() { return "Tree.cellRenderer"; } @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { if (!useTreeColors && (sel || hasFocus)) { SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel. getUIOfType(getUI(), SynthLabelUI.class), sel, hasFocus, tree.isEnabled(), false); } else { SynthLookAndFeel.resetSelectedUI(); } return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); } @Override public void paint(Graphics g) { paintComponent(g); if (hasFocus) { SynthContext context = getContext(tree, Region.TREE_CELL); if (context.getStyle() == null) { assert false: "SynthTreeCellRenderer is being used " + "outside of UI that created it"; return; } int imageOffset = 0; Icon currentI = getIcon(); if(currentI != null && getText() != null) { imageOffset = currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1); } if (selected) { context.setComponentState(ENABLED | SELECTED); } else { context.setComponentState(ENABLED); } if(getComponentOrientation().isLeftToRight()) { context.getPainter().paintTreeCellFocus(context, g, imageOffset, 0, getWidth() - imageOffset, getHeight()); } else { context.getPainter().paintTreeCellFocus(context, g, 0, 0, getWidth() - imageOffset, getHeight()); } context.dispose(); } SynthLookAndFeel.resetSelectedUI(); } } private static class SynthTreeCellEditor extends DefaultTreeCellEditor { public SynthTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) { super(tree, renderer); setBorderSelectionColor(null); } @Override protected TreeCellEditor createTreeCellEditor() { JTextField tf = new JTextField() { @Override public String getName() { return "Tree.cellEditor"; } }; DefaultCellEditor editor = new DefaultCellEditor(tf); // One click to edit. editor.setClickCountToStart(1); return editor; } } // // BasicTreeUI directly uses expandIcon outside of the Synth methods. // To get the correct context we return an instance of this that fetches // the SynthContext as needed. // private class ExpandedIconWrapper extends SynthIcon { public void paintIcon(SynthContext context, Graphics g, int x, int y, int w, int h) { if (context == null) { context = getContext(tree); SynthIcon.paintIcon(expandedIcon, context, g, x, y, w, h); context.dispose(); } else { SynthIcon.paintIcon(expandedIcon, context, g, x, y, w, h); } } public int getIconWidth(SynthContext context) { int width; if (context == null) { context = getContext(tree); width = SynthIcon.getIconWidth(expandedIcon, context); context.dispose(); } else { width = SynthIcon.getIconWidth(expandedIcon, context); } return width; } public int getIconHeight(SynthContext context) { int height; if (context == null) { context = getContext(tree); height = SynthIcon.getIconHeight(expandedIcon, context); context.dispose(); } else { height = SynthIcon.getIconHeight(expandedIcon, context); } return height; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -