josecampos
josecampos

Reputation: 861

List folders and files with Tree on Eclipse

Now I am developing a sample plugin for Eclipse, to show all directories and files from my computer, using JFace (Tree and TreeView).

I want something like this:

----------------------------------
| Element  | Number of Childrens |
----------------------------------
| + usr    | 5                   |
|  + bin   | 4                   |
|     file |                     |
|  + games | 2                   |
----------------------------------

I create the plugin View witch ViewPart, the ContentProvider and the TreeLabelProvider.

public class ListFiles extends ViewPart {
    /** The ID of the view as specified by the extension. */
    public static final String ID = "sortnumbers.views.SortNumbers";

    /**
     * The constructor.
     */
    public ListFiles() { }

    /**
     * This is a callback that will allow us to create the viewer and initialize it
     */
    public void createPartControl(Composite parent) {
        Tree tree = new Tree(parent, SWT.MULTI);
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);

        TreeColumn column0 = new TreeColumn(tree, SWT.NONE);
        column0.setText("Element");
        column0.setWidth(300);
        TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
        column1.setText("Number");
        column1.setWidth(300);

        TreeViewer treeviewer = new TreeViewer(tree);
        treeviewer.setContentProvider(new TreeContentProvider());
        treeviewer.setLabelProvider(new TreeLabelProvider());
        treeviewer.setInput(new FolderNode(new File("/")));
    }

    @Override
    public void setFocus() {
        // TODO
    }
}

public class TreeContentProvider implements ITreeContentProvider
{
    public Object[] getChildren(Object parentElement) {
        return ((ITreeNode)parentElement).getChildren().toArray();
    }

    public Object getParent(Object element) {
        return ((ITreeNode)element).getParent();
    }

    public boolean hasChildren(Object element) {
        return ((ITreeNode)element).hasChildren();
    }

    public Object[] getElements(Object inputElement) {
        return getChildren(inputElement);
    }

    public void dispose() {
    }

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

public class TreeLabelProvider extends LabelProvider
{
    public String getText(Object element) {
        return ((ITreeNode)element).getName();
    }
}

To search for all Files in my computer I create the interface ITreeNode, TreeNode, FolderNode and FileNode...

public interface ITreeNode
{
    public String getName();

    public List getChildren();

    public boolean hasChildren();

    public ITreeNode getParent();
}

public abstract class TreeNode implements ITreeNode
{
    protected ITreeNode fParent;
    protected List fChildren;

    public TreeNode(ITreeNode parent) {
        fParent = parent;
    }

    public boolean hasChildren() {
        return true;
    }

    public ITreeNode getParent() {
        return fParent;
    }

    public List getChildren() 
    {
        if( fChildren != null )
            return fChildren;

        fChildren = new ArrayList();
        createChildren(fChildren);

        return fChildren;
    }

    /* subclasses should override this method and add the child nodes */
    protected abstract void createChildren(List children);
}

public class FolderNode extends TreeNode
{
    private File fFolder; /* actual data object */

    public FolderNode(File folder) {
        this(null, folder);
    }

    public FolderNode(ITreeNode parent, File folder) {
        super(parent);
        fFolder = folder;
    }

    public String getName() {
        return "FOLDER: " + fFolder.getName();
    }

    @Override
    protected void createChildren(List children)
    {
        File[] childFiles = fFolder.listFiles();
        for(int i=0; i<childFiles.length; i++)
        {
            File childFile = childFiles[i];
            if( childFile.isDirectory() )
                children.add(new FolderNode(this, childFile));
            else
                children.add(new FileNode(this, childFile));
        }
    }
}

public class FileNode extends TreeNode
{
    private File fFile; /* actual data object */

    public FileNode(ITreeNode parent, File file)
    {
        super(parent);
        fFile = file;
    }

    public String getName() {
        return "FILE: " + fFile.getName();
    }

    protected void createChildren(List children) {
    }

    public boolean hasChildren() {
        return false;
    }
}

But the result was:

----------------------------------
| Element  | Number of Childrens |
----------------------------------
| + usr    | + usr               |
|  + bin   |  + bin              |
|     file |     file            |
|  + games |  + games            |
----------------------------------

I cannot understand why TreeView fill all fields with the same string... What I need to change?

-- Thanks in advance


Alexey Romanov, thank you... :-)

Now I need to sort all elements in the TreeView by column (by name for the first column and by size for the second column). Should I create a Class extended from ViewerComparator or ViewerSorter? And how can implement compare method to do this?

-- Thanks in advance

Upvotes: 1

Views: 2303

Answers (2)

Buddhika Hasthanayake
Buddhika Hasthanayake

Reputation: 1299

josecampos : Hi i have gone through the source code at Eclipse Community but there are some errors. This is not an answer to the question. I post here since I cannot add a comment to the answer above

  1. in FolderNode.java

                children.add(new FolderNode(this, childFile));
                children.add(new FileNode(this, childFile.getName()));
    

give 2 errors of the same type:

The method add(String) in the type List is not applicable for the arguments (FolderNode)

  1. in TreeContentProvider.java

    return ((ITreeNode)parentElement).getChildren().toArray();
    

gives an error :

The method toArray() is undefined for the type List

  1. in TreeLableProvider

    public Image getColumnImage(Object element, int columnIndex) {
    

    gives

The return type is incompatible with ITableLabelProvider.getColumnImage(Object, int)

And

 return Integer.toString(((ITreeNode)element).getChildren().size());

gives

The method toString(int) in the type Integer is not applicable for the arguments (Dimension)

  1. Finally in TreeSorter.java

    res = (int) (node1.getChildren().getSize() - node2.getChildren().getSize());
    

gives

The operator - is undefined for the argument type(s) java.awt.Dimension, java.awt.Dimension

Upvotes: 0

Alexey Romanov
Alexey Romanov

Reputation: 170723

You need to use ITableLabelProvider:

public class TreeLabelProvider extends BaseLabelProvider implements ITableLabelProvider
{
    public Image getColumnImage(Object element, int columnIndex) {
        return null;
    }

    public String getColumnText(Object element, int columnIndex) {
        if (columnIndex == 0)
            return ((ITreeNode)element).getName();
        else
            return ((ITreeNode)element).getChildren().size();
    }
}

Upvotes: 1

Related Questions