如何仅在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;
}
}
答案 0 :(得分:0)
public Object[] getElements(Object inputElement) {
return ResourcesPlugin.getWorkspace().getRoot().getProjects();
}
我现在能够获得项目。我的代码正在运行
答案 1 :(得分:0)
Eclipse框架中已经存在工作空间对象的内容提供者和标签提供者:
使用它们,您的代码将如下所示:
{% block body %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h4>What sound does it make?</h4>
<img src="{{ pic_details.imgsrc.url }}" alt="image">
</form>
{% endblock %}