2

I tried adding a number for each row in my tree. The tree and its numbering is working but not that great so is there any other best way to implement this? because if the tree node expanded I have to get all the expanded tree node and increase its numbering. I also used the TreeViewer to create the tree.

enter image description here

// base container
Composite composite = new Composite( (Composite) parent, SWT.NONE );
composite.setLayout( new GridLayout( 2, false ) );
composite.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );

// The container for numbering in the tree
Composite numComposite = new Composite( composite, SWT.NONE );
numComposite.setLayout( new FillLayout( SWT.VERTICAL ) );
numComposite.setLayoutData( new GridData( GridData.VERTICAL_ALIGN_BEGINNING ) );

// the tree container
MyTree tree = new MyTree( composite );
tree.setContentProvider( new ContentProvider() );

Label test = new Label( numComposite, SWT.NONE );
test.setText( "1" );
test = new Label( numComposite, SWT.NONE );
test.setText( "2" );
test = new Label( numComposite, SWT.NONE );
test.setText( "3" );
test = new Label( numComposite, SWT.NONE );
test.setText( "4" );
test = new Label( numComposite, SWT.NONE );
test.setText( "5" );
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Kevin King
  • 557
  • 1
  • 9
  • 25
  • Using separate labels is going to give you alignment problems as there is no guarantee that the tree row height is the same as the label heights / spacing - and it will vary between platforms. Even in your picture the alignment is already slipping. I can't think of a good way to do this. – greg-449 Oct 20 '16 at 07:23
  • Seeing `MyTree` I'd like to remind that [SWT controls are not meant to be subclassed](http://stackoverflow.com/questions/4264983/why-is-subclassing-not-allowed-for-many-of-the-swt-controls). – Rüdiger Herrmann Oct 20 '16 at 10:33
  • The [Nebula Grid](http://www.eclipse.org/nebula/widgets/grid/grid.php) might be worth a look. It does row numbering, but maybe not the way you'd like it to be. It'll number items based on their position of the fully expanded tree rather than just numbering the visible items from 1 to n. – Baz Oct 20 '16 at 12:42

1 Answers1

5

I suggest to start with a custom Canvas control that has the same height as the Tree widget.

Whenever the vertical scroll position of the tree is changed the Canvas need to be redrawn (canvas.redraw()). To track such changes, several listeners need to be added

  • a selection listener (tree.getVerticalBar().addSelectionListener()) on the scroll bar
  • a key listener (tree.addKeyListener()) that updates the Canvas when navigation keys such as Up/Down, PgUp/PgDn, etc. are pressed
  • a resize listener (tree.addControlListener()) that updates the size and content of the Canvas when the tree is resized
  • ... probably some more ...

The paint listener of the custom Canvas could draw the row numbers. The first row number could be derived from the top index (tree.getTopIndex()). Subsequent row numbers would use the tree's item height (tree.getItemHeight()) to align themselves with the rows.


I have uploaded a draft of the above-sketched solution here:

https://gist.github.com/rherrmann/fc248fb01a00fb4fa73187cbbdaf441f

A main method is included that that demonstrates the row number ruler with a small SWT application.

The preferred width is currently derived from the width of the string "9999" in the current font. But since the maximum row number can always be computed with getVisibleItemCount() it shouldn't be hard to adjust the preferred width provide enough room for the row numbers currently being displayed.

The tricky part will probably be to determine when to relayout the parent of Tree and TreeRule after the preferred width changed.

There may be minor glitches here and there that need to be addressed, but overall, the code shows the feasibility of this approach.

The code in getVisibleItemCount() is copied from an SWT Snippet and then adjusted.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • 1
    +1 Very nice example. Hopefully, there's a good way to measure the width of the ruler based on the number of visible items in the tree. – Baz Oct 21 '16 at 08:14