posted 13 years ago
Wendy, thanks for responding. I think I need a little more explanation to understand your suggestion.
Here is my situation. I need to run calculations on specific rows in my table. Each calculation has a list of the rows it should use. When I add a row, I need to to figure out which calculation to do and then add the row to the appropriate list. I have a listener that responds when a row is added (or deleted).
Now, let's say the first list contains rows 1,3,4 and the second list rows 2,5,6. The user deletes row 2. My goal is to remove row 2 from the list and rerun the calculation on what were rows 5 and 6. Except they aren't anymore. They are now rows 4 and 5. Likewise the first list should now read 1,2,3. I have this problem whenever I delete a row or add a row anywhere except the end of the table.
Solutions-
brute-force #1: typically there will be 100 or so lists, loop over them all and update the row ids. This can be done by my listeners for add and remove rows.
brute force #2: when a row is added or removed, redo all the calculations that assign the rows to a specific list. This means looping over all the rows in the table. Because calculations are involved, this might be slower that #1.
invariant ids: here I would store a,c,d and b,e,f. Then if row 2 was deleted, I would call a function that would say 2 maps to b and remove b. The inverse would be to call a function that said what e, for example, was currently mapped to. Before deletion, it would map to 5, after deletion it would map to 4. So I would not need to modify my lists, my methods would be passed the invariant list and do the conversion using the appropriate function.
If I want invariant ids, I could create an object which is my mapping and call methods on it when I add a row, delete a row or need to convert from invariant to table row id or vice versa. I would call the method when I add and remove rows.
Or (in theory, as I am a bit sketchy on extending a table model for anything other than setting specific columns to be editable and things like that) I could use a table model that inherently supports the concept of invariant ids. One way I have seen this done is by adding an additional column to the underlying structure. That column contains the displayed row id or -1 if the row was deleted. The invariant id is the row number of this structure (new rows are always added at the end, rows are never deleted). That would seem to require that I override the constructor so that I can set up this array, and since it needs to return or set values based on the first column and will never actually remove a row, override all the methods that do anything with rows and columns.
I also know that if rows are sorted, there are functions to convert back and forth between the original row order and the sorted order (convertRowIndexToView and convertRowIndexToModel) and I was hoping the solution to my problem was as simple as getting equivalent methods for invariant ids.