2
我有一段代碼和一個樹查看器作爲eclipse插件的一部分。該代碼是在這裏(這是直接從Eclipse的PDE樣品樹視圖拍攝):在Eclipse樹狀查看器中單獨選擇樹節點
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()是如何工作的。但是問題是即使我得到這個項目,我也無法(或者不確定如何)修改它。
感謝所有幫助
謝謝你的回答!有什麼方法可以使用這種方法,我可以得到它代表的底層對象(在我的示例代碼的情況下,TreeObjects)?我問這是因爲我需要根據它們所代表的對象的內部狀態來區分和對待每個節點。 – KWJ2104 2012-07-19 20:45:24
我相信你可以使用TreeItem上的getData()方法來獲取關聯的樹對象。我也相信這種聯繫是自動爲你做的。 – 2012-07-19 21:20:18