Here is a new patch for these classes. I implemented it so the user can
select paths/rows in the JTree. But still, but visual testing you cannot
see this just yet. I am working on the painting aspect now. In light of
not submitting a massive patch, here is what I have done so far:

Patch attached. Approval Pending.


2005-06-29  Lillian Angel  <[EMAIL PROTECTED]>

        * javax/swing/JTree.java:
        (valueChanged): repaint everytime something is selected in the 
        tree.
        (setSelectionModel): set TreeSelectionListeners
        (isRowSelected): isRowSelected uses isPathSelected
        * javax/swing/plaf/basic/BasicTreeUI.java
        (setSelectionModel): works with JTree to set the selection model
        (getPathForRow): took out unneeded lines
        (mouseClicked): switched if statements, should check if already 
        selected first.
        (paintLeaf): checked selection of Leaf
        (paintNonLeaf): checked selection of nonLeaf
        (paintRecursive): updated to work with other paint functions
        * javax/swing/tree/DefaultTreeSelectionModel.java:
        (addSelectionPath): fire change, so selection is registered to 
        listener
        (addSelectionPaths): fire change, so selection is registered to 
        listener
        (removeSelectionPath): fire change, so selection is registered 
        to listener
        (removeSelectionPaths): fire change, so selection is registered 
        to listener
        (isPathSelected): took out unneeded lines
        (getTreeSelectionListeners): made more logical to call 
        getListeners
        (fireValueChanged): fixed loop to be more logical
        
        
        
        
Index: javax/swing/JTree.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTree.java,v
retrieving revision 1.24
diff -u -r1.24 JTree.java
--- javax/swing/JTree.java	29 Jun 2005 13:20:01 -0000	1.24
+++ javax/swing/JTree.java	29 Jun 2005 18:30:55 -0000
@@ -172,6 +172,8 @@
       TreeSelectionEvent rewritten =
         (TreeSelectionEvent) ev.cloneWithSource(JTree.this);
       fireValueChanged(rewritten);
+      JTree.this.revalidate();
+      JTree.this.repaint();
     }
   } // TreeSelectionRedirector
 
@@ -889,11 +891,20 @@
   public void setSelectionModel(TreeSelectionModel model)
   {
     if (selectionModel == model)
-      return;
-    
-    TreeSelectionModel oldValue = selectionModel;
-    selectionModel = model;
-    firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model);
+        return;
+      
+      if (selectionModel != null)
+        selectionModel.removeTreeSelectionListener(selectionRedirector);
+      
+      TreeSelectionModel oldValue = selectionModel;
+      selectionModel = model;
+      
+      if (selectionModel != null)
+        selectionModel.addTreeSelectionListener(selectionRedirector);
+
+      firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model);
+      revalidate();
+      repaint();
   }
 
   public int getVisibleRowCount()
@@ -1221,7 +1232,7 @@
 
   public boolean isRowSelected(int row)
   {
-    return selectionModel.isRowSelected(row);
+    return selectionModel.isPathSelected(getPathForRow(row));
   }
 
   public boolean isSelectionEmpty()
Index: javax/swing/plaf/basic/BasicTreeUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTreeUI.java,v
retrieving revision 1.11
diff -u -r1.11 BasicTreeUI.java
--- javax/swing/plaf/basic/BasicTreeUI.java	29 Jun 2005 13:25:24 -0000	1.11
+++ javax/swing/plaf/basic/BasicTreeUI.java	29 Jun 2005 18:30:55 -0000
@@ -564,12 +564,7 @@
 	{
 		if (newLSM != null)
 		{
-			tree
-					.removePropertyChangeListener(selectionModelPropertyChangeListener);
 			treeSelectionModel = newLSM;
-			treeState.setSelectionModel(newLSM);
-			tree
-					.addPropertyChangeListener(selectionModelPropertyChangeListener);
 			tree.setSelectionModel(treeSelectionModel);
 		}
 	}
@@ -619,6 +614,8 @@
 			if (pathForRow != null)
 				pathForRow = pathForRow.getNextNode();
 		}
+		if (pathForRow == null)
+			return null;
 		return new TreePath(pathForRow.getPath());
 	}
 
@@ -1700,11 +1697,12 @@
 		{
 			Point click = e.getPoint();
 			int row = ((int) click.getY() / getRowHeight()) - 1;
-		    
-			if (BasicTreeUI.this.tree.getSelectionModel().getSelectionMode() == treeSelectionModel.SINGLE_TREE_SELECTION)
-				BasicTreeUI.this.tree.addSelectionRow(row);
-			else if (BasicTreeUI.this.tree.isRowSelected(row))
+
+			if (BasicTreeUI.this.tree.isRowSelected(row))
 				BasicTreeUI.this.tree.removeSelectionRow(row);
+			else if (BasicTreeUI.this.tree.getSelectionModel()
+					.getSelectionMode() == treeSelectionModel.SINGLE_TREE_SELECTION)
+				BasicTreeUI.this.tree.addSelectionRow(row);
 			// FIXME: add in selection for more than 1 row, or an entire
 			// path
 		}
@@ -2232,20 +2230,35 @@
 
 	/* * HELPER METHODS FOR PAINTING * */
 
-	private void paintLeaf(Graphics g, int x, int y, JTree tree, Object leaf)
+	private void paintLeaf(Graphics g, int x, int y, JTree tree, Object leaf,
+			int childIndex)
 	{
+
+		DefaultMutableTreeNode root = (DefaultMutableTreeNode) (tree.getModel())
+				.getRoot();
+		TreePath tp = new TreePath(((DefaultMutableTreeNode) (root
+				.getChildAt(childIndex))).getPath());
+		boolean selected = tree.isPathSelected(tp);
+
 		Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree,
-				leaf, false, false, true, 0, false);
+				leaf, selected, false, true, 0, false);
 		g.translate(x, y);
 		c.paint(g);
 		g.translate(-x, -y);
 	}
 
 	private void paintNonLeaf(Graphics g, int x, int y, JTree tree,
-			Object nonLeaf)
+			Object nonLeaf, int childIndex)
 	{
+		DefaultMutableTreeNode root = (DefaultMutableTreeNode) (tree.getModel())
+				.getRoot();
+		TreePath tp = new TreePath(((DefaultMutableTreeNode) (root
+				.getChildAt(childIndex))).getPath());
+		boolean selected = tree.isPathSelected(tp);
+
 		Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree,
-				nonLeaf, false, false, false, 0, false);
+				nonLeaf, selected, false, false, 0, false);
+
 		g.translate(x, y);
 		c.paint(g);
 		g.translate(-x, -y);
@@ -2265,13 +2278,13 @@
 
 		if (mod.isLeaf(curr))
 		{
-			paintLeaf(g, indentation, descent, tree, curr);
+			paintLeaf(g, indentation, descent, tree, curr, childNumber);
 			descent += getRowHeight();
 		} else
 		{
 			if (depth > 0 || tree.isRootVisible())
 			{
-				paintNonLeaf(g, indentation, descent, tree, curr);
+				paintNonLeaf(g, indentation, descent, tree, curr, childNumber);
 				descent += getRowHeight();
 				y0 += halfHeight;
 			}
Index: javax/swing/tree/DefaultTreeSelectionModel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/tree/DefaultTreeSelectionModel.java,v
retrieving revision 1.13
diff -u -r1.13 DefaultTreeSelectionModel.java
--- javax/swing/tree/DefaultTreeSelectionModel.java	29 Jun 2005 13:20:01 -0000	1.13
+++ javax/swing/tree/DefaultTreeSelectionModel.java	29 Jun 2005 18:30:55 -0000
@@ -51,6 +51,7 @@
 import javax.swing.event.TreeSelectionEvent;
 import javax.swing.event.TreeSelectionListener;
 
+
 /**
  * DefaultTreeSelectionModel
  * 
@@ -271,10 +272,12 @@
 			{
 				TreePath[] temp = new TreePath[selection.length + 1];
 				System.arraycopy(selection, 0, temp, 0, selection.length);
-				temp[temp.length - 1] = value0;
+				temp[temp.length - 1] = value0;				
 				selection = new TreePath[temp.length];
 				System.arraycopy(temp, 0, selection, 0, temp.length);
 			}
+			
+			fireValueChanged(new TreeSelectionEvent(this, value0, true, leadPath, value0));
 		}
 	}
 
@@ -305,7 +308,9 @@
 					selection = new TreePath[temp.length];
 					System.arraycopy(temp, 0, selection, 0, temp.length);
 				}
-			}
+				
+				fireValueChanged(new TreeSelectionEvent(this, v0, true, leadPath, value0[0]));
+			}			
 		}
 	}
 
@@ -336,6 +341,8 @@
 					selection.length - index - 1);
 			selection = new TreePath[temp.length];
 			System.arraycopy(temp, 0, selection, 0, temp.length);
+			
+			fireValueChanged(new TreeSelectionEvent(this, value0, false, leadPath, value0));
 		}
 	}
 
@@ -370,6 +377,8 @@
 						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]));
 			}
 		}
 	}
@@ -421,6 +430,9 @@
 	 */
 	public boolean isPathSelected(TreePath value0)
 	{
+		if (selection == null)
+			return false;
+		
 		for (int i = 0; i < selection.length; i++)
 		{
 			if (selection[i].equals(value0))
@@ -477,8 +489,7 @@
 	 */
 	public TreeSelectionListener[] getTreeSelectionListeners()
 	{
-		return (TreeSelectionListener[]) listenerList
-				.getListeners(TreeSelectionListener.class);
+		return (TreeSelectionListener[]) getListeners(TreeSelectionListener.class);
 	}
 
 	/**
@@ -488,9 +499,9 @@
 	 */
 	protected void fireValueChanged(TreeSelectionEvent event)
 	{
-		TreeSelectionListener[] listeners = getTreeSelectionListeners();
+		TreeSelectionListener[] listeners = getTreeSelectionListeners();		
 
-		for (int i = listeners.length - 1; i >= 0; --i)
+		for (int i = 0; i < listeners.length; ++i)
 			listeners[i].valueChanged(event);
 	}
 
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to