tapestry提供了樹狀組件tree,源碼以下:html
TreeBrowse.javajava
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.corelib.components.Tree;
import org.apache.tapestry5.tree.DefaultTreeModel;
import org.apache.tapestry5.tree.TreeModel;
import com.tapestry.app.entities.Stuff;
import com.tapestry.app.services.StuffTreeModelAdapter;
public class TreeBrowse {
// Screen fields
private TreeModel<Stuff> stuffModel;
// Generally useful bits and pieces
@InjectComponent
private Tree tree;
// The code
void onClearExpansions() {
tree.clearExpansions();
}
// Getters and setters
public TreeModel<Stuff> getStuffModel() {
if (stuffModel == null) {
ValueEncoder<Stuff> stuffEncoder = new ValueEncoder<Stuff>() {
@Override
public String toClient(Stuff stuff) {
return stuff.uuid;
}
@Override
public Stuff toValue(String uuid) {
return Stuff.ROOT.searchSubTree(uuid);
}
};
stuffModel = new DefaultTreeModel<Stuff>(stuffEncoder, new StuffTreeModelAdapter(), Stuff.ROOT.children);
}
return stuffModel;
}
}
TreeBrowse.tmlsql
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<div style="margin: 20px;">
<a t:type="eventlink" t:event="clearExpansions">Clear Expansions</a><br/><br/>
<t:tree t:id="tree" model="stuffModel"/><br/>
<a t:type="pagelink" t:page="TreeBrowse" href="#">Refresh</a>
</div>
</html>
Stuff.javaapache
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.entities;
import java.util.List;
import java.util.UUID;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
public class Stuff {
public static final Stuff ROOT = new Stuff("<root>");
static {
ROOT.addChild(new Stuff("Pets").addChildrenNamed("Oscar", "Gromit", "Max", "Roger", "Cooper"));
ROOT.addChild(new Stuff("Games").addChild(
new Stuff("Board Games").addChildrenNamed("Settlers of Catan", "Agricola", "Ra", "Risk", "Dvonn"))
.addChild(new Stuff("Card Games").addChildrenNamed("Magic the Gathering", "Dominion", "Mu")));
Stuff numbers = new Stuff("Numbers");
for (int i = 0; i < 100; i++) {
numbers.addChild(new Stuff(Integer.toString(i)));
}
ROOT.addChild(numbers);
}
public final String uuid = UUID.randomUUID().toString();
public final String name;
public List<Stuff> children;
public Stuff(String name) {
this.name = name;
}
public Stuff addChild(Stuff child) {
if (children == null) {
children = CollectionFactory.newList();
}
children.add(child);
return this;
}
public Stuff addChildrenNamed(String... names) {
for (String name : names) {
addChild(new Stuff(name));
}
return this;
}
public Stuff searchSubTree(String uuid) {
if (this.uuid.equals(uuid)) {
return this;
}
for (Stuff child : children) {
Stuff match = child.searchSubTree(uuid);
if (match != null) {
return match;
}
}
return null;
}
}
StuffTreeModelAdapter.javaapp
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.services;
import java.util.List;
import org.apache.tapestry5.tree.TreeModelAdapter;
import com.tapestry.app.entities.Stuff;
public class StuffTreeModelAdapter implements TreeModelAdapter<Stuff> {
@Override
public boolean isLeaf(Stuff stuff) {
return !hasChildren(stuff);
}
@Override
public boolean hasChildren(Stuff stuff) {
return stuff.children != null && !stuff.children.isEmpty();
}
@Override
public List<Stuff> getChildren(Stuff stuff) {
return stuff.children;
}
@Override
public String getLabel(Stuff stuff) {
return stuff.name;
}
}
http://localhost/tree/browse