[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] Problem inserting an element into a TableViewer with a filter

I am using version 0.120 [ne] 01 of org.eclipse.jface.viewers.TableViewer.

The TableViewer is configured with a filter to only allow certain elements
to be displayed.

When I tried to insert an element into the TableViewer using the
insert(Object element , int position) method, I found out that the
position argument is ignored, and the element is always inserted at the
end.

I dug in the code and found out that if the TableViewer has either a
Sorter or a Filter, the insert method calls add(Object) to service the
call (see code fragment below)

public void insert(Object element, int position) {
	tableViewerImpl.applyEditorValue();
	if (getSorter() != null || hasFilters()) {
		add(element);
		return;
	}
	if (position == -1)
		position = table.getItemCount();
	updateItem(new TableItem(table, SWT.NONE, position), element);
}

As you can see, if the TableViewer has any filters, it calls add(Object),
but does not pass the position argument along.  Internally, add(Object)
calls indexForElement(Object), but without a Sorter it always returns the
position at the end of the table.

Does it mean that if one uses a filter, one must supply a sorter at the
same time?

I thought about it a little bit and this makes sense to a certain degree. 
The semantics of the position is ambiguous.  It can mean the position of
the added element in either the filtered list or un-filtered list. 
However, simply ignoring the position argument and relegating the element
to the end of the list seems to be the worst of both worlds.

Thanks in advance.