Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Row numbering in TableViewer
Row numbering in TableViewer [message #463079] Tue, 06 February 2007 07:30 Go to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
Hello,

I noticed that the controller ("content provider") of a TableViewer
does not allow access to the current row, only access to the column
is provided. This approach has the disadvantage, that its really hard
to realize an often occuring user request to provide a static
line numbering for the table (typically the first column).

Besides the fact, that such a numbering is generic and I would hate to
add an artifical row number to my model data (such that I would use the
current element data available from the ITableLabelProvider interface),
this ansatz would also lead to the problem, that any sorting by the
table columns would also sort the row numbers, which it shouldn't.

Any ideas how to solve this issue with TableViewer?

Thanks and Greetings from Bremen,

Daniel Krügler
Re: Row numbering in TableViewer [message #463080 is a reply to message #463079] Tue, 06 February 2007 08:23 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
In 3.2 that's not really easy. But in 3.3 it's very simple:

TableViewer viewer = new TableViewer(parent);
TableViewerColumn column = new TableViewerColumn(viewer,SWT.NONE);
column.getColumn().setText("Row");

column.setLabelProvider(new ColumnLabelProvider() {
public void update(ViewerCell cell) {
cell.setText(table.indexOf((TableItem)cell.getItem())+"")
}
});

TableViewerColumn column = new TableViewerColumn(viewer,SWT.NONE);
column.getColumn().setText("Column 1");

....

If you want to know more about the new API you can read my blog at
http://tom-eclipse-dev.blogspot.com/

Tom

Daniel Krügler schrieb:
> Hello,
>
> I noticed that the controller ("content provider") of a TableViewer
> does not allow access to the current row, only access to the column
> is provided. This approach has the disadvantage, that its really hard
> to realize an often occuring user request to provide a static
> line numbering for the table (typically the first column).
>
> Besides the fact, that such a numbering is generic and I would hate to
> add an artifical row number to my model data (such that I would use the
> current element data available from the ITableLabelProvider interface),
> this ansatz would also lead to the problem, that any sorting by the
> table columns would also sort the row numbers, which it shouldn't.
>
> Any ideas how to solve this issue with TableViewer?
>
> Thanks and Greetings from Bremen,
>
> Daniel Krügler
Re: Row numbering in TableViewer [message #463087 is a reply to message #463080] Tue, 06 February 2007 12:23 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
One more note for 3.2 you simply need to traverse the table your own

something like:
------------------8<------------------
public void getText(Object element)

// Maybe cache the last index and start searching from there!
for( int i = 0; i < table.getItems(); i++ ) {
if( element.equals(item.getData()) ) {
return table.indexOf(item)+"";
}
}
------------------8<------------------

The problem this might cause is that you'll loose a lot of performance
because you always have to traverse the whole table so you need to think
about other possibilities the best one might be to use a counter and
always reset this counter before refresh(*) is called which you can
easily achieve by subclassing TableViewer and overloading
refresh-methods (those are called internally even when you set a sorted
but this is an implementation detail you can not rely on 100%).

Tom

Tom Schindl schrieb:
> In 3.2 that's not really easy. But in 3.3 it's very simple:
>
> TableViewer viewer = new TableViewer(parent);
> TableViewerColumn column = new TableViewerColumn(viewer,SWT.NONE);
> column.getColumn().setText("Row");
>
> column.setLabelProvider(new ColumnLabelProvider() {
> public void update(ViewerCell cell) {
> cell.setText(table.indexOf((TableItem)cell.getItem())+"")
> }
> });
>
> TableViewerColumn column = new TableViewerColumn(viewer,SWT.NONE);
> column.getColumn().setText("Column 1");
>
> ....
>
> If you want to know more about the new API you can read my blog at
> http://tom-eclipse-dev.blogspot.com/
>
> Tom
>
> Daniel Krügler schrieb:
>> Hello,
>>
>> I noticed that the controller ("content provider") of a TableViewer
>> does not allow access to the current row, only access to the column
>> is provided. This approach has the disadvantage, that its really hard
>> to realize an often occuring user request to provide a static
>> line numbering for the table (typically the first column).
>>
>> Besides the fact, that such a numbering is generic and I would hate to
>> add an artifical row number to my model data (such that I would use the
>> current element data available from the ITableLabelProvider interface),
>> this ansatz would also lead to the problem, that any sorting by the
>> table columns would also sort the row numbers, which it shouldn't.
>>
>> Any ideas how to solve this issue with TableViewer?
>>
>> Thanks and Greetings from Bremen,
>>
>> Daniel Krügler
>
Re: Row numbering in TableViewer [message #463089 is a reply to message #463087] Tue, 06 February 2007 14:03 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
Tom Schindl wrote:
> One more note for 3.2 you simply need to traverse the table your own
>
> something like:
> ------------------8<------------------
> public void getText(Object element)
>
> // Maybe cache the last index and start searching from there!
> for( int i = 0; i < table.getItems(); i++ ) {
> if( element.equals(item.getData()) ) {
> return table.indexOf(item)+"";
> }
> }

Thank you very much Tom! In our use case performance is of high
relevance, so we decided to wait on 3.3 for the feature you mentioned.

Greetings from Bremen,

Daniel
Re: Row numbering in TableViewer [message #463090 is a reply to message #463089] Tue, 06 February 2007 14:52 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

Well even in 3.3 you need to traverse the table when calling
table#indexOf()! At least on Win32 this is plain Java traversing the
whole table searching for the requested item.

So you have 2 possibilities:

1. Use the work around I described by using a counter this has the great
advantage that no extra calculation needs to take place => is really
fast but you need to track structural changes yourself. What I could
envision post 3.3 is a Listener-API for viewers which informs
components about structural changes. Please file an enhancement
request using a listener approach your task can be solved without
subclassing!

I think of something like:

public abstract class AbstractViewerListener {
public void beforeRefresh(RefreshEvent);
public void afterRefresh(RefreshEvent);
public void beforeInsert(InsertEvent);
public void afterInsert(InsertEvent);
public void beforeRemove(RemoveEvent);
public void afterRemove(RemoveEvent);
public void beforeFilterChanged(FilterEvent);
public void afterFilterChanged(FilterEvent);
public void beforeSorterChanged(SorterEvent);
public void afterSorterChanged(SorterEvent);
// ...
}

I think this would provide you all the power you'll need to fullfill
your task without any problems. But that's nothing currently planned and
M5 (=API-freeze) coding has ended last week.

2. Request an enhancement for JFace ViewerRow to provide API something
like TableViewerRow#getRowNumber(). The problem I see with this that
this will slow down inserts/removes because the row number of all
subsequental rows would need to be recalculated! Nevertheless you
could give it a try but for 3.3 I don't see a chance this will get
in! But maybe we could make this optional to turn on/off.

And because I'm at it, the solution I presented to you doesn't take into
account that you need to recalculate all row indices if a user modifies
a table using insert/remove!

Tom


Daniel Krügler schrieb:
> Tom Schindl wrote:
>> One more note for 3.2 you simply need to traverse the table your own
>>
>> something like:
>> ------------------8<------------------
>> public void getText(Object element)
>>
>> // Maybe cache the last index and start searching from there!
>> for( int i = 0; i < table.getItems(); i++ ) {
>> if( element.equals(item.getData()) ) {
>> return table.indexOf(item)+"";
>> }
>> }
>
> Thank you very much Tom! In our use case performance is of high
> relevance, so we decided to wait on 3.3 for the feature you mentioned.
>
> Greetings from Bremen,
>
> Daniel
Previous Topic:Non-resizable view
Next Topic:Listen to data changes with CommonNavigator
Goto Forum:
  


Current Time: Fri Sep 13 02:57:55 GMT 2024

Powered by FUDForum. Page generated in 0.03857 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top