gpt4 book ai didi

java - 如何在树查看器中展开对象?

转载 作者:行者123 更新时间:2023-12-01 12:37:56 25 4
gpt4 key购买 nike

我正在尝试理解java TreeView 。我使用以下教程创建了一个树查看器:

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

谢谢。

最佳答案

您的getChildren方法似乎返回PackageCCID(也许还有其他?)对象的数组。

当树查看器想要显示这些对象的子对象时,它将调用 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'
}

关于java - 如何在树查看器中展开对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25404594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com