Riccardo pointed out some problems with JTree when the icons are larger
than the row height. I fixed it so the tree uses the max row height of
all the nodes.

2006-01-09  Lillian Angel  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicTreeUI.java:
        Added new field.
        (setRowHeight): Row height is set to the max height of
        all the nodes, or 20 as a default value.
        (getPathBounds): Cleaned up code.
        (getMaxHeight): New helper function that gets the max
        height of all the rows.
        (getClosestPathForLocation): Fixed to use getMaxHeight.
        (updateCachedPreferredSize): Likewise.
        (installUI): Shouldn't expand tree on startup.
        (getNodeDimensions): Fixed to use getMaxHeight.

Index: javax/swing/plaf/basic/BasicTreeUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTreeUI.java,v
retrieving revision 1.116
diff -u -r1.116 BasicTreeUI.java
--- javax/swing/plaf/basic/BasicTreeUI.java	20 Dec 2005 21:34:32 -0000	1.116
+++ javax/swing/plaf/basic/BasicTreeUI.java	9 Jan 2006 18:34:28 -0000
@@ -234,29 +233,38 @@
-  /** Default row height, if none was set. */
-  int rowHeight = 20;
+
+  /** The max height of the nodes in the tree. */
+  int maxHeight = 0;
@@ -437,7 +445,7 @@
   protected void setRowHeight(int rowHeight)
   {
     if (rowHeight == 0)
-      rowHeight = this.rowHeight;
+      rowHeight = Math.max(getMaxHeight(tree), 20);
     treeState.setRowHeight(rowHeight);
   }
 
@@ -625,19 +633,49 @@
    */
   public Rectangle getPathBounds(JTree tree, TreePath path)
   {
-    Rectangle bounds = null;
     int row = -1;
     Object cell = null;
     if (path != null)
       {
         row = getRowForPath(tree, path);
         cell = path.getLastPathComponent();
-        bounds = new Rectangle(0, row * getRowHeight(), 0, 0);
       }
-    return nodeDimensions.getNodeDimensions(cell, row,
-                                            getLevel(cell),
+    return nodeDimensions.getNodeDimensions(cell, row, getLevel(cell),
                                             tree.isExpanded(path),
-                                            bounds);
+                                            new Rectangle());
+  }
+
+  /**
+   * Returns the max height of all the nodes in the tree.
+   * 
+   * @param tree -
+   *          the current tree
+   * @return the max height.
+   */
+  private int getMaxHeight(JTree tree)
+  {
+    if (maxHeight != 0)
+      return maxHeight;
+
+    Icon e = UIManager.getIcon("Tree.openIcon");
+    Icon c = UIManager.getIcon("Tree.closedIcon");
+    Icon l = UIManager.getIcon("Tree.leafIcon");
+    int rc = getRowCount(tree);
+    int iconHeight = 0;
+
+    for (int row = 0; row < rc; row++)
+      {
+        if (isLeaf(row))
+          iconHeight = l.getIconHeight();
+        else if (tree.isExpanded(row))
+          iconHeight = e.getIconHeight();
+        else
+          iconHeight = c.getIconHeight();
+
+        maxHeight = Math.max(maxHeight, iconHeight + gap);
+      }
+
+    return maxHeight;
   }
 
   /**
@@ -720,7 +758,7 @@
    */
   public TreePath getClosestPathForLocation(JTree tree, int x, int y)
   {
-    int row = Math.round(y / getRowHeight());
+    int row = Math.round(y / getMaxHeight(tree));
     TreePath path = getPathForRow(tree, row);
 
     // no row is visible at this node
@@ -1186,9 +1227,13 @@
               bounds.width += getCurrentControlIcon(curr).getIconWidth();
             maxWidth = Math.max(maxWidth, bounds.x + bounds.width);
           }
-        preferredSize = new Dimension(maxWidth, (getRowHeight() * path.length));
+
+        maxHeight = 0;
+        maxHeight = getMaxHeight(tree);
+        preferredSize = new Dimension(maxWidth, (maxHeight * path.length));
       }
-    else preferredSize = new Dimension(0, 0);
+    else
+      preferredSize = new Dimension(0, 0);
     validCachedPreferredSize = true;
   }
 
@@ -1343,23 +1388,12 @@
     installComponents();
     installKeyboardActions();
     installListeners();
-    
+
     setCellEditor(createDefaultCellEditor());
     createdCellEditor = true;
     isEditing = false;
 
-    TreeModel mod = tree.getModel();
-    setModel(mod);
-    if (mod != null)
-      {
-        Object root = mod.getRoot();
-        if (root != null)
-          {
-            TreePath path = new TreePath(root);
-            if (!tree.isExpanded(path))
-              toggleExpandState(path);
-          }
-      }
+    setModel(tree.getModel());
     treeSelectionModel = tree.getSelectionModel();
 
     completeUIInstall();

@@ -2496,8 +2522,10 @@
         {
           size.x = getRowX(row, depth);
           size.width = SwingUtilities.computeStringWidth(fm, s);
-          size.height = fm.getHeight();
+          size.height = getMaxHeight(tree);
+          size.y = size.height * row;
         }
+
       return size;
     }
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to