如何在Treeviewer中展开对象? [英] How to expand an object in a Treeviewer?

查看:100
本文介绍了如何在Treeviewer中展开对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解Java树形视图.我使用以下教程创建了Treeviewer:

I am trying to understand java treeviews. I created a Treeviewer with the following tutorial:

http://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm

现在我有一个无法解决的问题:/

Now I have a problem that I can´t solve :/

我想用另一个对象来扩展书本对象.

I want to expand the book-objects with another object.

例如:

+ BookTitleX      BookAuthorX       etc
    otherobjectname     etc      etc  etc

如何将对象添加到对象?

How can I add an object to an object ?

非常感谢你

这是代码:

内容提供商:

import java.util.Iterator;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;


public class ContentProvider implements ITreeContentProvider, IDeltaListener {
    private static Object[] EMPTY_ARRAY = new Object[0];
    protected TreeViewer viewer;

    /*
     * @see IContentProvider#dispose()
     */
    public void dispose() {}

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        this.viewer = (TreeViewer)viewer;
        if(oldInput != null) {
            removeListenerFrom((MovingBox)oldInput);
        }
        if(newInput != null) {
            addListenerTo((MovingBox)newInput);
        }
    }

    /** Because the domain model does not have a richer
     * listener model, recursively remove this listener
     * from each child box of the given box. */
    protected void removeListenerFrom(MovingBox box) {
        box.removeListener(this);
        for (Iterator iterator = box.getBoxes().iterator(); iterator.hasNext();) {
            MovingBox aBox = (MovingBox) iterator.next();
            removeListenerFrom(aBox);
        }
    }

    /** Because the domain model does not have a richer
     * listener model, recursively add this listener
     * to each child box of the given box. */
    protected void addListenerTo(MovingBox box) {
        box.addListener(this);
        for (Iterator iterator = box.getBoxes().iterator(); iterator.hasNext();) {
            MovingBox aBox = (MovingBox) iterator.next();
            addListenerTo(aBox);
        }
    }


    public Object[] getChildren(Object parentElement) {
        if(parentElement instanceof MovingBox) {
            MovingBox box = (MovingBox)parentElement;
            return concat(box.getBoxes().toArray(), 
                box.getBooks().toArray(), box.getGames().toArray());
        }
        return EMPTY_ARRAY;
    }

    protected Object[] concat(Object[] object, Object[] more, Object[] more2) {
        Object[] both = new Object[object.length + more.length + more2.length];
        System.arraycopy(object, 0, both, 0, object.length);
        System.arraycopy(more, 0, both, object.length, more.length);
        System.arraycopy(more2, 0, both, object.length + more.length, more2.length);        
        return both;
    }

    /*
     * @see ITreeContentProvider#getParent(Object)
     */
    public Object getParent(Object element) {
        if(element instanceof Model) {
            return ((Model)element).getParent();
        }
        return null;
    }

    /*
     * @see ITreeContentProvider#hasChildren(Object)
     */
    public boolean hasChildren(Object element) {
        return getChildren(element).length > 0;
    }

    /*
     * @see IStructuredContentProvider#getElements(Object)
     */
    public Object[] getElements(Object inputElement) {
        return getChildren(inputElement);
    }

    /*
     * @see IDeltaListener#add(DeltaEvent)
     */
    public void add(DeltaEvent event) {
        Object movingBox = ((Model)event.receiver()).getParent();
        viewer.refresh(movingBox, false);
    }

    /*
     * @see IDeltaListener#remove(DeltaEvent)
     */
    public void remove(DeltaEvent event) {
        add(event);
    }

}

这是具有输入内容的查看器:

And this is the Viewer with the inputs:

public MovingBox getInitalInput() throws SQLException {
    root = new MovingBox();
    MovingBox someBooks = new MovingBox("Books");
    MovingBox games = new MovingBox("Games");
    MovingBox books = new MovingBox("More books");
    MovingBox games2 = new MovingBox("More games");
    MovingBox test = new MovingBox("Test");

    root.add(someBooks);
    root.add(games);
    root.add(new MovingBox());
    root.add(test);

    String tabelleName = "PACKAGE";

    Db2Connector db2conn = new Db2Connector(Db2ConnectorParms.DB2HOST, Db2ConnectorParms.DB2PORT, "DB2R",
            Db2ConnectorParms.USERID, Db2ConnectorParms.PASSWORD);

    String db2drl = new String(
            "select PKID, STAGEID, DESCRIPTION, STATUS, PACKAGE_SMML_KZ, LAST_ACTION_RC, START_TIME, END_TIME, CREATE_TIME,"
                    + "CREATE_USER, CAST_TIME, CAST_USER, APPROVE_TIME, APPROVE_USER, EXECUTE_TIME, EXECUTE_USER, LAST_ACTION, LAST_ACTION_USER"
                    + "    from " + tabelleName);

    try {
        db2conn.connect();
        ResultSet rs = db2conn.executeQuery(db2drl);

        while (rs.next()) {
            String packageId = rs.getString("PKID");
            String stageId = rs.getString("STAGEID");
            String description = rs.getString("DESCRIPTION");

            if (rs.wasNull()) {
                packageId = "";
            } else {
                packageId = packageId.trim();
            }

            Package testpackage = new Package(packageId, stageId, description);
            root.add(testpackage);

            db2conn.close();
        }
    }

    finally {

    }

    someBooks.add(books);
    games.add(games2);

    books.add(new CCID("Taj Mahal", "Reiner", "Knizia"));
    books.add(new Package("Cryptonomicon", "Neal", "Stephenson"));
    books.add(new Package("Smalltalk, Objects, and Design", "Chamond", "Liu"));
    books.add(new Package("A Game of Thrones", "George R. R.", " Martin"));
    books.add(new Package("The Hacker Ethic", "Pekka", "Himanen"));
    // books.add(new MovingBox());

    books.add(new Package("The Code Book", "Simon", "Singh"));
    books.add(new Package("The Chronicles of Narnia", "C. S.", "Lewis"));
    books.add(new Package("The Screwtape Letters", "C. S.", "Lewis"));
    books.add(new Package("Mere Christianity ", "C. S.", "Lewis"));
    games.add(new CCID("Tigris & Euphrates", "Reiner", "Knizia"));
    games.add(new CCID("La Citta", "Gerd", "Fenchel"));
    games.add(new CCID("El Grande", "Wolfgang", "Kramer"));
    games.add(new CCID("The Princes of Florence", "Richard", "Ulrich"));
    games.add(new CCID("The Traders of Genoa", "Rudiger", "Dorn"));
    games2.add(new CCID("Tikal", "M.", "Kiesling"));
    games2.add(new CCID("Modern Art", "Reiner", "Knizia"));
    return root;
}

我想添加一个对象,例如. CCId到测试包.

I want to add an object for eg. CCId to the testpackage.

谢谢.

推荐答案

getChildren方法似乎正在返回PackageCCID(可能还有其他?)对象的数组.

You getChildren method appears to be returning an array of Package, CCID (and maybe other?) objects.

当树查看器想要显示这些对象的子代时,它将调用getChildren方法,其中一个对象为parentElement.因此,您需要添加以下代码:

When the tree viewer wants to show the children of those objects it will call getChildren method with one of those objects as the parentElement. So you need to add code like:

if (parentElement instanceof Package) {
  Package package = (Package)parentElement;
  ... return the children for 'package'
}

if (parentElement instanceof CCID) {
  CCID ccid = (CCID)parentElement;
  ... return the children for 'ccid'
}

这篇关于如何在Treeviewer中展开对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆