Index: javax/swing/JTree.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/JTree.java,v retrieving revision 1.25 diff -u -r1.25 JTree.java --- javax/swing/JTree.java 30 Jun 2005 14:42:57 -0000 1.25 +++ javax/swing/JTree.java 30 Jun 2005 20:13:01 -0000 @@ -1079,6 +1079,13 @@ if (path != null) selectionModel.addSelectionPath(path); + else + { + selectionModel.clearSelection(); + // need to repaint because cant fire an event with + // null selection. + repaint(); + } } public void addSelectionRows(int[] rows) Index: javax/swing/plaf/basic/BasicTreeUI.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTreeUI.java,v retrieving revision 1.12 diff -u -r1.12 BasicTreeUI.java --- javax/swing/plaf/basic/BasicTreeUI.java 30 Jun 2005 14:42:56 -0000 1.12 +++ javax/swing/plaf/basic/BasicTreeUI.java 30 Jun 2005 20:13:04 -0000 @@ -37,6 +37,8 @@ import java.awt.Color; import java.awt.Component; import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; @@ -85,6 +87,7 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellEditor; import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.SwingUtilities; import javax.swing.tree.TreeCellEditor; import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreeSelectionModel; @@ -1723,12 +1726,16 @@ { Point click = e.getPoint(); int row = ((int) click.getY() / getRowHeight()) - 1; - + if (BasicTreeUI.this.tree.isRowSelected(row)) BasicTreeUI.this.tree.removeSelectionRow(row); else if (BasicTreeUI.this.tree.getSelectionModel() .getSelectionMode() == treeSelectionModel.SINGLE_TREE_SELECTION) + { + // clear selection, since only able to select one row at a time. + BasicTreeUI.this.tree.getSelectionModel().clearSelection(); BasicTreeUI.this.tree.addSelectionRow(row); + } // FIXME: add in selection for more than 1 row, or an entire // path } @@ -2257,6 +2264,21 @@ /* * HELPER METHODS FOR PAINTING * */ /** + * Returns the cell bounds for painting selected cells + * + * @returns Rectangle that represents the cell bounds + */ + private Rectangle getCellBounds(int x, int y, Object cell) + { + String s = cell.toString(); + Font f = tree.getFont(); + FontMetrics fm = tree.getToolkit().getFontMetrics(tree.getFont()); + + return new Rectangle(x, y, SwingUtilities.computeStringWidth(fm, s), fm.getHeight()); + + } + + /** * Paints a leaf in the tree * * @param g the Graphics context in which to paint @@ -2270,21 +2292,22 @@ TreePath tp = new TreePath(((DefaultMutableTreeNode) leaf).getPath()); boolean selected = tree.isPathSelected(tp); - Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree, - leaf, selected, false, true, 0, false); - if (selected) { Component comp = tree.getCellRenderer() .getTreeCellRendererComponent(tree, leaf, true, false, true, 0, false); - rendererPane.paintComponent(g, comp, tree, new Rectangle(x, y, 10, - 25)); + rendererPane.paintComponent(g, comp, tree, getCellBounds(x, y, leaf)); + } + else + { + Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree, + leaf, false, false, true, 0, false); + + g.translate(x, y); + c.paint(g); + g.translate(-x, -y); } - - g.translate(x, y); - c.paint(g); - g.translate(-x, -y); } /** @@ -2302,20 +2325,22 @@ TreePath tp = new TreePath(((DefaultMutableTreeNode) nonLeaf).getPath()); boolean selected = tree.isPathSelected(tp); - Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree, - nonLeaf, selected, false, false, 0, false); - if (selected) { Component comp = tree.getCellRenderer() .getTreeCellRendererComponent(tree, nonLeaf, true, false, true, 0, false); - rendererPane.paintComponent(g, comp, tree, new Rectangle(x, y, 10, - 25)); + rendererPane.paintComponent(g, comp, tree, getCellBounds(x, y, nonLeaf)); + } + else + { + Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree, + nonLeaf, false, false, false, 0, false); + + g.translate(x, y); + c.paint(g); + g.translate(-x, -y); } - g.translate(x, y); - c.paint(g); - g.translate(-x, -y); } /** Index: javax/swing/tree/DefaultTreeCellRenderer.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/tree/DefaultTreeCellRenderer.java,v retrieving revision 1.6 diff -u -r1.6 DefaultTreeCellRenderer.java --- javax/swing/tree/DefaultTreeCellRenderer.java 30 Jun 2005 14:42:57 -0000 1.6 +++ javax/swing/tree/DefaultTreeCellRenderer.java 30 Jun 2005 20:13:04 -0000 @@ -116,6 +116,7 @@ * borderSelectionColor */ protected Color borderSelectionColor; + // ------------------------------------------------------------- // Initialization --------------------------------------------- @@ -380,18 +381,43 @@ this.hasFocus = hasFocus; if (leaf) - setIcon(getLeafIcon()); + setLeafIcon(getLeafIcon()); else if (expanded) - setIcon(getOpenIcon()); + setOpenIcon(getOpenIcon()); else - setIcon(getClosedIcon()); + setClosedIcon(getClosedIcon()); setText(val.toString()); setHorizontalAlignment(LEFT); + setOpaque(true); setVerticalAlignment(TOP); + setEnabled(true); + setFont(getFont()); + if (selected) + { + super.setBackground(getBackgroundSelectionColor()); + super.setForeground(getTextSelectionColor()); + } + else + { + super.setBackground((tree.getParent()).getBackground()); + super.setForeground(getTextNonSelectionColor()); + } + + return this; } + + /** + * getFont + * + * @return the current Font + */ + public Font getFont() + { + return super.getFont(); + } /** * paint Index: javax/swing/tree/DefaultTreeSelectionModel.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/tree/DefaultTreeSelectionModel.java,v retrieving revision 1.14 diff -u -r1.14 DefaultTreeSelectionModel.java --- javax/swing/tree/DefaultTreeSelectionModel.java 30 Jun 2005 14:42:57 -0000 1.14 +++ javax/swing/tree/DefaultTreeSelectionModel.java 30 Jun 2005 20:13:04 -0000 @@ -291,25 +291,29 @@ */ public void addSelectionPaths(TreePath[] value0) { - TreePath v0 = null; - for (int i = 0; i < value0.length; i++) + if (value0 != null) { - v0 = value0[i]; - if (!isPathSelected(v0)) + TreePath v0 = null; + for (int i = 0; i < value0.length; i++) { - if (isSelectionEmpty()) - setSelectionPath(v0); - else + v0 = value0[i]; + if (!isPathSelected(v0)) { - TreePath[] temp = new TreePath[selection.length + 1]; - System.arraycopy(selection, 0, temp, 0, selection.length); - temp[temp.length - 1] = v0; - selection = new TreePath[temp.length]; - System.arraycopy(temp, 0, selection, 0, temp.length); - } + if (isSelectionEmpty()) + setSelectionPath(v0); + else + { + TreePath[] temp = new TreePath[selection.length + 1]; + System.arraycopy(selection, 0, temp, 0, + selection.length); + temp[temp.length - 1] = v0; + selection = new TreePath[temp.length]; + System.arraycopy(temp, 0, selection, 0, temp.length); + } - fireValueChanged(new TreeSelectionEvent(this, v0, true, - leadPath, value0[0])); + fireValueChanged(new TreeSelectionEvent(this, v0, true, + leadPath, value0[0])); + } } } } @@ -357,30 +361,33 @@ */ public void removeSelectionPaths(TreePath[] value0) { - int index = -1; - TreePath v0 = null; - for (int i = 0; i < value0.length; i++) + if (value0 != null) { - v0 = value0[i]; - if (isPathSelected(v0)) + int index = -1; + TreePath v0 = null; + for (int i = 0; i < value0.length; i++) { - for (int x = 0; x < selection.length; x++) + v0 = value0[i]; + if (isPathSelected(v0)) { - if (selection[i].equals(v0)) + for (int x = 0; x < selection.length; x++) { - index = x; - break; + if (selection[i].equals(v0)) + { + index = x; + break; + } } - } - TreePath[] temp = new TreePath[selection.length - 1]; - System.arraycopy(selection, 0, temp, 0, index); - System.arraycopy(selection, index + 1, temp, index, - selection.length - index - 1); - selection = new TreePath[temp.length]; - System.arraycopy(temp, 0, selection, 0, temp.length); + TreePath[] temp = new TreePath[selection.length - 1]; + System.arraycopy(selection, 0, temp, 0, index); + System.arraycopy(selection, index + 1, temp, index, + selection.length - index - 1); + selection = new TreePath[temp.length]; + System.arraycopy(temp, 0, selection, 0, temp.length); - fireValueChanged(new TreeSelectionEvent(this, v0, false, - leadPath, value0[0])); + fireValueChanged(new TreeSelectionEvent(this, v0, false, + leadPath, value0[0])); + } } } }