import java.util.ArrayList;
import java.util.List;
import com.smartgwt.client.widgets.grid.events.FilterEditorSubmitEvent;
import com.smartgwt.client.widgets.grid.events.FilterEditorSubmitHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;
public class TreeGridExperiment extends TreeGrid{
private Tree tree;
private TreeNodeRecord[] records;
public TreeGridExperiment() {
tree = new Tree();
updateData();
setShowFilterEditor(true);
setFilterOnKeypress(false);
enableFiltering();
TreeGridField field = new TreeGridField(TreeNodeRecord.NODE_NAME);
field.setCanFilter(true);
setFields(field);
}
private void enableFiltering() {
addFilterEditorSubmitHandler(new FilterEditorSubmitHandler() {
@Override
public void onFilterEditorSubmit(FilterEditorSubmitEvent event) {
String inputValue = event.getCriteria().getAttribute(TreeNodeRecord.NODE_NAME);
if(inputValue == null){
setData(tree);
return;
}
setData(new Tree());
List
for (TreeNodeRecord record : records) {
if(record.contains(inputValue.toLowerCase(), tree)){
matchingNodes.add(record);
}
}
Tree newTree = new Tree();
newTree.setData(matchingNodes.toArray(new TreeNodeRecord[0]));
setData(newTree);
for (TreeNodeRecord treeNodeRecord : matchingNodes) {
newTree.openAll(treeNodeRecord);
}
}
});
}
public void updateData() {
records = new TreeNodeRecord[2];
for (int i = 0; i < 2; i++) {
records[i] = new TreeNodeRecord("Display " + i);
TreeNodeRecord[] leafs = new TreeNodeRecord[2];
for (int j = 0; j < 1; j++) {
leafs[j] = new TreeNodeRecord("Leaf" + j+i);
}
records[i].setChildren(leafs);
}
tree.setData(records);
this.setData(tree);
}
}
class TreeNodeRecord extends TreeNode {
public static String NODE_NAME = "nodeName";
public TreeNodeRecord(String attribute) {
setAttribute(NODE_NAME, attribute);
}
public boolean contains(String inputValue, Tree tree) {
if(getAttributeAsString(NODE_NAME).toLowerCase().contains(inputValue)) {
return true;
} else {
TreeNode[] childes = tree.getChildren(this);
for (TreeNode treeNode : childes) {
TreeNodeRecord record = (TreeNodeRecord)treeNode;
if(record.contains(inputValue, tree)){
return true;
}
}
}
return false;
}
}