Vertical align 2 jface TreeViewers to specific element

964 views Asked by At

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?

2

There are 2 answers

3
Frettman On

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).

1
Tonny Madsen On

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