org.eclipse.jface.viewers.TreeViewer什么都没显示

时间:2014-01-10 14:47:09

标签: java swt jface treeviewer

为什么以下示例不起作用?

TreeNodeEx类允许类似LISP的语法初始化:

(Life, Archaea, (Bacteria), (Eukarya, Fungi, Plantae, Plantae, Protista))

即。列表中的第一个元素表示父元素和尾部表示它的成员。

public class View2 extends ViewPart {

    public static final String ID = "Try_EclipseRCPView_03.view2";
    //private static final Logger log = LoggerFactory.getLogger(View2.class);

    private TreeViewer viewer;

    public class TreeNodeEx extends TreeNode {

        private void constructor(List<Object> headedlist) {
            if( headedlist.size() == 0 ) {
                this.value = null;
            }
            else {
                this.value = headedlist.get(0);
                Object[] childrenObject = headedlist.subList(1, headedlist.size()).toArray();
                TreeNodeEx[] children = new TreeNodeEx[childrenObject.length];
                for(int i=0; i<childrenObject.length; ++i) {
                    children[i] = new TreeNodeEx(childrenObject[i]);
                }
                setChildren(children);
            }
        }

        public TreeNodeEx(Object value) {
            super(null);
            if(value instanceof Object[]) {
                constructor(Arrays.asList((Object[])value));
            }
            else {
                this.value = value;
            }
        }

        @Override
        public String toString() {
            return value.toString();
        }

        public String toFullString() {

            StringBuilder ans = new StringBuilder();
            boolean first;

            ans.append(toString());

            if( getChildren() != null && getChildren().length > 0 ) {
                ans.append("{");

                first = true;
                for(org.eclipse.jface.viewers.TreeNode node : getChildren()) {

                    if( !first ) {
                        ans.append(",");
                    }

                    ans.append(((TreeNodeEx)node).toFullString());
                    first = false;
                }

                ans.append("}");
            }

            return ans.toString();

        }

    }



    class ViewLabelProvider extends LabelProvider implements
            ITableLabelProvider {
        public String getColumnText(Object obj, int index) {
            return getText(obj);
        }

        public Image getColumnImage(Object obj, int index) {
            return getImage(obj);
        }

        public Image getImage(Object obj) {
            return PlatformUI.getWorkbench().getSharedImages().getImage(
                    ISharedImages.IMG_OBJ_ELEMENT);
        }
    }

    /**
     * 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);

        //viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        viewer.setContentProvider(new TreeNodeContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        // Provide the input to the ContentProvider

        // LISP syntax
        TreeNodeEx life = new TreeNodeEx(
                new Object[] {"Life", 
                        "Archaea", 
                        new Object[] {"Bacteria"},
                        new Object[] {"Eukarya",
                            "Fungi", "Plantae", "Plantae", "Protista"}
                }
            );

        //log.info(life.toString());
        System.out.println(life.toFullString());

        viewer.setInput(life);


    }

    /**
     * Passing the focus request to the viewer's control.
     */
    public void setFocus() {
        viewer.getControl().setFocus();
    }
}

文本输出显示结构正常。

1 个答案:

答案 0 :(得分:3)

TreeNodeContentProvider期望setInput值为TreeNode的数组,而不是单个树节点。尝试

viewer.setInput(new TreeNode [] {life});