[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Deleting table items in a network event monitoring application

Thanks to Tom Schindl for pointing out that this technique can't work when the Table is virtual (created with SWT.VIRTUAL). Eliminating that flag at Table creation time solved the problem.

Joseph Schwarz wrote:
Tom Schindl wrote:

The TableViewer internally restores the relation between an row and the
Control using TableItem#setData(Object) and makes this accessible
through TableViewer#getElementAt(int)

So you can retrieve the Array of elements you want to drop using:

Object[] els = new Object[10];

for( int i = 0; i < 10; i++ ) {
   els[i] = v.getElementAt(i);
}

v.remove(els);

Or did I get your question completely wrong.

Thanks for your very speedy answer, and you certainly got the question right, but I haven't been able to get my implementation to work:


private void checkFreeMemory() {
long ultimateFreeMemory = max_memory - (runtime.totalMemory()-runtime.freeMemory());
if (ultimateFreeMemory < MEMORY_MARGIN_IN_BYTES) {
int itemCount = viewer.getTable().getItemCount();
if (itemCount < NUMBER_TO_DELETE) { // This is 1000 in my code
logger.fine("We're almost out of memory, but there are only: "+itemCount+" rows left!");
return; // It's hopeless!
}
logger.fine("Now have "+itemCount+" rows in event table.");
Object[] els = new Object[NUMBER_TO_DELETE];


boolean hasNull = false;
for( int i = 0; i < NUMBER_TO_DELETE; i++ ) {
els[i] = viewer.getElementAt(i);
if (els[i] == null && hasNull == false) {
logger.fine("Element # "+i+" is null."); // Log first null element found
hasNull = true;
}
}
if (hasNull) { // THIS IS ALWAYS (SAD BUT) TRUE!
return; // bailing out...
}


logger.fine("...removing "+NUMBER_TO_DELETE+" rows to avoid running out of memory, thread=" + Thread.currentThread().getName());
viewer.remove(els);
logger.fine("Remove done!");
logger.fine("...item count reduced by: "+(itemCount-viewer.getTable().getItemCount()));
}
}


Even though there are more than 1000 elements in the table, according to itemCount, the 13th and following elements are all null (I had to invoke this via syncExec in order to see the exception in the first place). The code never gets beyond the check for null. Am I misunderstanding the meaning of what viewer.getTable().getItemCount() returns?

Joe