0

我正在尝试了解 Java 树视图。我使用以下教程创建了一个 Treeviewer:

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

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

我想用另一个对象扩展书籍对象。

例如:

+ BookTitleX      BookAuthorX       etc
    otherobjectname     etc      etc  etc

如何将对象添加到对象?

非常感谢你

这是代码:

内容提供商:

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

}

这是带有输入的查看器:

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 到测试包。

谢谢。

4

1 回答 1

1

getChildren的方法似乎正在返回一个Package, CCID(也许还有其他?)对象的数组。

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

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'
}
于 2014-08-20T13:12:56.153 回答