Same as the previous patch, just a slight change in a comment for
getCellBounds



On Thu, 2005-06-30 at 16:21 -0400, Lillian Angel wrote:
> Now when you click on a node in the JTree it actually highlights it.
> patch attached, approval needed.
> 
> 2005-06-30  Lillian Angel  <[EMAIL PROTECTED]>
> 
>       * javax/swing/JTree.java
>               (addSelectionPath): if mouse click somewhere other than 
>               a row, all selections are removed
>       * javax/swing/plaf/basic/BasicTreeUI.java
>               (mouseClicked): if mouse clicked on a row, all other 
>               selections are cleared
>               (getCellBounds): Implemented
>               (paintLeaf): paints with cell bounds
>               (paintNonLeaf): paints with cell bounds
>       * javax/swing/tree/DefaultTreeCellRenderer.java:
>               (getTreeCellRendererComponent): changed color of 
>               selected row
>               (getFont): Implemented
>       * javax/swing/tree/DefaultTreeSelectionModel.java:
>               (addSelectionPaths): check if parameter is null
>               (removeSelectionPaths): check if parameter is null
> _______________________________________________
> Classpath-patches mailing list
> [email protected]
> http://lists.gnu.org/mailman/listinfo/classpath-patches
Index: javax/swing/JTree.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTree.java,v
retrieving revision 1.26
diff -u -r1.26 JTree.java
--- javax/swing/JTree.java	2 Jul 2005 20:32:49 -0000	1.26
+++ javax/swing/JTree.java	4 Jul 2005 13:52:17 -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.13
diff -u -r1.13 BasicTreeUI.java
--- javax/swing/plaf/basic/BasicTreeUI.java	2 Jul 2005 20:32:50 -0000	1.13
+++ javax/swing/plaf/basic/BasicTreeUI.java	4 Jul 2005 13:52:19 -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,25 @@
 	/* * HELPER METHODS FOR PAINTING * */
 
 	/**
+	 * Returns the cell bounds for painting selected cells
+	 * 
+	 * @param x is the x location of the cell
+	 * @param y is the y location of the cell
+	 * @param cell is the Object to get the bounds for
+	 * 
+	 * @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 +2296,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 +2329,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.7
diff -u -r1.7 DefaultTreeCellRenderer.java
--- javax/swing/tree/DefaultTreeCellRenderer.java	2 Jul 2005 20:32:52 -0000	1.7
+++ javax/swing/tree/DefaultTreeCellRenderer.java	4 Jul 2005 13:52:19 -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.15
diff -u -r1.15 DefaultTreeSelectionModel.java
--- javax/swing/tree/DefaultTreeSelectionModel.java	2 Jul 2005 20:32:52 -0000	1.15
+++ javax/swing/tree/DefaultTreeSelectionModel.java	4 Jul 2005 13:52:19 -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]));
+				}
 			}
 		}
 	}
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to