2014-01-10 80 views
0

爲什麼下面的例子不工作?org.eclipse.jface.viewers.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(); 
    } 
} 

文本輸出顯示結構顯示正常。

回答

3

TreeNodeContentProvider預計setInput值爲TreeNode的數組而不是單個樹節點。嘗試

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

不可思議!怎麼可能知道? –

+2

我讀過來源 –

相關問題