Hi,

please review the attached patch that improves the behaviour of JList
within a scrolling container, especially with layoutOrientation set to
JList.HORIZONTAL_WRAP or VERTICAL_WRAP. Is the ChangeLog entry ok?

2005-02-27  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/JList.java
        (getPreferredScrollableViewportSize):
        previous implementation was merely guessing the size,
        now it respects layoutOrientation, visibleRowCount
        and preferredSize
        (getScrollableTracksViewportHeight):
        reimplemented to respect layoutOrientation, visibleRowCount
        and preferred size
        (getScrollableTracksViewportWidth):
        reimplemented to respect layoutOrientation, visibleRowCount
        and preferred size
        * javax/swing/plaf/basic/BasicListUI.java
        (getPreferredSize):
        improved calculation of preferredSize when JList is
        set to HORIZONTAL_WRAP or VERTICAL_WRAP.
        (getCellBounds):
        previous implementation assumed a layoutOrientation of
        JList.VERTICAL, now also ok with JList.HORIZONTAL_WRAP and
        JList.VERTICAL_WRAP

/Roman

Index: javax/swing/JList.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JList.java,v
retrieving revision 1.24
diff -u -r1.24 JList.java
--- javax/swing/JList.java	16 Feb 2005 12:36:22 -0000	1.24
+++ javax/swing/JList.java	27 Feb 2005 22:12:27 -0000
@@ -1029,32 +1029,20 @@
    */
   public Dimension getPreferredScrollableViewportSize()
   {
-    int vis = getVisibleRowCount();
-    int nrows = getModel() == null ? 0 : getModel().getSize();
-    // FIXME: this is a somewhat arbitrary default, but.. ?
-    Dimension single = new Dimension(10, 10);;
-    Rectangle bounds = null;
 
-    if (vis > nrows)
+    Dimension retVal = getPreferredSize();
+    if (getLayoutOrientation() == VERTICAL)
       {
-        if (fixedCellWidth != -1 && 
-            fixedCellHeight != -1)
+        if (fixedCellHeight != -1)
           {
-            single = new Dimension(fixedCellWidth, fixedCellHeight);
-          }
-        else if (nrows != 0 && getUI() != null)
-          {
-            Rectangle tmp = getUI().getCellBounds(this, 0, 0);
-            if (tmp != null)
-              single = tmp.getSize();
-          }
-      }
-    else if (getUI() != null)
-      {
-        return getUI().getCellBounds(this, 0, vis - 1).getSize();
+            if (fixedCellWidth != -1)
+              {
+                int size = getModel().getSize();
+                retVal = new Dimension(fixedCellWidth, size * fixedCellHeight);
+              } // TODO: add else clause (preferredSize is ok for now)
+          } // TODO: add else clause (preferredSize is ok for now)
       }
-
-    return new Dimension(single.width, single.height * vis);
+    return retVal;
   }
 
   /**
@@ -1193,7 +1181,19 @@
    */
   public boolean getScrollableTracksViewportWidth()
   {
-    return false;
+    Component parent = getParent();
+    boolean retVal = false;
+    if (parent instanceof JViewport)
+      {
+        JViewport viewport = (JViewport) parent;
+        Dimension pref = getPreferredSize();
+        if (viewport.getSize().width > pref.width)
+          retVal = true;
+        if ((getLayoutOrientation() == HORIZONTAL_WRAP)
+            && (getVisibleRowCount() <= 0))
+          retVal = true;
+      }
+    return retVal;
   }
 
   /**
@@ -1206,7 +1206,19 @@
    */
   public boolean getScrollableTracksViewportHeight()
   {
-    return false;
+    Component parent = getParent();
+    boolean retVal = false;
+    if (parent instanceof JViewport)
+      {
+        JViewport viewport = (JViewport) parent;
+        Dimension pref = getPreferredSize();
+        if (viewport.getSize().height > pref.height)
+          retVal = true;
+        if ((getLayoutOrientation() == VERTICAL_WRAP)
+            && (getVisibleRowCount() <= 0))
+          retVal = true;
+      }
+    return retVal;
   }
 
   public int getAnchorSelectionIndex()
Index: javax/swing/plaf/basic/BasicListUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicListUI.java,v
retrieving revision 1.14
diff -u -r1.14 BasicListUI.java
--- javax/swing/plaf/basic/BasicListUI.java	27 Feb 2005 20:35:55 -0000	1.14
+++ javax/swing/plaf/basic/BasicListUI.java	27 Feb 2005 22:12:30 -0000
@@ -56,6 +56,7 @@
 import javax.swing.CellRendererPane;
 import javax.swing.JComponent;
 import javax.swing.JList;
+import javax.swing.JViewport;
 import javax.swing.ListCellRenderer;
 import javax.swing.ListModel;
 import javax.swing.ListSelectionModel;
@@ -386,16 +387,21 @@
     if (l != list || cellWidth == -1)
       return null;
 
-    int lo = Math.min(index1, index2);
-    int hi = Math.max(index1, index2);
-    Point loLoc = indexToLocation(list, lo);
-    Point hiLoc = indexToLocation(list, hi);
-    Rectangle lobounds = new Rectangle(loLoc.x, loLoc.y, cellWidth,
-                                       getRowHeight(lo));
-    Rectangle hibounds = new Rectangle(hiLoc.x, hiLoc.y, cellWidth,
-                                       getRowHeight(hi));
+    int minIndex = Math.min(index1, index2);
+    int maxIndex = Math.max(index1, index2);
+    Point loc = indexToLocation(list, minIndex);
+    Rectangle bounds = new Rectangle(loc.x, loc.y, cellWidth,
+                                     getRowHeight(minIndex));
 
-    return lobounds.union(hibounds);
+    for (int i = minIndex + 1; i <= maxIndex; i++)
+      {
+        Point hiLoc = indexToLocation(list, i);
+        Rectangle hibounds = new Rectangle(hiLoc.x, hiLoc.y, cellWidth,
+                                       getRowHeight(i));
+        bounds = bounds.union(hibounds);
+      }
+
+    return bounds;
   }
 
   /**
@@ -639,10 +645,34 @@
    */
   public Dimension getPreferredSize(JComponent c)
   {
-    if (list.getModel().getSize() == 0)
+    int size = list.getModel().getSize();
+    if (size == 0)
       return new Dimension(0, 0);
+    int visibleRows = list.getVisibleRowCount();
+    int layoutOrientation = list.getLayoutOrientation();
     Rectangle bounds = getCellBounds(list, 0, list.getModel().getSize() - 1);
-    return bounds.getSize();
+    Dimension retVal = bounds.getSize();
+    Component parent = list.getParent();
+    if ((visibleRows == -1) && (parent instanceof JViewport))
+      {
+        JViewport viewport = (JViewport) parent;
+
+        if (layoutOrientation == JList.HORIZONTAL_WRAP)
+          {
+            int h = viewport.getSize().height;
+            int cellsPerCol = h / cellHeight;
+            int w = size / cellsPerCol * cellWidth;
+            retVal = new Dimension(w, h);
+          }
+        else if (layoutOrientation == JList.VERTICAL_WRAP)
+          {
+            int w = viewport.getSize().width;
+            int cellsPerRow = Math.max(w / cellWidth, 1);
+            int h = size / cellsPerRow * cellHeight;
+            retVal = new Dimension(w, h);
+          }
+      }
+    return retVal;
   }
 
   /**

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to