As Roman noticed, in the previous patch the number of rows per whell click is multiplied to the magic number 3 in the JTree and JTable. This forces always to scroll by 3 lines, when clicking on the arrow button of the scroll bar should only scroll by one.

I move the magic 3 constant to the BasicScrollPaneUI. Probably we may later think about making it adjustable via system property or something like that.

2006-03-20  Audrius Meskauskas  <[EMAIL PROTECTED]>

   * javax/swing/JTable.java (ROWS_PER_WHEEL_CLICK): Removed.
   (getScrollableUnitIncrement): Rewritten.
   * javax/swing/JTree.java (ROWS_PER_WHEEL_CLICK): Removed.
   (getScrollableUnitIncrement): Rewritten.
   * javax/swing/plaf/basic/BasicScrollPaneUI.java
   (ROWS_PER_WHEEL_CLICK): New field.
   (MouseWheelHandler.mouseWheelMoved): Rewritten.

Index: JTable.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.88
diff -u -r1.88 JTable.java
--- JTable.java	20 Mar 2006 09:09:36 -0000	1.88
+++ JTable.java	20 Mar 2006 11:15:06 -0000
@@ -1325,12 +1325,6 @@
    * in the table) to provide or absorb excess space requirements.
    */
   public static final int AUTO_RESIZE_LAST_COLUMN = 3;
-  
-  /**
-   * The number of rows to scroll per mouse wheel click. From impression,
-   * Sun seems using the value 3.
-   */
-  static int ROWS_PER_WHEEL_CLICK = 3;  
 
   /**
    * A table mapping [EMAIL PROTECTED] java.lang.Class} objects to 
@@ -2105,7 +2099,9 @@
   
   /**
    * Return the preferred scrolling amount (in pixels) for the given scrolling
-   * direction and orientation.
+   * direction and orientation. This method handles a partially exposed row by
+   * returning the distance required to completely expose the item. When
+   * scrolling the top item is completely exposed.
    * 
    * @param visibleRect the currently visible part of the component.
    * @param orientation the scrolling orientation
@@ -2113,22 +2109,24 @@
    *          The values greater than one means that more mouse wheel or similar
    *          events were generated, and hence it is better to scroll the longer
    *          distance.
+   * @author Audrius Meskauskas ([EMAIL PROTECTED])
    */
   public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
                                         int direction)
   {
     int h = (rowHeight + rowMargin);
-    int delta = h * ROWS_PER_WHEEL_CLICK * direction;
-    
-    // Round so that the top would start from the row boundary 
+    int delta = h * direction;
+
+    // Round so that the top would start from the row boundary
     if (orientation == SwingConstants.VERTICAL)
       {
-        int near = ((visibleRect.y + delta +h/2) / h) * h;
+        // Completely expose the top row
+        int near = ((visibleRect.y + delta + h / 2) / h) * h;
         int diff = visibleRect.y + delta - near;
         delta -= diff;
       }
     return delta;
-    // TODO when scrollng horizontally, scroll into the column boundary.    
+    // TODO when scrollng horizontally, scroll into the column boundary.
   }
 
   /**
Index: JTree.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JTree.java,v
retrieving revision 1.58
diff -u -r1.58 JTree.java
--- JTree.java	20 Mar 2006 09:09:36 -0000	1.58
+++ JTree.java	20 Mar 2006 11:15:13 -0000
@@ -1347,12 +1347,6 @@
   }
 
   private static final long serialVersionUID = 7559816092864483649L;
-  
-  /**
-   * The number of rows to scroll per mouse wheel click. From impression,
-   * Sun seems using the value 3.
-   */
-  static int ROWS_PER_WHEEL_CLICK = 3;    
 
   public static final String CELL_EDITOR_PROPERTY = "cellEditor";
 
@@ -1630,7 +1624,8 @@
   
   /**
    * Return the preferred scrolling amount (in pixels) for the given scrolling
-   * direction and orientation.
+   * direction and orientation. This method handles a partially exposed row by
+   * returning the distance required to completely expose the item.
    * 
    * @param visibleRect the currently visible part of the component.
    * @param orientation the scrolling orientation
@@ -1638,8 +1633,7 @@
    *          The values greater than one means that more mouse wheel or similar
    *          events were generated, and hence it is better to scroll the longer
    *          distance.
-   *          
-   * @author Audrius Meskauskas ([EMAIL PROTECTED])          
+   * @author Audrius Meskauskas ([EMAIL PROTECTED])
    */
   public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
                                         int direction)
@@ -1649,19 +1643,17 @@
     // Round so that the top would start from the row boundary
     if (orientation == SwingConstants.VERTICAL)
       {
-        int row = getClosestRowForLocation(visibleRect.x, visibleRect.y);
+        // One pixel down, otherwise picks another row too high.
+        int row = getClosestRowForLocation(visibleRect.x, visibleRect.y + 1);
         row = row + direction;
         if (row < 0)
           row = 0;
-        if (row > getRowCount())
-          row = getRowCount();
-        
+
         Rectangle newTop = getRowBounds(row);
         delta = newTop.y - visibleRect.y;
       }
     else
-      delta = direction * ROWS_PER_WHEEL_CLICK * rowHeight == 0 ? 20
-                                                               : rowHeight;
+      delta = direction * rowHeight == 0 ? 20 : rowHeight;
     return delta;
   }
 
Index: plaf/basic/BasicScrollPaneUI.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/plaf/basic/BasicScrollPaneUI.java,v
retrieving revision 1.22
diff -u -r1.22 BasicScrollPaneUI.java
--- plaf/basic/BasicScrollPaneUI.java	20 Mar 2006 09:09:36 -0000	1.22
+++ plaf/basic/BasicScrollPaneUI.java	20 Mar 2006 11:15:14 -0000
@@ -43,7 +43,6 @@
 import java.awt.Graphics;
 import java.awt.Point;
 import java.awt.Rectangle;
-import java.awt.event.ComponentListener;
 import java.awt.event.ContainerEvent;
 import java.awt.event.ContainerListener;
 import java.awt.event.MouseWheelEvent;
@@ -251,7 +250,7 @@
 
       boolean tracksHeight = scrollable != null
                              && scrollable.getScrollableTracksViewportHeight();
-      int wheel = e.getWheelRotation();
+      int wheel = e.getWheelRotation() * ROWS_PER_WHEEL_CLICK;
       int delta;
 
       // If possible, scroll vertically.
@@ -366,6 +365,12 @@
    * not implement Scrollable.
    */
   static int SCROLL_NON_SCROLLABLES = 10;
+  
+  /**
+   * The number of rows to scroll per mouse wheel click. From impression,
+   * Sun seems using the value 3.
+   */
+  static int ROWS_PER_WHEEL_CLICK = 3;     
 
   /** The Scrollpane for which the UI is provided by this class. */
   protected JScrollPane scrollpane;

Reply via email to