2

I have an action which scrolls the NatTable to the selected Row. I tried to use the below snippet which works partially

natTable.doCommand(new ShowRowInViewportCommand(
natTable.getBodyLayer().getViewportLayer(), rowPosition+delta));

It works if I set the delta value to rowPosition+10. However, if the current scroll is at the bottom of the table, it doesnot scroll upto the row, it needs an extra scroll to see the selection. Logically I need to do rowPosition-10 to get the scroll up to the right location.

So, we need to find the scroll position is at end of table or beginning of table and then set the rowPosition+10 or rowPosition-10 accordingly. Is there a way to find the current scroll position??

This is similar to the question Scroll NatTable programmatically here. Unfortunately dont have reputation to comment and hence creating a separate thread for this question.

Appreciate if anyone has solved this issue could post the answer.

Thank you.

SDS
  • 457
  • 5
  • 17

2 Answers2

2

I've just run into the same situation as the original poster. What I came to realize after playing around with this for a while is that the ShowRowInViewportCommand is going to "Scroll" the viewport as minimally as possible to make the row visible. One of three pre-conditions will be true:

  • (A) The target row is already visible (in which case nothing will happen and the visible target row remains where it is);
  • (B) The target row is below the viewport (in which case the viewport is scrolled down so that the target row is at the bottom of the viewport)
  • (C) The target row is above the viewport (in which case the viewport is scrolled up so that the target row is at the top or the viewport).

In my use case I wanted the target row to be positioned in the center of the viewport if it was not currently visible (or left where it is if it is visible).

To do this I was calculating the index of the object that should be in the top row of the viewport and issuing the doCommand(). This worked fine for case (C), but failed for (B). The fix was simply recognizing if the viewport was going to go up or down and then calculating the correct top or bottom index accordingly.

The direction of the scroll can be determined by code akin to (where dataIndex is the index of the row (within my base data set) that I want to make visible:

int viewportPosition = bodyLayerStack.viewportLayer.getRowPositionByIndex(dataIndex);
int viewportHeight = bodyLayerStack.viewportLayer.getRowCount();
boolean isVisible = (viewportPosition >= 0) && (viewportPosition < viewportHeight);
if (!isVisible) {
        if (viewportPosition < 0) {
            /*
             * The target row is below the viewport window so we want to calculate the
             * index of the bottom of the table.
             */
            viewPortTargetPosition = dataIndex - (viewportHeight / 2) + columnHeaderLayer.getRowCount();
            viewPortTargetPosition  = Math.max(0, viewPortTargetPosition);
        }
        else {
            /*
             * The target row is above the viewport window so we want to calculate the
             * index of the top row to show in the viewport.
             */
            viewPortTargetPosition = Math.min(dataIndex + (viewportHeight / 2) - 1, bodyLayerStack.bodyDataLayer.getRowCount()-1);
        }

    natTable.doCommand(new ShowRowInViewportCommand(bodyLayerStack.viewportLayer, viewPortTargetPosition));
        return true;
}
Kevin
  • 365
  • 2
  • 9
0

The title of your question is misleading. It seems to be unrelated to selection.

The actual question is how to find the current scroll position. And you can ask the ViewportLayerfor the last shown row and see if the row with the highest index is shown. In that case you are at the end.

Or you check the scrollbar state. The it is unrelated to NatTable and moreover a general SWT topic.

Dirk Fauth
  • 4,128
  • 2
  • 13
  • 23
  • The problem is `code`ShowRowInViewportCommand( natTable.getBodyLayer().getViewportLayer(), rowPosition)); `code` should ideally scroll to the rowPosition. Instead the scroll is misplaced and rowPosition is not visible. The solution I was working with is a workaround, so nattable scrolls to appropriate row. I havent yet managed to progrmmatically scroll to the particular row. – SDS Sep 22 '17 at 21:04
  • Either you are reporting a bug as you say that the feature does not work the way it should. And I don't think that is the case for a default configuration. Either you are not using it correctly, e.g. misunderstanding of index and position or you are using word wrapping and dynamic height calculation with additional features that might lead to a similar issue. There is not enough information and the question points to something else. – Dirk Fauth Sep 23 '17 at 06:00
  • Sorry for not framing the question correctly. This is an issue which is already reported in https://www.eclipse.org/forums/index.php/t/1066912/ . As described in the answer https://stackoverflow.com/questions/17593049/scroll-nattable-programmatically. I was expecting use of ShowRowInViewportCommand will update the view with the rowPosition. Correct me if I am wrong, if I am understanding the feature incorrectly. My usecase is a simple "Link to row" action similar to "Link to editor" in Package Explorer. So when the user clicks this action associated Row is visible in the NatTable. – SDS Sep 24 '17 at 16:05
  • As said before, you don't provide enough information. It should exactly work as described. – Dirk Fauth Sep 24 '17 at 21:49
  • I have a FullFeaturedBodyStackLayer and based on user selection in nattable the properties view is updated. For large dataset >500 rows, if the user would have scrolled the table up or down,the selected row is not visible. So, I provide "Link to Row" action which updates the natTable view to show the selectedRow. The ShowRowInViewportCommand( natTable.getBodyLayer().getViewportLayer(), rowPosition)); doesnot scroll to selected row. I fetch the rowPosition from SelectionLayer and returns the correct rowPosition. Is there a way to fix the view? – SDS Sep 25 '17 at 15:58
  • I think the problem is that you use the ViewportLayer as command parameter. That should be the SelectionLayer in your composition. – Dirk Fauth Sep 25 '17 at 16:37
  • Using ViewPortLayer as parameter I am able to scroll close to selectedRow. However, with SelectionLayer, the table doesnot even scroll to rowPosition. I tried using CompositeFreezeLayer but there is no change in the behaviour. – SDS Sep 25 '17 at 18:44
  • hm, looks like ShowRowInViewportCommand really needs the ViewportLayer as parameter, otherwise the command processing is skipped in the hierarchy. But anyhow, using for example bodyLayerStack.getSelectionLayer().getSelectionAnchor().getRowPosition() as second parameter, it works for in the NatTable examples. So I assume your rowPosition is not correct. – Dirk Fauth Sep 26 '17 at 05:14