This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch][gui] More fixes to JTable


Hi,

Here is the patch that makes more improvements to JTable and DefaultTableColumnModel. I'll be committing this patch to java-gui-branch.

       * javax/swing/JTable.java
       (columnAtPoint): New Method. Implemented.
       (rowAtPoint): Likewise.
       (countSelections): Fixed few small count errors.
       (getSelections): Likewise.
       (setSelectionMode): Set selection mode for column
       selection model in addition to row selection model.
       * javax/swing/plaf/basic/BasicTableUI.java:
       (getRowForPoint): Removed. Replaced by
       JTable.rowAtPoint().
       (getColForPoint): Removed. Replaced by
       JTable.columnAtPoint().
       (updateSelection): Updated to call JTable.columnAtPoint
       and JTable.rowAtPoint.
       * javax/swing/table/DefaultTableColumnModel.java:
       (getSelectedColumns): Implemented.
       (getSelectedColumnCount): Implemented.

Olga.
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JTable.java,v
retrieving revision 1.4.18.16
diff -u -r1.4.18.16 JTable.java
--- javax/swing/JTable.java	31 Dec 2004 10:19:45 -0000	1.4.18.16
+++ javax/swing/JTable.java	4 Jan 2005 19:40:11 -0000
@@ -42,6 +42,7 @@
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.Rectangle;
+import java.awt.Point;
 import java.util.Hashtable;
 import java.util.Vector;
 
@@ -571,6 +572,58 @@
     repaint();
   }
 
+ /**
+   * Returns index of the column that contains specified point 
+   * or -1 if this table doesn't contain this point.
+   *
+   * @param point point to identify the column
+   * @return index of the column that contains specified point or 
+   * -1 if this table doesn't contain this point.
+   */
+  public int columnAtPoint(Point point)
+  {
+    int x0 = getLocation().x;
+    int ncols = getColumnCount();
+    Dimension gap = getIntercellSpacing();
+    TableColumnModel cols = getColumnModel();
+    int x = point.x;
+    
+    for (int i = 0; i < ncols; ++i)
+      {
+        int width = cols.getColumn(i).getWidth() + (gap == null ? 0 : gap.width);
+        if (0 <= x && x < width)
+          return i;
+        x -= width;  
+      }
+    
+    return -1;
+  }
+
+  /**
+   * Returns index of the row that contains specified point or 
+   * -1 if this table doesn't contain this point.
+   *
+   * @param point point to identify the row
+   * @return index of the row that contains specified point or 
+   * -1 if this table doesn't contain this point.
+   */
+  public int rowAtPoint(Point point)
+  {
+    int y0 = getLocation().y;
+    int nrows = getRowCount();
+    Dimension gap = getIntercellSpacing();
+    int height = getRowHeight() + (gap == null ? 0 : gap.height);
+    int y = point.y;
+    
+    for (int i = 0; i < nrows; ++i)
+      {
+        if (0 <= y && y < height)
+          return i;
+        y -= height;
+      }
+      
+    return -1;
+  }
 
   /** 
    * Calculate the visible rectangle for a particular row and column. The
@@ -921,11 +974,11 @@
             break;
             
           case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
-            sum = hi - lo;
+            sum = hi - lo + 1;
             break;
             
           case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:        
-            for (int i = lo; i < hi; ++i)
+            for (int i = lo; i <= hi; ++i)
               if (lsm.isSelectedIndex(i))        
                 ++sum;
             break;
@@ -952,12 +1005,12 @@
             break;      
       
           case ListSelectionModel.SINGLE_INTERVAL_SELECTION:            
-            for (int i = lo; i < hi; ++i)
+            for (int i = lo; i <= hi; ++i)
               ret[j++] = i;
             break;
             
           case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:        
-            for (int i = lo; i < hi; ++i)
+            for (int i = lo; i <= hi; ++i)
               if (lsm.isSelectedIndex(i))        
                 ret[j++] = i;
             break;
@@ -1328,13 +1381,16 @@
 
   /**
    * Set the value of the {@link #selectionMode} property by
-   * delegation to the {@link #selectionModel} field.
+   * delegation to the {@link #selectionModel} field. The same selection
+   * mode is set for row and column selection models.
    *
    * @param s The new value of the property
    */ 
   public void setSelectionMode(int s)
-  {
-    selectionModel.setSelectionMode(s);
+  { 
+    selectionModel.setSelectionMode(s);    
+    columnModel.getSelectionModel().setSelectionMode(s);
+    
     repaint();
   }
 
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.1.2.4
diff -u -r1.1.2.4 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java	16 Dec 2004 19:11:39 -0000	1.1.2.4
+++ javax/swing/plaf/basic/BasicTableUI.java	4 Jan 2005 19:40:11 -0000
@@ -104,45 +104,12 @@
   {
     Point begin, curr;
 
-    private int getRowForPoint(Point p)
-    {      
-      int y0 = table.getLocation().y;
-      int nrows = table.getRowCount();
-      Dimension gap = table.getIntercellSpacing();
-      int height = table.getRowHeight() + (gap == null ? 0 : gap.height);
-      int y = p.y;
-      for (int i = 0; i < nrows; ++i)
-        {
-          if (0 <= y && y < height)
-            return i;
-          y -= height;
-        }
-      return -1;
-    }
-
-    private int getColForPoint(Point p)
-    {
-      int x0 = table.getLocation().x;
-      int ncols = table.getColumnCount();
-      Dimension gap = table.getIntercellSpacing();
-      TableColumnModel cols = table.getColumnModel();      
-      int x = p.x;
-      for (int i = 0; i < ncols; ++i)
-        {
-          int width = cols.getColumn(i).getWidth() + (gap == null ? 0 : gap.width);
-          if (0 <= x && x < width)
-            return i;
-          x -= width;
-        }
-      return -1;
-    }
-
     private void updateSelection()
     {
       if (table.getRowSelectionAllowed())
         {
-          int lo_row = getRowForPoint(begin);
-          int hi_row  = getRowForPoint(curr);
+          int lo_row = table.rowAtPoint(begin);
+          int hi_row  = table.rowAtPoint(curr);
           ListSelectionModel rowModel = table.getSelectionModel();
           if (lo_row != -1 && hi_row != -1)
             rowModel.setSelectionInterval(lo_row, hi_row);
@@ -150,8 +117,8 @@
 
       if (table.getColumnSelectionAllowed())
         {
-          int lo_col = getColForPoint(begin);
-          int hi_col = getColForPoint(curr);
+          int lo_col = table.columnAtPoint(begin);
+          int hi_col = table.columnAtPoint(curr);
           ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
           if (lo_col != -1 && hi_col != -1)
             colModel.setSelectionInterval(lo_col, hi_col);
Index: javax/swing/table/DefaultTableColumnModel.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/table/DefaultTableColumnModel.java,v
retrieving revision 1.3.18.6
diff -u -r1.3.18.6 DefaultTableColumnModel.java
--- javax/swing/table/DefaultTableColumnModel.java	30 Dec 2004 17:10:07 -0000	1.3.18.6
+++ javax/swing/table/DefaultTableColumnModel.java	4 Jan 2005 19:40:11 -0000
@@ -298,7 +298,40 @@
    */
   public int[] getSelectedColumns()
   {
-    return null; // TODO
+    // FIXME: Implementation of this method was taken from private method 
+    // JTable.getSelections(), which is used in various places in JTable
+    // including selected row calculations and cannot be simply removed.
+    // This design should be improved to illuminate duplication of code.
+    
+    ListSelectionModel lsm = this.selectionModel;    
+    int sz = getSelectedColumnCount();
+    int [] ret = new int[sz];
+
+    int lo = lsm.getMinSelectionIndex();
+    int hi = lsm.getMaxSelectionIndex();
+    int j = 0;
+    java.util.ArrayList ls = new java.util.ArrayList();
+    if (lo != -1 && hi != -1)
+      {
+        switch (lsm.getSelectionMode())
+          {
+          case ListSelectionModel.SINGLE_SELECTION:
+            ret[0] = lo;
+            break;      
+      
+          case ListSelectionModel.SINGLE_INTERVAL_SELECTION:            
+            for (int i = lo; i <= hi; ++i)
+              ret[j++] = i;
+            break;
+            
+          case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:        
+            for (int i = lo; i <= hi; ++i)
+              if (lsm.isSelectedIndex(i))        
+                ret[j++] = i;
+            break;
+          }
+      }
+    return ret;
   }
 
   /**
@@ -307,7 +340,37 @@
    */
   public int getSelectedColumnCount()
   {
-    return 0; // TODO
+    // FIXME: Implementation of this method was taken from private method 
+    // JTable.countSelections(), which is used in various places in JTable
+    // including selected row calculations and cannot be simply removed.
+    // This design should be improved to illuminate duplication of code.
+   
+    ListSelectionModel lsm = this.selectionModel;
+    int lo = lsm.getMinSelectionIndex();
+    int hi = lsm.getMaxSelectionIndex();
+    int sum = 0;
+    
+    if (lo != -1 && hi != -1)
+      {
+        switch (lsm.getSelectionMode())
+          {
+          case ListSelectionModel.SINGLE_SELECTION:
+            sum = 1;
+            break;
+            
+          case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
+            sum = hi - lo + 1;
+            break;
+            
+          case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:        
+            for (int i = lo; i <= hi; ++i)
+              if (lsm.isSelectedIndex(i))        
+                ++sum;
+            break;
+          }
+      }
+     
+     return sum;
   }
 
   /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]