classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Revised patch: JTable and DefaultTableColumnModel


From: David Gilbert
Subject: [cp-patches] Revised patch: JTable and DefaultTableColumnModel
Date: Tue, 05 Jul 2005 12:43:18 +0000
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050426)

Attached is a revised version of the patch I proposed late last week.
It adds implementations for the isCellEditable() and
createDefaultColumnsFromModel() methods in JTable, and fixes some bugs
uncovered while writing Mauve tests for the new methods.

2005-07-05  David Gilbert  <address@hidden>

        * javax/swing/JTable.java:
        (addColumn): retrieve correct column name,
        (convertColumnIndexToModel): remove check for > columnCount and let
        exception happen,
        (getColumnName): retrieve name from TableColumn,
        (isCellEditable): implemented,
        (createDefaultColumnsFromModel): implemented.
        * javax/swing/table/DefaultTableColumnModel.java:
        (addColumn): throw exception for null argument, set correct column
        index in TableColumnModelEvent,
        (removeColumn): use correct column index,
        (moveColumn): move the column, don't swap it. Also added argument
        checks,
        (getColumnIndex): reimplemented.

I've committed more Mauve tests to cover these changes.  OK to commit?

Regards,

Dave Gilbert
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.30
diff -u -r1.30 JTable.java
--- javax/swing/JTable.java     5 Jul 2005 09:03:43 -0000       1.30
+++ javax/swing/JTable.java     5 Jul 2005 11:11:06 -0000
@@ -672,8 +672,8 @@
   {
     if (column.getHeaderValue() == null)
       {
-       String name = getColumnName(column.getModelIndex());
-       column.setHeaderValue(name);
+        String name = dataModel.getColumnName(column.getModelIndex());
+        column.setHeaderValue(name);
       }
     
     columnModel.addColumn(column);
@@ -1025,8 +1025,6 @@
   {
     if (vc < 0)
       return vc;
-    else if (vc > getColumnCount())
-      return -1;
     else
       return columnModel.getColumn(vc).getModelIndex();
   }
@@ -1946,7 +1944,7 @@
   
   public String getColumnName(int column)
   {
-    return dataModel.getColumnName(column);
+    return this.columnModel.getColumn(column).getIdentifier().toString();
   }
 
   public int getEditingColumn()
@@ -2077,6 +2075,47 @@
     return columnModel.getColumn(columnModel.getColumnIndex(identifier));
   }
 
+  /**
+   * Returns <code>true</code> if the specified cell is editable, and
+   * <code>false</code> otherwise.
+   *
+   * @param row  the row index.
+   * @param column  the column index.
+   *
+   * @return A boolean.
+   */
+  public boolean isCellEditable(int row, int column)
+  {
+    return dataModel.isCellEditable(row, convertColumnIndexToModel(column));
+  }
+
+  /**
+   * Clears any existing columns from the <code>JTable</code>'s
+   * address@hidden TableColumnModel} and creates new columns to match the 
values in
+   * the data (address@hidden TableModel}) used by the table.
+   *
+   * @see #setAutoCreateColumnsFromModel(boolean)
+   */
+  public void createDefaultColumnsFromModel()
+  {
+    // remove existing columns
+    int columnIndex = columnModel.getColumnCount() - 1;
+    while (columnIndex >= 0)
+    {
+      columnModel.removeColumn(columnModel.getColumn(columnIndex));
+      columnIndex--;
+    }
+  
+    // add new columns to match the TableModel
+    int columnCount = dataModel.getColumnCount();
+    for (int c = 0; c < columnCount; c++)
+    {
+      TableColumn column = new TableColumn(c);
+      column.setIdentifier(dataModel.getColumnName(c));
+      columnModel.addColumn(column);
+    }
+  }
+
   public void changeSelection (int rowIndex, int columnIndex, boolean toggle, 
boolean extend)
   {
     if (toggle && extend)
Index: javax/swing/table/DefaultTableColumnModel.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/table/DefaultTableColumnModel.java,v
retrieving revision 1.13
diff -u -r1.13 DefaultTableColumnModel.java
--- javax/swing/table/DefaultTableColumnModel.java      2 Jul 2005 20:32:51 
-0000       1.13
+++ javax/swing/table/DefaultTableColumnModel.java      5 Jul 2005 11:11:07 
-0000
@@ -1,5 +1,5 @@
 /* DefaultTableColumnModel.java --
-   Copyright (C) 2002, 2004  Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -119,9 +119,11 @@
    */
   public void addColumn(TableColumn col)
   {
+    if (col == null)
+      throw new IllegalArgumentException("Null 'col' argument.");
     tableColumns.add(col);
     invalidateWidthCache();
-    fireColumnAdded(new TableColumnModelEvent(this,0,tableColumns.size()));
+    fireColumnAdded(new TableColumnModelEvent(this, 0, tableColumns.size() - 
1));
   }
 
   /**
@@ -132,8 +134,10 @@
    */
   public void removeColumn(TableColumn col)
   {
-    int index = getColumnIndex(col);
-    fireColumnRemoved(new TableColumnModelEvent(this,index,0));    
+    int index = this.tableColumns.indexOf(col);
+    if (index < 0)
+      return;
+    fireColumnRemoved(new TableColumnModelEvent(this, index, 0));    
     tableColumns.remove(col);
     invalidateWidthCache();
   }
@@ -147,10 +151,14 @@
    */
   public void moveColumn(int i, int j)
   {
-    Object tmp = tableColumns.get(i);
-    tableColumns.set(i, tableColumns.get(j));
-    tableColumns.set(j, tmp);
-    fireColumnAdded(new TableColumnModelEvent(this,i,j));
+    int columnCount = getColumnCount();
+    if (i < 0 || i >= columnCount)
+      throw new IllegalArgumentException("Index 'i' out of range.");
+    if (j < 0 || j >= columnCount)
+      throw new IllegalArgumentException("Index 'j' out of range.");
+    Object column = tableColumns.remove(i);
+    tableColumns.add(j, column);
+    fireColumnAdded(new TableColumnModelEvent(this, i, j));
   }
 
   /**
@@ -182,14 +190,27 @@
   }
 
   /**
-   * getColumnIndex returns index of the specified column
+   * Returns the index of the address@hidden TableColumn} with the given 
identifier.
    *
-   * @param identifier identifier of the column
-   * @return int index of the given column
+   * @param identifier  the identifier (<code>null</code> not permitted).
+   * 
+   * @return The index of the address@hidden TableColumn} with the given 
identifier.
+   * 
+   * @throws IllegalArgumentException if <code>identifier</code> is 
+   *         <code>null</code> or there is no column with that identifier.
    */
   public int getColumnIndex(Object identifier)
   {
-    return tableColumns.indexOf(identifier, 0);
+    if (identifier == null)
+      throw new IllegalArgumentException("Null identifier.");
+    int columnCount = tableColumns.size();
+    for (int i = 0; i < columnCount; i++) 
+    {
+      TableColumn tc = (TableColumn) tableColumns.get(i);
+      if (identifier.equals(tc.getIdentifier()))
+        return i;
+    }
+    throw new IllegalArgumentException("No TableColumn with that identifier.");
   }
 
   /**

reply via email to

[Prev in Thread] Current Thread [Next in Thread]