0

I have two TreeViewer objects on a page (2 columns, one TreeViewer in each column), and I want to vertically align a tree when the other is scrolled or selected.

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Tree;

I think the solution should look something like

treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  @Override
  public void selectionChanged(SelectionChangedEvent arg0) {
     TreeViewer mirrorTree = (treeViewer == treeVwrSource ? treeVwrTarget : treeVwrSource);
     // find position of selected element (element x) in treeViewer
     // set position of element x in mirrorTree, it is already selected.
  }
});

Any advice?

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
karnbo
  • 191
  • 6

2 Answers2

1

From what I've read, scrolling tables or trees isn't directly possible (unless that has changed in the meantime). Programmatically changing the position of the thumb of the scrollbar would not change the viewport of the table/tree.

But you could try if the following snippet works for you:

Tree tree1 = ...;
Tree tree2 = ...;

int topIndex = tree1.indexOf(tree1.getTopItem());
tree2.setTopItem(tree2.getItem(topIndex));

You would call that code in a SelectionListener registered to the vertical scrollbar of your tree (tree.getVerticalBar()).

Synchronizing the selection is fairly easy (if both tree viewers display the same input/model):

viewer.setSelection(otherViewer.getSelection)

(called by the ISelectionChangedListener from your question).

Frettman
  • 2,251
  • 1
  • 13
  • 9
  • Hi. Thanks for your help. I've tried the approach, and the tree1.getTopItem() returns the item listed on top after the scroll, as expected. However, the tree1.indexOf(...), always returns -1, regardless of which direction I'm scrolling... – karnbo Mar 09 '12 at 20:12
  • BTW the 'setSelection' works fine, thanks. However, I have the same problem here with alignment... The selection in the right pane could be on the top and left pane selection could be on the bottom... quite disturbing actually... – karnbo Mar 09 '12 at 20:27
  • From the Javadoc of `indexOf(..)`: "If no item is found, returns -1." So the `Tree` doesn't find an item it just gave you? That doesn't make much sense. I can't think of any reason how `tree1.indexOf(tree1.getTopItem());` could ever return -1. – Frettman Mar 12 '12 at 08:43
1

For a complete example of how to synchronize two tables see this SWT Snippet.

Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
  • Hi Tonny. Yes maybe the solution is to use table instead. 'table2.setTopIndex(table1.getTopIndex())' seems to do the trick. I will try this if I dont find another solution soon. Thanks. – karnbo Mar 09 '12 at 20:20