Avoid race conditions when deleting, inserting and updating items and saving/restoring the selection.

by mrs, 10 Oct, 2009 11:21 PM
988 1020  
123123   public int getColumnCount() { return m_tm.getColumnCount(); }
124124   public synchronized Object getValueAt(int row, int col) { return m_tm.getValueAt(row, col); }
125125 
126   public synchronized boolean delete(Object o) {
127     final int myRow = m_tm.findRow(o);
128 //    final Object deleted = o;
126   public boolean delete(Object o) {
127     final int myRow;
128     synchronized(this) {
129       myRow = m_tm.findRow(o);
130     }
131 
129132     final TableSorter sorter = this;
130133     if(myRow == -1) return false;
131134     final Selection save = new Selection(_table, _sorted);
132135     save.delete(myRow);
133     m_tm.delete(myRow);
134     SwingUtilities.invokeLater(new Runnable() {
135       public void run() {
136 //        int row = m_tm.findRow(deleted);
137         _table.tableChanged(new TableModelEvent(sorter, myRow, myRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
138         restoreSelection(save);
139       }
140     });
136     synchronized (this) {
137       m_tm.delete(myRow);
138     }
139     try {
140       SwingUtilities.invokeAndWait(new Runnable() {
141         public void run() {
142           _table.tableChanged(new TableModelEvent(sorter, myRow, myRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
143           restoreSelection(save);
144         }
145       });
146     } catch (Exception e) {
147       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
148     }
141149     return true;
142150   }
143151 
------
198206     }
199207   }
200208 
201   public synchronized int insert(Object o) {
209   public int insert(Object o) {
202210     final Selection save = getSelectionSafely();
203     int myRow = m_tm.insert(o);
204     //final Object inserted = o;
211 
212     int myRow;
213     synchronized(this) {
214       myRow = m_tm.insert(o);
215     }
216     if(myRow == -1) return -1;
217 
205218     final TableSorter sorter = this;
206     if(myRow == -1) return -1;
219     final int count = m_tm.getRowCount();
220 
207221     SwingUtilities.invokeLater(new Runnable() {
208222       public void run() {
209         //int row = m_tm.findRow(inserted);
210         _table.tableChanged(new TableModelEvent(sorter, 0, m_tm.getRowCount(), TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
223         _table.tableChanged(new TableModelEvent(sorter, 0, count, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
211224         if(save != null) restoreSelection(save);
212225       }
213226     });
------
225238   }
226239 
227240   public boolean update(final Object updated) {
228     int myRow = m_tm.findRow(updated);
241     final int myRow;
242     synchronized(this) {
243       myRow = m_tm.findRow(updated);
244     }
229245     final TableSorter sorter = this;
230246     if (myRow != -1) {
231247       SwingUtilities.invokeLater(new Runnable() {
232248         public void run() {
233249           Selection save = new Selection(_table, _sorted);
234           int row = m_tm.findRow(updated);
235           _table.tableChanged(new TableModelEvent(sorter, row));
250           _table.tableChanged(new TableModelEvent(sorter, myRow));
236251           restoreSelection(save);
237252         }
238253       });