Implemented PAGE UP and PAGE DOWN (including SHIFT and CTRL modifiers)
for BasicTableUI.java.  This involved fixing a bug in JTable's
getCellRect method (it was returning the height of the entire table
instead of just the particular cell).

Also fixed a bug in JViewport scrollRectToVisible.

Patch is attached.

2005-08-02  Anthony Balkissoon  <[EMAIL PROTECTED]>

* javax/swing/JTable.java:
(getCellRect): Height should be the row height, not the entire table
height.
(getRowHeight): New public method, part of API.
* javax/swing/JViewport.java:
(scrollRectToVisible): Fixed buggy scrolling conditions.
* javax/swing/plaf/basic/BasicTableUI.java:
(KeyHandler.keyPressed): Implemented PAGE-UP, PAGE-DOWN, CTRL-PAGE-UP,
and CTRL-PAGE-DOWN key actions.  Also added line to scroll the table
appropriately after changing the selection.
(KeyHandler.getFirstVisibleColumnIndex): New implementation method.
(KeyHandler.getLastVisibleColumnIndex): Likewise.
(KeyHandler.getFirstVisibleRowIndex): Likewise.
(KeyHandler.getLastVisibleRowIndex): Likewise.

-Tony 
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.37
diff -u -r1.37 JTable.java
--- javax/swing/JTable.java	28 Jul 2005 19:37:53 -0000	1.37
+++ javax/swing/JTable.java	2 Aug 2005 19:11:39 -0000
@@ -872,7 +872,7 @@
                                int column,
                                boolean includeSpacing)
   {
-    int height = getHeight();
+    int height = getRowHeight(row);
     int width = columnModel.getColumn(column).getWidth();
     int x_gap = columnModel.getColumnMargin();
     int y_gap = rowMargin;
@@ -1098,6 +1098,19 @@
   {
     return rowHeight;
   }
+
+  /**
+   * Get the height of the specified row.
+   *
+   * @param row the row whose height to return
+   */
+  public int getRowHeight(int row)
+  {
+    // FIXME: return the height of the specified row
+    // which may be different from the general rowHeight
+    return rowHeight;
+  }
+
 
   /**
    * Get the value of the [EMAIL PROTECTED] #rowMargin} property.
Index: javax/swing/JViewport.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JViewport.java,v
retrieving revision 1.24
diff -u -r1.24 JViewport.java
--- javax/swing/JViewport.java	28 Jul 2005 19:42:54 -0000	1.24
+++ javax/swing/JViewport.java	2 Aug 2005 19:11:39 -0000
@@ -498,11 +498,11 @@
     Point pos = getViewPosition();
     Rectangle viewBounds = getView().getBounds();
     Rectangle portBounds = getBounds();
-
+    
     // FIXME: should validate the view if it is not valid, however
     // this may cause excessive validation when the containment
     // hierarchy is being created.
-
+    
     // if contentRect is larger than the portBounds, center the view
     if (contentRect.height > portBounds.height || 
         contentRect.width > portBounds.width)
@@ -510,24 +510,22 @@
         setViewPosition(new Point(contentRect.x, contentRect.y));
         return;
       }
-
+    
     // Y-DIRECTION
-    if (contentRect.y + viewBounds.y < portBounds.y)
+    if (contentRect.y < -viewBounds.y)
       setViewPosition(new Point(pos.x, contentRect.y));
-    else if (contentRect.y + viewBounds.y + contentRect.height > 
-             (portBounds.y+portBounds.height))
+    else if (contentRect.y + contentRect.height > 
+             -viewBounds.y + portBounds.height)
       setViewPosition (new Point(pos.x, contentRect.y - 
-                                 (portBounds.height - contentRect.height) -
-                                 portBounds.y));
-
+                                 (portBounds.height - contentRect.height)));
+    
     // X-DIRECTION
     pos = getViewPosition();
-    if (contentRect.x + viewBounds.x < portBounds.x)
+    if (contentRect.x < -viewBounds.x)
       setViewPosition(new Point(contentRect.x, pos.y));
-    else if (contentRect.x + viewBounds.x + contentRect.width > 
-             (portBounds.x + portBounds.height))
+    else if (contentRect.x + contentRect.width > 
+             -viewBounds.x + portBounds.width)
       setViewPosition (new Point(contentRect.x - 
-                                 (portBounds.width - contentRect.width)
-                                 - portBounds.x, pos.y));
+                                 (portBounds.width - contentRect.width), pos.y));
   }
 }
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.18
diff -u -r1.18 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java	26 Jul 2005 20:19:46 -0000	1.18
+++ javax/swing/plaf/basic/BasicTableUI.java	2 Aug 2005 19:11:40 -0000
@@ -40,6 +40,7 @@
 
 import java.awt.Color;
 import java.awt.Component;
+import java.awt.ComponentOrientation;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Point;
@@ -366,7 +367,8 @@
         }
       else if (evt.getKeyCode() == KeyEvent.VK_HOME)
         {
-          if (evt.getModifiers() == (InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK))
+          if (evt.getModifiers() == 
+              (InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK))
             {
               rowModel.setLeadSelectionIndex(0);
               colModel.setLeadSelectionIndex(colLead);
@@ -395,11 +397,93 @@
         }
       else if (evt.getKeyCode() == KeyEvent.VK_PAGE_UP)
         {
-          // FIXME: implement, need JList.ensureIndexIsVisible to work
+          int target;
+          if (!evt.isControlDown())
+            {
+              if (rowLead == getFirstVisibleRowIndex())
+                target = Math.max
+                  (0, rowLead - (getLastVisibleRowIndex() - 
+                                      getFirstVisibleRowIndex() + 1));
+              else
+                target = getFirstVisibleRowIndex();
+              
+              if (evt.getModifiers() == 0)
+                {
+                  rowModel.setSelectionInterval(target, target);
+                  colModel.setSelectionInterval(colLead, colLead);
+                }
+              else if (evt.getModifiers() == InputEvent.SHIFT_MASK)
+                {
+                  rowModel.setLeadSelectionIndex(target);
+                  colModel.setLeadSelectionIndex(colLead);
+                }
+            }
+          else
+            {
+              if (colLead == getFirstVisibleColumnIndex())
+                target = Math.max
+                  (0, colLead - (getLastVisibleColumnIndex() -
+                                      getFirstVisibleColumnIndex() + 1));
+              else
+                target = getFirstVisibleColumnIndex();
+              
+              if (evt.getModifiers() == InputEvent.CTRL_MASK)
+                {
+                  colModel.setSelectionInterval(target, target);
+                  rowModel.setSelectionInterval(rowLead, rowLead);
+                }
+              else if (evt.getModifiers() == 
+                       (InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK))
+                {
+                  colModel.setLeadSelectionIndex(target);
+                  rowModel.setLeadSelectionIndex(rowLead);
+                }
+            }
         }
       else if (evt.getKeyCode() == KeyEvent.VK_PAGE_DOWN)
         {
-          // FIXME: implement, need JList.ensureIndexIsVisible to work
+          int target;
+          if (!evt.isControlDown())
+            {
+              if (rowLead == getLastVisibleRowIndex())
+                target = Math.min
+                  (rowMax, rowLead + (getLastVisibleRowIndex() - 
+                                      getFirstVisibleRowIndex() + 1));
+              else
+                target = getLastVisibleRowIndex();
+              
+              if (evt.getModifiers() == 0)
+                {
+                  rowModel.setSelectionInterval(target, target);
+                  colModel.setSelectionInterval(colLead, colLead);
+                }
+              else if (evt.getModifiers() == InputEvent.SHIFT_MASK)
+                {
+                  rowModel.setLeadSelectionIndex(target);
+                  colModel.setLeadSelectionIndex(colLead);
+                }
+            }
+          else
+            {
+              if (colLead == getLastVisibleColumnIndex())
+                target = Math.min
+                  (colMax, colLead + (getLastVisibleColumnIndex() -
+                                      getFirstVisibleColumnIndex() + 1));
+              else
+                target = getLastVisibleColumnIndex();
+              
+              if (evt.getModifiers() == InputEvent.CTRL_MASK)
+                {
+                  colModel.setSelectionInterval(target, target);
+                  rowModel.setSelectionInterval(rowLead, rowLead);
+                }
+              else if (evt.getModifiers() == 
+                       (InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK))
+                {
+                  colModel.setLeadSelectionIndex(target);
+                  rowModel.setLeadSelectionIndex(rowLead);
+                }
+            }
         }
       else if (evt.getKeyCode() == KeyEvent.VK_TAB
                || evt.getKeyCode() == KeyEvent.VK_ENTER)
@@ -504,6 +588,9 @@
         {
           table.changeSelection(rowLead, colLead, true, false);
         }
+      table.scrollRectToVisible
+        (table.getCellRect(rowModel.getLeadSelectionIndex(), 
+                           colModel.getLeadSelectionIndex(), false));
     }
 
     public void keyReleased(KeyEvent e) 
@@ -512,6 +599,69 @@
 
     public void keyTyped(KeyEvent e) 
     {
+    }
+
+    /**
+     * Returns the column index of the first visible column.
+     *
+     */
+    int getFirstVisibleColumnIndex()
+    {
+      ComponentOrientation or = table.getComponentOrientation();
+      Rectangle r = table.getVisibleRect();
+      if (!or.isLeftToRight())
+        r.translate((int) r.getWidth() - 1, 0);
+      return table.columnAtPoint(r.getLocation());
+    }
+    
+    /**
+     * Returns the column index of the last visible column.
+     *
+     */
+    int getLastVisibleColumnIndex()
+    {
+      ComponentOrientation or = table.getComponentOrientation();
+      Rectangle r = table.getVisibleRect();
+      if (or.isLeftToRight())
+        r.translate((int) r.getWidth() - 1, 0);
+      return table.columnAtPoint(r.getLocation());      
+    }
+    
+    /**
+     * Returns the row index of the first visible row.
+     *
+     */
+    int getFirstVisibleRowIndex()
+    {
+      ComponentOrientation or = table.getComponentOrientation();
+      Rectangle r = table.getVisibleRect();
+      if (!or.isLeftToRight())
+        r.translate((int) r.getWidth() - 1, 0);
+      return table.rowAtPoint(r.getLocation());
+    }
+    
+    /**
+     * Returns the row index of the last visible row.
+     *
+     */
+    int getLastVisibleRowIndex()
+    {
+      ComponentOrientation or = table.getComponentOrientation();
+      Rectangle r = table.getVisibleRect();
+      r.translate(0, (int) r.getHeight() - 1);
+      if (or.isLeftToRight())
+        r.translate((int) r.getWidth() - 1, 0);
+      // The next if makes sure that we don't return -1 simply because
+      // there is white space at the bottom of the table (ie, the display
+      // area is larger than the table)
+      if (table.rowAtPoint(r.getLocation()) == -1)
+        {
+          if (getFirstVisibleRowIndex() == -1)
+            return -1;
+          else
+            return table.getModel().getRowCount() - 1;
+        }
+      return table.rowAtPoint(r.getLocation());
     }
   }
 
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to