Vertical align 2 jface TreeViewers to specific element

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 02:28:35

问题


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?


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




回答2:


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



来源:https://stackoverflow.com/questions/8964914/vertical-align-2-jface-treeviewers-to-specific-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!