Commit in lcsim/src/org/lcsim/plugin/browser/sort on MAIN
DefaultSortableTableModel.java+78-331.3 -> 1.4
Fix  LCSIM-172

lcsim/src/org/lcsim/plugin/browser/sort
DefaultSortableTableModel.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- DefaultSortableTableModel.java	10 Jun 2007 04:46:51 -0000	1.3
+++ DefaultSortableTableModel.java	25 Jun 2007 17:47:27 -0000	1.4
@@ -10,7 +10,7 @@
 /**
  * Converts any TableModel to a SortableTableModel.
  * @author Tony Johnson
- * @version $Id: DefaultSortableTableModel.java,v 1.3 2007/06/10 04:46:51 tonyj Exp $
+ * @version $Id: DefaultSortableTableModel.java,v 1.4 2007/06/25 17:47:27 tonyj Exp $
  */
 public class DefaultSortableTableModel implements SortableTableModel
 {
@@ -19,6 +19,7 @@
    private EventListenerList listeners = new EventListenerList();
    private TableModelListener internalListener = new InternalTableModelListener();
    private int sortColumn = UNSORTED;
+   private String sortColumnName;
    private boolean ascending = true;
    private int[] rowMap;
    private int[] reverseMap;
@@ -98,12 +99,7 @@
       listeners.remove(TableModelListener.class, l);
       if (listeners.getListenerCount() == 0) source.removeTableModelListener(internalListener);
    }
-   
-   public void setValueAt(Object aValue, int rowIndex, int columnIndex)
-   {
-      source.setValueAt(aValue, mapFromSorted(rowIndex), columnIndex);
-   }
-   
+      
    public void sort(int column, boolean ascending)
    {
       if (column == UNSORTED)
@@ -114,6 +110,7 @@
             reverseMap = null;
             reverseMapValid = false;
             this.sortColumn = column;
+            this.sortColumnName = null;
             TableModelEvent ee = new TableModelEvent(this,0,source.getRowCount()-1,TableModelEvent.ALL_COLUMNS);
             fireTableChanged(ee);
          }
@@ -138,6 +135,7 @@
             for (int i=0; i<nRows; i++) rowMap[i] = i;
          }
          this.sortColumn = column;
+         this.sortColumnName = getColumnName(column);
          this.ascending = ascending;
          reverseMapValid = false;
          sort1(0,nRows);
@@ -156,34 +154,45 @@
    {
       // Entire data was changed, including (perhaps) number of rows
       nRows = source.getRowCount();
-      rowMap = new int[nRows+10]; // Leave a little room for expansion
-      for (int i=0; i<nRows; i++) rowMap[i] = i;
-      reverseMapValid = false;
-      if (sortColumn != UNSORTED) sort1(0,nRows);
+      if (sortColumn != UNSORTED)
+      {
+         rowMap = new int[nRows+10]; // Leave a little room for expansion
+         for (int i=0; i<nRows; i++) rowMap[i] = i;
+         reverseMapValid = false;
+         sort1(0,nRows);
+      }
       TableModelEvent ee = new TableModelEvent(this,0,Integer.MAX_VALUE,TableModelEvent.ALL_COLUMNS);
       fireTableChanged(ee);
    }
    // Insert a newly added source row in a sorted list
    private int rowWasInserted(int row)
    {
-      if (nRows == rowMap.length)
+      int insertPoint;
+      if (sortColumn != UNSORTED)
       {
-         int[] newMap = new int[rowMap.length+10];
-         System.arraycopy(rowMap,0,newMap,0,rowMap.length);
-         rowMap = newMap;
-      }
-      // Add new row to the end to begin with
-      for (int i=0; i<nRows; i++)
-      {
-         if (rowMap[i] >= row) rowMap[i]++;
+         if (nRows >= rowMap.length)
+         {
+            int[] newMap = new int[nRows+10];
+            System.arraycopy(rowMap,0,newMap,0,rowMap.length);
+            rowMap = newMap;
+         }
+         // Add new row to the end to begin with
+         for (int i=0; i<nRows; i++)
+         {
+            if (rowMap[i] >= row) rowMap[i]++;
+         }
+         rowMap[nRows] = row;
+         // Do a binary search to find out where the new row should go
+         insertPoint = binarySearch(nRows,0,nRows);
+         if (insertPoint != nRows)
+         {
+            System.arraycopy(rowMap,insertPoint, rowMap, insertPoint+1,nRows-insertPoint);
+            rowMap[insertPoint] = row;
+         }
       }
-      rowMap[nRows] = row;
-      // Do a binary search to find out where the new row should go
-      int insertPoint = binarySearch(nRows,0,nRows);
-      if (insertPoint != nRows)
+      else
       {
-         System.arraycopy(rowMap,insertPoint, rowMap, insertPoint+1,nRows-insertPoint);
-         rowMap[insertPoint] = row;
+         insertPoint = row;
       }
       nRows++;
       reverseMapValid = false;
@@ -207,20 +216,33 @@
    }
    private int rowWasDeleted(int row)
    {
-      int sortedRow = mapToSorted(row);
-      System.arraycopy(rowMap,sortedRow+1,rowMap,sortedRow,nRows-sortedRow-1);
-      nRows--;
-      for (int i=0; i<nRows; i++)
+      int sortedRow;
+      if (sortColumn != UNSORTED)
+      {
+         sortedRow = mapToSorted(row);
+         System.arraycopy(rowMap,sortedRow+1,rowMap,sortedRow,nRows-sortedRow-1);
+         nRows--;
+         for (int i=0; i<nRows; i++)
+         {
+            if (rowMap[i] > row) rowMap[i]--;
+         }
+      }
+      else 
       {
-         if (rowMap[i] > row) rowMap[i]--;
+         sortedRow = row;
       }
       reverseMapValid = false;
       return sortedRow;
    }
    
+   public void setValueAt(Object aValue, int rowIndex, int columnIndex)
+   {
+      source.setValueAt(aValue, mapFromSorted(rowIndex), columnIndex);
+   }
+   
    private Object get(int index)
    {
-      return source.getValueAt(rowMap[index], sortColumn);
+      return source.getValueAt(mapFromSorted(index), sortColumn);
    }
    private int compare(int i, int j)
    {
@@ -356,7 +378,30 @@
             }
             else if (type == TableModelEvent.UPDATE)
             {
-               if (column == sortColumn || column == TableModelEvent.ALL_COLUMNS) reSort();
+               if (column == sortColumn) 
+               {
+                  reSort();
+               }
+               else if (column == TableModelEvent.ALL_COLUMNS)
+               {
+                  // Assume worst case, columns and data and nRows changed
+                  sortColumn = UNSORTED;
+                  reverseMapValid = false;
+                  rowMap = null;
+                  nRows = source.getRowCount();
+                  // See if a column with the same name exists
+                  if (sortColumnName != null)
+                  {
+                     int n = getColumnCount();
+                     for (int i=0; i<n; i++)
+                     {
+                        if (sortColumnName.equals(getColumnName(i)))
+                        {
+                           sort(i,ascending);
+                        }
+                     }
+                  }
+               }
             }
             TableModelEvent ee = new TableModelEvent(DefaultSortableTableModel.this,first,last,column,type);
             fireTableChanged(ee);
CVSspam 0.2.8