-
1. Re: How to reset tree
burton999 Mar 30, 2012 12:22 AM (in response to burton999)I have solved the problem.
To reset the tree, I need to reset node selection and collapse all nodes.
Following is my code.
<!-- open popup -->
<a4j:commandLink action="#{import.show}" oncomplete="#{rich:component('importDialog')}.show();">
<h:outputText value="show popup" />
</a4j:commandLink>
<rich:popupPanel id="importDialog" modal="true" resizeable="false" autosized="true" style="margin: 0px 0px;padding: 0px 0px;" >
<f:facet name="header"><h:outputText value="#{msg['webui.dialog.import.title']}" /></f:facet>
<h:form>
<a4j:outputPanel ajaxRendered="true">
<rich:tree
var="node"
toggleType="ajax"
selectionType="ajax"
selection="#{import.selectedKey}"
toggleListener="#{import.processTreeToggle}"
selectionChangeListener="#{import.processTreeSelectionChange}"
nodeType="#{node.type}">
<rich:treeModelRecursiveAdaptor roots="#{import.rootDrives}" nodes="#{node.directories}" leaf="#{node.leaf}">
<rich:treeNode type="drive" expanded="#{node.expanded}" iconCollapsed="../images/16/drive.png" iconExpanded="../images/16/drive.png" iconLeaf="../images/16/drive.png">
<h:outputText value="#{node.name}"/>
</rich:treeNode>
<rich:treeNode type="directory" expanded="#{node.expanded}" iconCollapsed="../images/16/folder.png" iconExpanded="../images/16/folder.png" iconLeaf="../images/16/folder.png">
<h:outputText value="#{node.name}"/>
</rich:treeNode>
</rich:treeModelRecursiveAdaptor>
</rich:tree>
</a4j:outputPanel>
</h:form>
</rich:popupPanel>
@ManagedBean(name = "import")
@ViewScoped
public class ImportPage implements Serializable {
private List<FileSystemNode> driveNodes;
private FileSystemNode selectedNode;
private SequenceRowKey selectedKey;
public synchronized List<FileSystemNode> getRootDrives() {
if (this.driveNodes == null) {
driveNodes = new ArrayList<FileSystemNode>();
for (File file : File.listRoots()) {
driveNodes.add(new FileSystemNode(file, true));
}
}
return driveNodes;
}
public void processTreeSelectionChange(TreeSelectionChangeEvent event) {
List<Object> selection = new ArrayList<Object>(event.getNewSelection());
this.selectedKey = (SequenceRowKey)selection.get(0);
UITree tree = (UITree) event.getSource();
tree.setRowKey(this.selectedKey);
this.selectedNode = (FileSystemNode)tree.getRowData();
}
public void processTreeToggle(TreeToggleEvent event) {
UITree tree = (UITree) event.getSource();
FileSystemNode node = (FileSystemNode) tree.getRowData();
node.setExpanded(tree.isExpanded());
}
/**
* initialize tree
*/
public void show() {
this.selectedNode = null;
this.selectedKey = null;
for (FileSystemNode node : this.driveNodes) {
node.collapseAll();
}
}
public FileSystemNode getSelectedNode() {
return this.selectedNode;
}
public Collection<SequenceRowKey> getSelectedKey() {
List<SequenceRowKey> selection = new ArrayList<SequenceRowKey>();
if (this.selectedKey != null) {
selection.add(this.selectedKey);
}
return selection;
}
public static class FileSystemNode extends TreeNodeImpl {
private final File file;
private final boolean isDrive;
private boolean expanded;
private List<FileSystemNode> directories;
public FileSystemNode(File file, boolean isDrive) {
this.file = file;
this.isDrive = isDrive;
}
public String getType() {
if (this.isDrive) {
return "drive";
} else {
return "directory";
}
}
public String getName() {
if (this.isDrive) {
return this.file.getAbsolutePath();
} else {
return this.file.getName();
}
}
public File getFile() {
return this.file;
}
public synchronized List<FileSystemNode> getDirectories() {
if (this.directories == null) {
this.directories = new ArrayList<FileSystemNode>();
File[] children = this.file.listFiles(new DirectoryFilter());
if (children != null) {
for (File child : children) {
this.directories.add(new FileSystemNode(child, false));
}
}
}
return this.directories;
}
@Override
public boolean isLeaf() {
List<FileSystemNode> children = this.getDirectories();
if (children == null || children.size() == 0) {
return true;
}
return false;
}
public boolean isExpanded() {
return this.expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public void collapseAll() {
this.expanded = false;
if (this.directories != null) {
for (FileSystemNode child : this.directories) {
child.collapseAll();
}
}
}
}
public static class DirectoryFilter implements java.io.FileFilter {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
}
public static class FileFilter implements java.io.FileFilter {
@Override
public boolean accept(File file) {
return !file.isDirectory();
}
}
}