View Javadoc

1   // %1126721046151:hoplugins.commons.ui.sorter%
2   /*
3    * Created on 7-apr-2005
4    *
5    * To change the template for this generated file go to
6    * Window>Preferences>Java>Code Generation>Code and Comments
7    */
8   package hoplugins.commons.ui.sorter;
9   
10  import javax.swing.event.TableModelEvent;
11  import javax.swing.event.TableModelListener;
12  
13  /***
14   * DOCUMENT ME!
15   *
16   * @author
17   */
18  class TableModelHandler implements TableModelListener {
19      /*** TODO Missing Parameter Documentation */
20      private final AbstractTableSorter sorter;
21  
22      /***
23       * Creates a new TableModelHandler object.
24       *
25       * @param sorter
26       */
27      TableModelHandler(AbstractTableSorter sorter) {
28          this.sorter = sorter;
29      }
30  
31      /***
32       * DOCUMENT ME!
33       *
34       * @param e
35       */
36      public void tableChanged(TableModelEvent e) {
37          // If we're not sorting by anything, just pass the event along.             
38          if (!this.sorter.isSorting()) {
39              this.sorter.clearSortingState();
40              this.sorter.fireTableChanged(e);
41  
42              return;
43          }
44  
45          // If the table structure has changed, cancel the sorting; the             
46          // sorting columns may have been either moved or deleted from             
47          // the model. 
48          if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
49              this.sorter.cancelSorting();
50              this.sorter.fireTableChanged(e);
51  
52              return;
53          }
54  
55          // We can map a cell event through to the view without widening             
56          // when the following conditions apply: 
57          // 
58          // a) all the changes are on one row (e.getFirstRow() == e.getLastRow()) and, 
59          // b) all the changes are in one column (column != TableModelEvent.ALL_COLUMNS) and,
60          // c) we are not sorting on that column (getSortingStatus(column) == NOT_SORTED) and, 
61          // d) a reverse lookup will not trigger a sort (modelToView != null)
62          //
63          // Note: INSERT and DELETE events fail this test as they have column == ALL_COLUMNS.
64          // 
65          // The last check, for (modelToView != null) is to see if modelToView 
66          // is already allocated. If we don't do this check; sorting can become 
67          // a performance bottleneck for applications where cells  
68          // change rapidly in different parts of the table. If cells 
69          // change alternately in the sorting column and then outside of             
70          // it this class can end up re-sorting on alternate cell updates - 
71          // which can be a performance problem for large tables. The last 
72          // clause avoids this problem. 
73          int column = e.getColumn();
74  
75          if ((e.getFirstRow() == e.getLastRow())
76              && (column != TableModelEvent.ALL_COLUMNS)
77              && (this.sorter.getSortingStatus(column) == AbstractTableSorter.NOT_SORTED)
78              && (this.sorter.getModelToView() != null)) {
79              int viewIndex = this.sorter.getModelToView()[e.getFirstRow()];
80  
81              this.sorter.fireTableChanged(new TableModelEvent(this.sorter,
82                      viewIndex, viewIndex, column, e.getType()));
83  
84              return;
85          }
86  
87          // Something has happened to the data that may have invalidated the row order. 
88          this.sorter.clearSortingState();
89          this.sorter.fireTableDataChanged();
90  
91          return;
92      }
93  }