Sort Table on ValueChangeEvent

I’m pretty much about to give up on this. What I want to do seems simple enough: I have an editable table backed by a BeanItemContainer that I want to remain sorted by date at all times. So I made a TableFieldFactory which returns, for that property, a DateField which has a ValueChangeListener that, on value change, sorts the table.

It seems all right until it explodes in my face with an out of memory error. The problem, I realized, is that value change events happen all the time for the strangest reasons; sorting a table makes at some point a call to getVisibleCellsNoCache, which at some point makes a call to getPropertyValue, which of all things calls setPropertyDataSource, firing a valueChangeEvent! Which in turn will fire my listener, calling sort() again…

So apparently I cannot trust valueChangeEvents to be fired only when values are actually changed. I tried putting setEditable as false before sorting, but that also fires a valueChangeEvent. I tried having the ValueChangeListener keep track with a boolean of whether that is the first call, but it doesn’t work because for some reason a new DateField is created for that field when I sort. I tried making a ValueChangeListener subclass which guaranteedly returns the same instance for the same bean item id, but that doesn’t work either for reasons I can’t fathom - value change events keep on being fired god knows where. Is there any way around this, or should I just give up on automatic sorting/editable tables altogether?

Well, I found the solution to that problem, but got stumped in another that is almost as bad. I backed my field factory with a hash based table which will always return the same field for the same pair (itemId, propertyId). Now either the boolean trick, or removing the listener and adding it again after sorting, work. However, for some reason items are rendered correctly and then disappear client side two seconds after sorting/refreshRowCache; the items in the generated column I have do not appear, and rows reachable only with scrolling are rendered inconsistently or not at all. This problem disappears completely when I make some request client side that forces a repaint, like refreshing the web page, but calling refreshRowCache immediately after sorting won’t solve it, for things seem fine client side before magically vanishing after a second or two. This problem does not happen at all when I set editable to false, but setting it to false and then true after sorting won’t solve it either. This did not happen before I implemented persistence in my field factory; afterwards, only persistent fields did not disappear, and even those were not rendered in the rows reachable only with scrolling. This happens in both firefox and chrome.

I’ll make a simple example which reproduces this behaviour when I have the spare time.

Meanwhile, dear people from the future: learn from my mistake and STAY AWAY FROM EDITABLE TABLES. I’m one week behind in my work thanks to all the issues they’ve been causing. They’re definitely not worth the risk, or the trouble.