From 2799793789d5b66c90efbd1a3eb56e0c1ec57f88 Mon Sep 17 00:00:00 2001 From: Andrew John Hughes Date: Thu, 5 Oct 2006 02:44:18 +0000 Subject: 2006-10-05 Andrew John Hughes * Merge of HEAD-->generics for 2006/09/03-2006/10/04. --- javax/swing/tree/DefaultTreeCellRenderer.java | 92 +++++++++++++++---------- javax/swing/tree/DefaultTreeModel.java | 27 ++++++-- javax/swing/tree/VariableHeightLayoutCache.java | 6 ++ 3 files changed, 83 insertions(+), 42 deletions(-) (limited to 'javax/swing/tree') diff --git a/javax/swing/tree/DefaultTreeCellRenderer.java b/javax/swing/tree/DefaultTreeCellRenderer.java index 26a30f0cc..3766485ab 100644 --- a/javax/swing/tree/DefaultTreeCellRenderer.java +++ b/javax/swing/tree/DefaultTreeCellRenderer.java @@ -77,7 +77,7 @@ public class DefaultTreeCellRenderer protected boolean hasFocus; /** - * drawsFocusBorderAroundIcon // FIXME: is this used? + * Indicates if the focus border is also drawn around the icon. */ private boolean drawsFocusBorderAroundIcon; @@ -152,6 +152,8 @@ public class DefaultTreeCellRenderer setBackgroundNonSelectionColor(UIManager.getColor("Tree.textBackground")); setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground")); setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor")); + Object val = UIManager.get("Tree.drawsFocusBorderAroundIcon"); + drawsFocusBorderAroundIcon = val != null && ((Boolean) val).booleanValue(); } /** @@ -499,47 +501,65 @@ public class DefaultTreeCellRenderer */ public void paint(Graphics g) { - // paint background - Rectangle vr = new Rectangle(); - Rectangle ir = new Rectangle(); - Rectangle tr = new Rectangle(); - - Insets insets = new Insets(0, 0, 0, 0); - Border border = UIManager.getBorder("Tree.selectionBorder"); - if (border != null) - insets = border.getBorderInsets(this); - - FontMetrics fm = getToolkit().getFontMetrics(getFont()); - SwingUtilities.layoutCompoundLabel((JLabel) this, fm, getText(), - getIcon(), getVerticalAlignment(), - getHorizontalAlignment(), - getVerticalTextPosition(), - getHorizontalTextPosition(), vr, ir, tr, - getIconTextGap()); - - // Reusing one rectangle. - Rectangle bounds = getBounds(ir); - - bounds.x = tr.x - insets.left; - bounds.width = tr.width + insets.left + insets.right; - - g.setColor(super.getBackground()); - g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); + // Determine background color. + Color bgColor; + if (selected) + bgColor = getBackgroundSelectionColor(); + else + { + bgColor = getBackgroundNonSelectionColor(); + if (bgColor == null) + bgColor = getBackground(); + } + // Paint background. + int xOffset = -1; + if (bgColor != null) + { + Icon i = getIcon(); + xOffset = getXOffset(); + g.setColor(bgColor); + g.fillRect(xOffset, 0, getWidth() - xOffset, getHeight()); + } - super.paint(g); - - // Paint the border of the focused element only (lead selection) if (hasFocus) { - Color b = getBorderSelectionColor(); - if (b != null) - { - g.setColor(b); - g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1); - } + if (drawsFocusBorderAroundIcon) + xOffset = 0; + else if (xOffset == -1) + xOffset = getXOffset(); + paintFocus(g, xOffset, 0, getWidth() - xOffset, getHeight()); + } + super.paint(g); + } + + /** + * Paints the focus indicator. + */ + private void paintFocus(Graphics g, int x, int y, int w, int h) + { + Color col = getBorderSelectionColor(); + if (col != null) + { + g.setColor(col); + g.drawRect(x, y, w - 1, h - 1); } } + /** + * Determines the X offset of the label that is caused by + * the icon. + * + * @return the X offset of the label + */ + private int getXOffset() + { + Icon i = getIcon(); + int offs = 0; + if (i != null && getText() != null) + offs = i.getIconWidth() + Math.max(0, getIconTextGap() - 1); + return offs; + } + /** * Returns the preferred size of the cell. * diff --git a/javax/swing/tree/DefaultTreeModel.java b/javax/swing/tree/DefaultTreeModel.java index e8cbf4bb4..afee7ea22 100644 --- a/javax/swing/tree/DefaultTreeModel.java +++ b/javax/swing/tree/DefaultTreeModel.java @@ -210,17 +210,32 @@ public class DefaultTreeModel } /** - * isLeaf + * Returns if the specified node is a leaf or not. When + * {@link #asksAllowsChildren} is true, then this checks if the TreeNode + * allows children, otherwise it returns the TreeNode's leaf + * property. * - * @param node TODO - * @return boolean + * @param node the node to check + * + * @return boolean true if the node is a leaf node, + * false otherwise + * + * @throws ClassCastException if the specified node is not a + * TreeNode instance + * + * @see TreeNode#getAllowsChildren() + * @see TreeNode#isLeaf() */ public boolean isLeaf(Object node) { - if (node instanceof TreeNode) - return ((TreeNode) node).isLeaf(); + // The RI throws a ClassCastException when node isn't a TreeNode, so do we. + TreeNode treeNode = (TreeNode) node; + boolean leaf; + if (asksAllowsChildren) + leaf = ! treeNode.getAllowsChildren(); else - return true; + leaf = treeNode.isLeaf(); + return leaf; } /** diff --git a/javax/swing/tree/VariableHeightLayoutCache.java b/javax/swing/tree/VariableHeightLayoutCache.java index 03251eb1f..b256f1fe9 100644 --- a/javax/swing/tree/VariableHeightLayoutCache.java +++ b/javax/swing/tree/VariableHeightLayoutCache.java @@ -339,6 +339,12 @@ public class VariableHeightLayoutCache return null; if (dirty) update(); + + // The RI allows null arguments for rect, in which case a new Rectangle + // is created. + if (rect == null) + rect = new Rectangle(); + Object last = path.getLastPathComponent(); NodeRecord r = (NodeRecord) nodes.get(last); if (r == null) -- cgit v1.2.1