1

ワークスペースのコンテンツのみを表示するにはどうすればよいTreeViewerですか?

これは私のメインビュークラスです:

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {
    public static final String ID = "com.MainView";

    public MainView() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void createPartControl(Composite parent) {
        TreeViewer tv = new TreeViewer(parent);
        tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
        tv.setContentProvider(new FileTreeContentProvider());
        tv.setLabelProvider(new FileTreeLabelProvider());
        tv.setInput(ResourcesPlugin.getWorkspace());
    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub

    }

}

これは私のコンテンツプロバイダークラスです:

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class FileTreeContentProvider implements ITreeContentProvider {

    public void dispose() {

    }

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

    }

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

    public Object[] getChildren(Object parentElement) {
        if (parentElement instanceof IProject) {
            IProject projects = (IProject) parentElement;
            try {
                return projects.members();
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (parentElement instanceof IFolder) {
            IFolder ifolder = (IFolder) parentElement;
            try {
                return ifolder.members();
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;
    }

    public Object getParent(Object element) {
        if (element instanceof IProject) {
            IProject projects = (IProject) element;
            return projects.getParent();
        }
        if (element instanceof IFolder) {
            IFolder folder = (IFolder) element;
            return folder.getParent();
        }
        if (element instanceof IFile) {
            IFile file = (IFile) element;
            return file.getParent();
        }
        return null;
    }

    public boolean hasChildren(Object element) {
        if (element instanceof IProject) {
            IProject projects = (IProject) element;
            try {
                return projects.members().length > 0;
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (element instanceof IFolder) {
            IFolder folder = (IFolder) element;
            try {
                return folder.members().length > 0;
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }

}

これは私のラベルプロバイダークラスです:

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

public class FileTreeLabelProvider implements ILabelProvider {

    public void addListener(ILabelProviderListener listener) {

    }

    public void dispose() {

    }

    public boolean isLabelProperty(Object element, String property) {

        return false;
    }

    public void removeListener(ILabelProviderListener listener) {

    }

    public Image getImage(Object element) {

        return null;
    }

    public String getText(Object element) {
        if (element instanceof IProject) {
            String text = ((IProject) element).getName();
            return text;
        }
        if (element instanceof IFolder) {
            String text = ((IFolder) element).getName();
            return text;
        }
        if (element instanceof IFile) {
            String text = ((IFile) element).getName();
            return text;
        }

        return null;
    }
}
4

2 に答える 2

0
public Object[] getElements(Object inputElement) {
        return ResourcesPlugin.getWorkspace().getRoot().getProjects();
    }

プロジェクトを取得できるようになりました。コードは機能しています

于 2013-01-16T05:31:39.183 に答える