org.eclipse.jface.viewers.TreeViewer shows nothing

321 views Asked by At

Why doesn't example below work?

TreeNodeEx class is one that allows LISP-like syntax initialization:

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

I.e. first element in the list means parent and tail means it's members.

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();
    }
}

textual output shows structure appears fine.

1

There are 1 answers

0
greg-449 On BEST ANSWER

TreeNodeContentProvider expects the setInput value to be an array of TreeNode rather than a single tree node. Try

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