Author: ilya_shaikovsky
Date: 2010-12-03 10:19:09 -0500 (Fri, 03 Dec 2010)
New Revision: 20358
Modified:
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemBean.java
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemNode.java
trunk/examples/richfaces-showcase/src/main/webapp/richfaces/treeAdaptors/samples/treeModelRecursiveAdaptor-sample.xhtml
Log:
treeAdaptors demo
Modified:
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemBean.java
===================================================================
---
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemBean.java 2010-12-03
14:42:06 UTC (rev 20357)
+++
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemBean.java 2010-12-03
15:19:09 UTC (rev 20358)
@@ -14,7 +14,7 @@
public synchronized List<FileSystemNode> getSourceRoots() {
if (srcRoots == null) {
- srcRoots = new FileSystemNode(SRC_PATH).getNodes();
+ srcRoots = new FileSystemNode(SRC_PATH).getDirectories();
}
return srcRoots;
Modified:
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemNode.java
===================================================================
---
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemNode.java 2010-12-03
14:42:06 UTC (rev 20357)
+++
trunk/examples/richfaces-showcase/src/main/java/org/richfaces/demo/tree/adaptors/FileSystemNode.java 2010-12-03
15:19:09 UTC (rev 20358)
@@ -1,17 +1,47 @@
package org.richfaces.demo.tree.adaptors;
+import static com.google.common.base.Predicates.containsPattern;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.Iterables.filter;
+import static com.google.common.collect.Iterables.transform;
+
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
public class FileSystemNode {
+
+ 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);
+ };
+ };
+
private String path;
- private List<FileSystemNode> children;
+ private List<FileSystemNode> directories;
+ private List<String> files;
+
private String shortPath;
public FileSystemNode(String path) {
@@ -23,27 +53,39 @@
shortPath = path;
}
}
+
+ public synchronized List<FileSystemNode> getDirectories() {
+ if (directories == null) {
+ directories = Lists.newArrayList();
- public synchronized List<FileSystemNode> getNodes() {
- if (children == null) {
- children = new ArrayList<FileSystemNode>();
- FacesContext facesContext = FacesContext.getCurrentInstance();
- ExternalContext externalContext = facesContext.getExternalContext();
- Set resourcePaths = externalContext.getResourcePaths(this.path);
- if (resourcePaths != null) {
- Object[] nodes = (Object[]) resourcePaths.toArray();
- for (Object node : nodes) {
- String nodePath = node.toString();
- if (nodePath.endsWith("/")) {
- nodePath = nodePath.substring(0, nodePath.length() - 1);
- }
- children.add(new FileSystemNode(nodePath));
- }
- }
+ Iterables.addAll(directories, transform(filter(getResourcePaths(),
containsPattern("/$")), FACTORY));
}
- return children;
+
+ 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() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ExternalContext externalContext = facesContext.getExternalContext();
+ Set<String> resourcePaths = externalContext.getResourcePaths(this.path);
+
+ if (resourcePaths == null) {
+ resourcePaths = Collections.emptySet();
+ }
+
+ return resourcePaths;
+ }
+
public String getShortPath() {
return shortPath;
}
Modified:
trunk/examples/richfaces-showcase/src/main/webapp/richfaces/treeAdaptors/samples/treeModelRecursiveAdaptor-sample.xhtml
===================================================================
---
trunk/examples/richfaces-showcase/src/main/webapp/richfaces/treeAdaptors/samples/treeModelRecursiveAdaptor-sample.xhtml 2010-12-03
14:42:06 UTC (rev 20357)
+++
trunk/examples/richfaces-showcase/src/main/webapp/richfaces/treeAdaptors/samples/treeModelRecursiveAdaptor-sample.xhtml 2010-12-03
15:19:09 UTC (rev 20358)
@@ -8,10 +8,13 @@
<h:form>
<rich:tree style="width:300px" toggleType="ajax"
var="item">
- <rich:treeModelRecursiveAdaptor
roots="#{fileSystemBean.sourceRoots}" nodes="#{item.nodes}" >
+ <rich:treeModelRecursiveAdaptor
roots="#{fileSystemBean.sourceRoots}" nodes="#{item.directories}"
>
<rich:treeNode>
#{item.shortPath}
</rich:treeNode>
+ <rich:treeModelAdaptor nodes="#{item.files}">
+ <rich:treeNode>#{item}</rich:treeNode>
+ </rich:treeModelAdaptor>
</rich:treeModelRecursiveAdaptor>
</rich:tree>
</h:form>
Show replies by date