This implements some of the key actions for a JTable.

2005-07-20  Anthony Balkissoon  <[EMAIL PROTECTED]>

* javax/swing/JTable:
(JTable): Set the lead selection index for each of the two lists
associated with this table to 0 (instead of -1).  This complies with
JDK.
* javax/swing/plaf/basic/BasicTableUI.java:
(KeyHandler.keyPressed):  Implemented the following key actions (with
CTRL/SHIFT handling when appropriate): UP/DOWN/LEFT/RIGHT, HOME/END,
CTRL-A, CTRL-\, CTRL-/.


-Tony
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.34
diff -u -r1.34 JTable.java
--- javax/swing/JTable.java	7 Jul 2005 21:15:54 -0000	1.34
+++ javax/swing/JTable.java	20 Jul 2005 20:16:52 -0000
@@ -618,9 +618,14 @@
   {
     setModel(dm == null ? createDefaultDataModel() : dm);
     setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);
-
+    
     this.columnModel = cm;
     initializeLocalVars();
+    // The next two lines are for compliance with the JDK which starts
+    // the JLists associated with a JTable  with both lead selection 
+    // indices at 0, rather than -1 as in regular JLists
+    selectionModel.setLeadSelectionIndex(0);
+    columnModel.getSelectionModel().setLeadSelectionIndex(0);
     updateUI();
   }    
 
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.10
diff -u -r1.10 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java	8 Jul 2005 10:35:35 -0000	1.10
+++ javax/swing/plaf/basic/BasicTableUI.java	20 Jul 2005 20:16:54 -0000
@@ -97,12 +97,178 @@
 
   class KeyHandler implements KeyListener
   {
-    public void keyPressed(KeyEvent e) 
+    public void keyPressed(KeyEvent evt) 
     {
+      ListSelectionModel rowModel = table.getSelectionModel();
+      ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
+
+      int rowLead = rowModel.getLeadSelectionIndex();
+      int rowMax = table.getModel().getRowCount() - 1;
+
+      int colLead = colModel.getLeadSelectionIndex();
+      int colMax = table.getModel().getColumnCount() - 1;
+
+      if ((evt.getKeyCode() == KeyEvent.VK_DOWN)
+          || (evt.getKeyCode() == KeyEvent.VK_KP_DOWN))
+        {
+          if (!evt.isShiftDown())
+            {
+              
+              table.clearSelection();
+              rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
+                                            Math.min(rowLead + 1, rowMax));
+              colModel.setSelectionInterval(colLead,colLead);
+            }
+          else 
+            {
+              rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
+              colModel.setLeadSelectionIndex(colLead);
+            }
+        }
+      else if ((evt.getKeyCode() == KeyEvent.VK_UP)
+               || (evt.getKeyCode() == KeyEvent.VK_KP_UP))
+        {
+          if (!evt.isShiftDown())
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
+                                            Math.max(rowLead - 1, 0));
+              colModel.setSelectionInterval(colLead,colLead);
+            }
+          else
+            {
+              rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));
+              colModel.setLeadSelectionIndex(colLead);
+            }
+        }
+      else if ((evt.getKeyCode() == KeyEvent.VK_LEFT)
+               || (evt.getKeyCode() == KeyEvent.VK_KP_LEFT))
+        {
+          if (evt.isShiftDown())
+            {
+              colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));
+              rowModel.setLeadSelectionIndex(rowLead);
+            }
+          else
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(rowLead,rowLead);
+              colModel.setSelectionInterval(Math.max(colLead - 1, 0),
+                                            Math.max(colLead - 1, 0));
+            }
+        }
+      else if ((evt.getKeyCode() == KeyEvent.VK_RIGHT)
+               || (evt.getKeyCode() == KeyEvent.VK_KP_RIGHT))
+        {
+          if (evt.isShiftDown())
+            {
+              colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));
+              rowModel.setLeadSelectionIndex(rowLead);
+            }
+          else
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(rowLead,rowLead);
+              colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
+                                            Math.min(colLead + 1, colMax));
+            }
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_END)
+        {
+          if (evt.isControlDown() && evt.isShiftDown())
+            {
+              rowModel.setLeadSelectionIndex(rowMax);
+              colModel.setLeadSelectionIndex(colLead);
+            }
+          else if (evt.isControlDown())
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(rowMax,rowMax);
+              colModel.setSelectionInterval(colLead, colLead);
+            }
+          else if (evt.isShiftDown())
+            {
+              colModel.setLeadSelectionIndex(colMax);
+              rowModel.setLeadSelectionIndex(rowLead);
+            }
+          else
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(rowLead, rowLead);
+              colModel.setSelectionInterval(colMax, colMax);
+            }
+         }
+      else if (evt.getKeyCode() == KeyEvent.VK_HOME)
+        {
+          if (evt.isControlDown() && evt.isShiftDown())
+            {
+              rowModel.setLeadSelectionIndex(0);
+              colModel.setLeadSelectionIndex(colLead);
+            }
+          else if (evt.isControlDown())
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(0,0);
+              colModel.setSelectionInterval(colLead, colLead);
+            }
+          else if (evt.isShiftDown())
+            {
+              colModel.setLeadSelectionIndex(0);
+              rowModel.setLeadSelectionIndex(rowLead);
+            }
+          else
+            {
+              table.clearSelection();
+              rowModel.setSelectionInterval(rowLead, rowLead);
+              colModel.setSelectionInterval(0, 0);
+            }
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_F2)
+        {
+          // FIXME: Implement "start editing"
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_PAGE_UP)
+        {
+          // FIXME: implement, need JList.ensureIndexIsVisible to work
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_PAGE_DOWN)
+        {
+          // FIXME: implement, need JList.ensureIndexIsVisible to work
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_TAB)
+        {
+          // FIXME: Implement
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_ENTER)
+        {
+          // FIXME: Implement
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
+        {
+          // FIXME: implement "cancel"
+        }
+      else if ((evt.getKeyCode() == KeyEvent.VK_A || evt.getKeyCode()
+                == KeyEvent.VK_SLASH) && evt.isControlDown())
+        {
+          rowModel.setSelectionInterval(0, rowMax);
+          colModel.setSelectionInterval(0, colMax);
+          // the next two lines are to restore the lead selection indices to 
+          // their previous values, because select-all operations shouldn't 
+          // change them
+          rowModel.addSelectionInterval(rowLead, rowLead);
+          colModel.addSelectionInterval(colLead, colLead);
+        }
+      else if (evt.getKeyCode() == KeyEvent.VK_BACK_SLASH
+               && evt.isControlDown())
+        {
+          table.clearSelection();
+        }
     }
+
     public void keyReleased(KeyEvent e) 
     {
     }
+
     public void keyTyped(KeyEvent e) 
     {
     }
_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to