-
1. Re: Richfaces 4 - expand some tree node
nbelaevski Jul 27, 2011 1:47 PM (in response to andrei.a)Hi Andrei,
Sorry, but not getting your point. Can you please clarify why do you need 15 different types of nodes?
-
2. Re: Richfaces 4 - expand some tree node
chris2209 Mar 30, 2012 11:49 AM (in response to nbelaevski)This example shows how you can collapse/expand the node with all their subnodes with a doubleclick.
In your xhtml page:
<rich:tree id="tree" var="node" toggleType="ajax"
value="#{backingTreeBean.rootNodes}" nodeType="#{node.type}"
selectionType="ajax"
selectionChangeListener="#{backingTreeBean.selectionChanged()}">
<rich:treeNode id="treeNode" type="#{node.type}" ondblclick="toggleAllNodes()" expanded="#{node.expanded}">
...
<a4j:jsFunction name="toggleAllNodes" actionListener="#{backingTreeBean.expandCollapseNodes}" render="tree" execute="@this" />
</rich:treeNode>
</rich:tree>
public class Project implements TreeNode {
private boolean expanded;
...
}
public class ComponentObject implements TreeNode {
private boolean expanded;
...
}
In BackingBean:
public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent) {
// considering only single selection
List<Object> selection = new ArrayList<Object>(
selectionChangeEvent.getNewSelection());
if (selection == null || selection.isEmpty()) {
return;
}
Object currentSelectionKey = selection.get(0);
UITree tree = (UITree) selectionChangeEvent.getSource();
Object storedKey = tree.getRowKey();
tree.setRowKey(currentSelectionKey);
currentSelection = (TreeNode) tree.getRowData();
tree.setRowKey(storedKey);
}
public void expandCollapseNodes(ActionEvent event) {
UIFunction uif = (UIFunction) event.getSource();
UITreeNode treeNode = (UITreeNode) uif.getParent();
UITree uiTree = (UITree) treeNode.getParent();
Collection<Object> selection = uiTree.getSelection();
if (selection != null && !selection.isEmpty()) {
Object[] sel = selection.toArray();
if (sel == null || sel.length == 0 || sel[0] == null) {
return;
}
SequenceRowKey srKey = (SequenceRowKey) sel[0];
uiTree.setRowKey(srKey);
TreeNode node = (TreeNode) uiTree.getRowData();
if (node == null) {
return;
}
boolean expand = false;
if (node instanceof Project) {
expand = !((Project)node).isExpanded();
}
if (node instanceof ComponentObject) {
expand = !((ComponentObject)node).isExpanded();
}
expandCollapse(node, expand);
}
}
private void expandCollapse(TreeNode node, boolean expand) {
if (node instanceof Project) {
Project p = (Project) node;
p.setExpanded(expand);
}
if (node instanceof ComponentObject) {
ComponentObject co = (ComponentObject) node;
co.setExpanded(expand);
}
if (node instanceof Asset) {
Asset a = (Asset) node;
return;
}
if (node.children() != null && node.children().hasMoreElements()) {
Enumeration childs = node.children();
while (childs.hasMoreElements()) {
expandCollapse((TreeNode) childs.nextElement(), expand);
}
}
}
-
3. Re: Richfaces 4 - expand some tree node
przemeck Apr 18, 2015 9:15 PM (in response to andrei.a)For each node type, you can provide relevant node class. Each of these classes can be derived from let's say:
public class YourNodeA extends BaseTreeNode {}
public class YourNodeB extends BaseTreeNode {}
(..)
public class BaseTreeNode extends TreeNodeImpl implements Serializable {
private boolean expanded;
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
That's how you bind your
<rich:tree ... >
<rich:treeNode type="a" expanded="#{node.expanded}".../>
<rich:treeNode type="b" expanded="#{node.expanded}".../>
(..)
</rich:tree>
(1) Next, if You want some paths beeing expanded on init, you can just configure when constructing tree nodes (passed later to <rich:tree value="treeBean.rootNodes">) by calling setExpanded(expanded) e.g. in their constructor.
(2) If you want to expand / collapse some path ( let's say SequenceRowKey["key1", "key2"] ) later, on demand:
BaseNode parent = (BaseNode) rootNode.getChild("key1");
parent.setExpanded(true);
BaseNode node = (BaseNode) parent.getChild("key2");
node.setExpanded(true);
If You need working demo example of node's collapsing / expanding and selecting pogramatically please read my post:
Web and mobile developer's site: Interactions with RichTree
There are: description, deployed app and GitHub repo.
I used RichFaces 4.5.4