gpt4 book ai didi

java - 在 Eclipse Tree Viewer 中单独选择树节点

转载 作者:行者123 更新时间:2023-11-30 06:29:26 26 4
gpt4 key购买 nike

作为 eclipse 插件的一部分,我有一段带有树查看器的代码。代码在这里(这是直接取自 eclipse PDE 示例 TreeView ):

public class SampleView extends ViewPart {

/**
* The ID of the view as specified by the extension.
*/
public static final String ID = "com.test2.views.SampleView";

private TreeViewer viewer;
private DrillDownAdapter drillDownAdapter;
private Action action1;
private Action action2;
private Action doubleClickAction;

/*
* The content provider class is responsible for
* providing objects to the view. It can wrap
* existing objects in adapters or simply return
* objects as-is. These objects may be sensitive
* to the current input of the view, or ignore
* it and always show the same content
* (like Task List, for example).
*/

class TreeObject implements IAdaptable {
private String name;
private TreeParent parent;

public TreeObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setParent(TreeParent parent) {
this.parent = parent;
}
public TreeParent getParent() {
return parent;
}
public String toString() {
return getName();
}
public Object getAdapter(Class key) {
return null;
}
}

class TreeParent extends TreeObject {
private ArrayList children;
public TreeParent(String name) {
super(name);
children = new ArrayList();
}
public void addChild(TreeObject child) {
children.add(child);
child.setParent(this);
}
public void removeChild(TreeObject child) {
children.remove(child);
child.setParent(null);
}
public TreeObject [] getChildren() {
return (TreeObject [])children.toArray(new TreeObject[children.size()]);
}
public boolean hasChildren() {
return children.size()>0;
}
}

class ViewContentProvider implements IStructuredContentProvider,
ITreeContentProvider {
private TreeParent invisibleRoot;

public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if (parent.equals(getViewSite())) {
if (invisibleRoot==null) initialize();
return getChildren(invisibleRoot);
}
return getChildren(parent);
}
public Object getParent(Object child) {
if (child instanceof TreeObject) {
return ((TreeObject)child).getParent();
}
return null;
}
public Object [] getChildren(Object parent) {
if (parent instanceof TreeParent) {
return ((TreeParent)parent).getChildren();
}
return new Object[0];
}
public boolean hasChildren(Object parent) {
if (parent instanceof TreeParent)
return ((TreeParent)parent).hasChildren();
return false;
}
/*
* We will set up a dummy model to initialize tree heararchy.
* In a real code, you will connect to a real model and
* expose its hierarchy.
*/
private void initialize() {
TreeObject to1 = new TreeObject("Leaf 1");
TreeObject to2 = new TreeObject("Leaf 2");
TreeObject to3 = new TreeObject("Leaf 3");
TreeParent p1 = new TreeParent("Parent 1");
p1.addChild(to1);
p1.addChild(to2);
p1.addChild(to3);

TreeObject to4 = new TreeObject("Leaf 4");
TreeParent p2 = new TreeParent("Parent 2");
p2.addChild(to4);

TreeParent root = new TreeParent("Root");
root.addChild(p1);
root.addChild(p2);

invisibleRoot = new TreeParent("");
invisibleRoot.addChild(root);
}
}
class ViewLabelProvider extends LabelProvider {

public String getText(Object obj) {
return obj.toString();
}
public Image getImage(Object obj) {
String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
if (obj instanceof TreeParent)
imageKey = ISharedImages.IMG_OBJ_FOLDER;
return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
}
}
class NameSorter extends ViewerSorter {
}

/**
* The constructor.
*/
public SampleView() {
}

/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
drillDownAdapter = new DrillDownAdapter(viewer);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());

// Create the help context id for the viewer's control
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "com.test2.viewer");
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
}

我想以某种方式能够单独遍历树上的所有节点并控制它们展开或折叠的方式。基本上我只想知道如何能够遍历树上的所有节点以单独调整设置。

这应该是一个简单的问题,但我似乎还没有找到任何有用的东西......我试图弄清楚 TreeViewer 对象的 getItem() 到底是如何工作的。但问题是即使我得到了该项目,我也不能(或不确定如何)修改它。

感谢您的帮助

最佳答案

首先从查看器获取底层树:

Tree tree = viewer.getTree();

然后获取树中的第一层项目:

TreeItem[] item = tree.getItems();

任何树项都可以回答它的所有直接子项:

TreeItem[] item = aTreeItem.getItems();

一旦你得到一个树项目,你就可以展开或折叠它:

aTreeItem.setExpanded(true);

因此对所有树项目的访问必须是递归的。

关于java - 在 Eclipse Tree Viewer 中单独选择树节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11568682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com