How to expand an object in a Treeviewer?

笑着哭i 提交于 2019-12-12 04:47:01

问题


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.

For example:

+ BookTitleX      BookAuthorX       etc
    otherobjectname     etc      etc  etc

How can I add an object to an object ?

Thank u so much

This is the code:

Content Provider:

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;
}

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

Thanks.


回答1:


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

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'
}


来源:https://stackoverflow.com/questions/25404594/how-to-expand-an-object-in-a-treeviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!