Avoid race conditions when deleting, inserting and updating items and saving/restoring the selection.
| 988 | 1020 | |
|---|---|---|
| 123 | 123 | public int getColumnCount() { return m_tm.getColumnCount(); } |
| 124 | 124 | public synchronized Object getValueAt(int row, int col) { return m_tm.getValueAt(row, col); } |
| 125 | 125 | |
| 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 | ||
| 129 | 132 | final TableSorter sorter = this; |
| 130 | 133 | if(myRow == -1) return false; |
| 131 | 134 | final Selection save = new Selection(_table, _sorted); |
| 132 | 135 | 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 | } | |
| 141 | 149 | return true; |
| 142 | 150 | } |
| 143 | 151 | |
| --- | --- | |
| 198 | 206 | } |
| 199 | 207 | } |
| 200 | 208 | |
| 201 | public synchronized int insert(Object o) { | |
| 209 | public int insert(Object o) { | |
| 202 | 210 | 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 | ||
| 205 | 218 | final TableSorter sorter = this; |
| 206 | if(myRow == -1) return -1; | |
| 219 | final int count = m_tm.getRowCount(); | |
| 220 | ||
| 207 | 221 | SwingUtilities.invokeLater(new Runnable() { |
| 208 | 222 | 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)); | |
| 211 | 224 | if(save != null) restoreSelection(save); |
| 212 | 225 | } |
| 213 | 226 | }); |
| --- | --- | |
| 225 | 238 | } |
| 226 | 239 | |
| 227 | 240 | 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 | } | |
| 229 | 245 | final TableSorter sorter = this; |
| 230 | 246 | if (myRow != -1) { |
| 231 | 247 | SwingUtilities.invokeLater(new Runnable() { |
| 232 | 248 | public void run() { |
| 233 | 249 | 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)); | |
| 236 | 251 | restoreSelection(save); |
| 237 | 252 | } |
| 238 | 253 | }); |
