现在我正在为Eclipse开发一个示例插件,使用JFace(Tree和TreeView)显示计算机中的所有目录和文件。
我想要这样的事情:
----------------------------------
| Element | Number of Childrens |
----------------------------------
| + usr | 5 |
| + bin | 4 |
| file | |
| + games | 2 |
----------------------------------
我创建了插件查看女巫 ViewPart , ContentProvider 和 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();
}
}
要搜索计算机中的所有文件,我创建界面 ITreeNode , TreeNode , FolderNode 和 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;
}
}
但结果是:
----------------------------------
| Element | Number of Childrens |
----------------------------------
| + usr | + usr |
| + bin | + bin |
| file | file |
| + games | + games |
----------------------------------
我无法理解为什么TreeView用相同的字符串填充所有字段......我需要更改什么?
- 提前致谢
Alexey Romanov,谢谢......: - )
现在我需要按列对TreeView中的所有元素进行排序(第一列的名称和第二列的大小)。我应该创建一个从ViewerComparator或ViewerSorter扩展的类吗? 如何实现比较方法呢?
- 提前致谢
答案 0 :(得分:1)
您需要使用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();
}
}
答案 1 :(得分:0)
josecampos:您好我已经查看了Eclipse Community的源代码,但是有一些错误。这不是问题的答案。我在这里发帖,因为我无法在上面的答案中添加评论
children.add(new FolderNode(this, childFile));
children.add(new FileNode(this, childFile.getName()));
给出2个相同类型的错误:
类型List中的方法add(String)不适用于 arguments(FolderNode)
return ((ITreeNode)parentElement).getChildren().toArray();
给出错误:
未定义类型List
的方法toArray()
public Image getColumnImage(Object element, int columnIndex) {
给出
返回类型与ITableLabelProvider.getColumnImage(Object,int)不兼容
并且
return Integer.toString(((ITreeNode)element).getChildren().size());
给出
Integer类型中的toString(int)方法不适用于参数(Dimension)
最后在TreeSorter.java
中res = (int) (node1.getChildren().getSize() - node2.getChildren().getSize());
给出
操作符 - 未定义参数类型java.awt.Dimension,java.awt.Dimension