TreeViewer 使用

import java.io.File;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

public class TreeColumnTest2 extends Shell {

 private Tree tree;
 /**
  * Launch the application
  * @param args
  */
 public static void main(String args[]) {
  try {
   Display display = Display.getDefault();
   TreeColumnTest2 shell = new TreeColumnTest2(display, SWT.SHELL_TRIM);
   shell.open();
   shell.layout();
   while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
     display.sleep();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * Create the shell
  * @param display
  * @param style
  */
 public TreeColumnTest2(Display display, int style) {
  super(display, style);
  createContents();
 }

 /**
  * Create contents of the window
  */
 protected void createContents() {
  setText("SWT Application");
  setSize(584, 375);

  final TreeViewer viewer = new TreeViewer(this, SWT.FULL_SELECTION);
  tree = viewer.getTree();
  tree.setBounds(24, 10, 523, 300);
  viewer.getTree().setHeaderVisible(true);
  TreeColumn column = new TreeColumn(viewer.getTree(), SWT.LEFT);
  column.setText("Name");
  column.setWidth(200);
  column = new TreeColumn(viewer.getTree(), SWT.LEFT);
  column.setText("Size");
  column.setWidth(100);
  column = new TreeColumn(viewer.getTree(), SWT.LEFT);
  column.setText("Hidden");
  column.setWidth(100);
  viewer.setContentProvider(new MyTreeContenetProvider());
  viewer.setLabelProvider(new MyTableLableProvider());
  viewer.setInput(new File("/"));

  //
 }

 @Override
 protected void checkSubclass() {
  // Disable the check that prevents subclassing of SWT components
 }

 class MyTreeContenetProvider implements ITreeContentProvider {

  public Object[] getChildren(Object parentElement) {
   File file = (File) parentElement;
   if (file.isDirectory())
    return file.listFiles();
   else
    return null;
  }

  public Object getParent(Object element) {
   System.err.println("调用没");
   File file = (File) element;
//   return file.getParentFile();
   return null;
  }

  public boolean hasChildren(Object element) {
   File file = (File) element;
   return file.isDirectory()/*&&file.list().length>0*/;
  }

  public Object[] getElements(Object inputElement) {
   File file = (File) inputElement;
   return file.isDirectory() ? file.listFiles()
     : new Object[] { file };
  }

  public void dispose() {

  }

  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

  }

 }

    class MyTableLableProvider implements ITableLabelProvider{

         public Image getColumnImage(Object element, int columnIndex) {
             return null;
          }

         public String getColumnText(Object element, int columnIndex) {
              File file=(File)element;
             switch (columnIndex) {
             case 0:
                 return file.getName();
             case 1:
                 return ""+file.length();
             case 2:
                 return ""+file.isHidden();
             default:
                 return "";
              }
          }

         public void addListener(ILabelProviderListener listener) {
            
          }

         public void dispose() {
            
          }

         public boolean isLabelProperty(Object element, String property) {
             return false;
          }

         public void removeListener(ILabelProviderListener listener) {
            
          }
        
      }

}

你可能感兴趣的:(eclipse)