JSF 2.2 Rich:Tree & h:selectBooleanCheckbox, how to link both of them together?
zhefrench Mar 4, 2014 5:36 AMI'm stuck with a conception problem. I try to parse a folder to get all this directory and files. I want then to select some files using a checkbox to associate to a key reference.(to retrieve them by this key after in the backingBean to set my objects and to populate my database) A key could have several files associated. I don't see how to do it. I'll be great if i could have some help on this.
I don't understand how to retrieve the data from the view in a backing bean because of the complex structure
I write this for the jsf view
<rich:tree id="tree" toggleType="client" selectionType="ajax" var="item" rowKeyVar="rowKeyVar" treeNodeVar="treeNodeVar">
<rich:treeModelRecursiveAdaptor roots="#{fileSystemBean.sourceRoots}" nodes="#{item.directories}" rowKeyVar="rowKeyVar1" treeNodeVar="treeNodeVar1" >
<rich:treeNode>#{item.shortPath} > </rich:treeNode>
<rich:treeModelAdaptor nodes="#{item.files}">
<rich:treeNode>
#{item} <h:selectBooleanCheckbox id="selectAll"/>
</rich:treeNode>
</rich:treeModelAdaptor>
</rich:treeModelRecursiveAdaptor>
</rich:tree>
I created this backing bean :
public class FileSystemNode {
private String path;
private String shortPath;
private List<String> files;
private List<FileSystemNode> directories;
private static final Function<String, FileSystemNode> FACTORY = new Function<String, FileSystemNode>() {
public FileSystemNode apply(String from) {
return new FileSystemNode(from.substring(0, from.length() - 1));
};
};
private static final Function<String, String> TO_SHORT_PATH = new Function<String, String>() {
public String apply(String from) {
int idx = from.lastIndexOf('/');
if (idx < 0) {
return from;
}
return from.substring(idx + 1);
};
};
/************************************************************************************************/
/********************************** Constructor **************************************/
/************************************************************************************************/
public FileSystemNode(String path) {
this.path = path;
int idx = path.lastIndexOf('/');
if (idx != -1) {
shortPath = path.substring(idx + 1);
} else {
shortPath = path;
}
}
/************************************************************************************************/
/********************************** Methods ****************************************************/
/************************************************************************************************/
public synchronized List<FileSystemNode> getDirectories() {
if (directories == null) {
directories = Lists.newArrayList();
Iterables.addAll(directories, transform(filter(getResourcePaths(), containsPattern("/$")), FACTORY));
}
return directories;
}
public synchronized List<String> getFiles() {
if (files == null) {
files = new ArrayList<String>();
Iterables.addAll(files, transform(filter(getResourcePaths(), not(containsPattern("/$"))), TO_SHORT_PATH));
}
return files;
}
private Iterable<String> getResourcePaths() {
Set<String> pathsStringified = new HashSet<String>();
File folder = new File(this.path);
for (File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
String newString = fileEntry.getPath();
pathsStringified.add(newString+"/");
} else {
String newString = fileEntry.getPath();
pathsStringified.add(newString);
}
}
return pathsStringified;
}
/************************************************************************************************/
/********************************** Getters and Setters ****************************************/
/************************************************************************************************/
public String getShortPath() {
return shortPath;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public static Function<String, FileSystemNode> getFactory() {
return FACTORY;
}
public static Function<String, String> getToShortPath() {
return TO_SHORT_PATH;
}
public void setDirectories(List<FileSystemNode> directories) {
this.directories = directories;
}
public void setFiles(List<String> files) {
this.files = files;
}
public void setShortPath(String shortPath) {
this.shortPath = shortPath;
}
}