Eclipse Rcp系列 六 TreeView

Eclipse Rcp系列 六 TreeView

treeView的使用和TableView差不多,不同的是ContentProvider和LabelProvider的实现接口不同了。下边是个例子,看一下相信你就,明白了
 class KindLabelProvider extends LabelProvider {
  public String getText(Object obj) {
   if (obj instanceof Kind) {
    return obj.toString();
   }
   return null;
  }

  public Image getImage(Object obj) {
   // String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
   if (obj instanceof Kind) {
    String imageKey = ISharedImages.IMG_OBJ_FOLDER;
    PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
   }
   return null;
  }
 }

 class KindContentProvider implements IStructuredContentProvider,
   ITreeContentProvider {

  public Object[] getElements(Object parent) {
   if(parent instanceof Kind){
    return getChildren(parent);
   }
   return null;
  }

  public Object getParent(Object child) {
   if (child instanceof Node) {
    return ((Node) child).getParent();
   }
   return null;
  }

  public Object[] getChildren(Object parent) {
   if (parent instanceof Kind) {
    ArrayList children = ((Kind) parent).getChildren();
    
    return children.toArray(new Node[children.size()]);
    
   }
   return new Object[0];
  }

  public boolean hasChildren(Object parent) {
   if (parent instanceof Kind)
    return ((Kind) parent).hasChildren();
   return false;
  }

  public void dispose() {
   // TODO Auto-generated method stub

  }

  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
   // TODO Auto-generated method stub

  }
 }

你可能感兴趣的:(Eclipse Rcp系列 六 TreeView)