View on GitHub

Eclipse Tutorial

Ecsoya

online tutorialsWIKIoffline tutorialsPPT

TreeViewer Tutorial

TreeViewer和TableViewer的用法基本相似,最大的不同就是TreeViewer有子节点,所以这个也体现在content provider的不同上。

如:

class BookTreeContentProvider implements ITreeContentProvider {
    private Map<Library, Set<Author>> authorMap = new HashMap<Library, Set<Author>>();

    public void dispose() {
        authorMap.clear();
    }

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

    public Object[] getElements(Object inputElement) {
        if (inputElement instanceof List<?>) {
            return ((List<?>) inputElement).toArray();
        }
        return new Object[0];
    }

    public Object[] getChildren(Object parentElement) {
        if (parentElement instanceof Library) {
            Set<Author> authors = authorMap.get(parentElement);
            if (authors == null) {
                authors = new HashSet<Author>();
                List<Book> books = ((Library) parentElement).getBooks();
                for (Book book : books) {
                    Author author = book.getAuthor();
                    if (author == null) {
                        continue;
                    }
                    authors.add(author);
                }
                authorMap.put((Library) parentElement, authors);
            }
            return authors.toArray();
        } else if (parentElement instanceof Author) {
            return ((Author) parentElement).getBooks().toArray();
        }
        return new Object[0];
    }

    public Object getParent(Object element) {
        if (element instanceof Book) {
            return ((Book) element).getAuthor();
        }
        return null;
    }

    public boolean hasChildren(Object element) {
        return getChildren(element).length != 0;
    }
}

其中:

  1. getElement()方法取的是根目录元素。
  2. getChildren()方法取的是每一个元素的子节点,包含根目录元素。
  3. hasChildren()方法是在getChildren()方法调用之前判断有没有子节点的。
  4. getParent()方法对应取到的是子节点的父节点元素。

参考资料:

  1. JFace snippets and examples

Back to Home 上一篇:TableViewer Tutorial 下一篇:JFace Dialog Tutorial