A bigger example for the cookbook - Part 1
fmarwede May 8, 2008 8:24 AMHi folks!
Concerning my suggestion in http://www.jboss.com/index.html?module=bb&op=viewtopic&t=134778
I wanna create a bigger example combining a few RF components.
I'll try to implement a small user management with users and user groups displayed in a tree, a scrollable datatable for users not yet assigned and a details pane where you can manage some attributes of users and groups.
This example will be published bit by bit here: http://wiki.jboss.org/wiki/RichFacesCookbook
I start with the built-in drag & drop of the tree because there are some forum questions about that and I couldn't find an accurate solution.
I'm using RF 3.2.0 SR1, Facelets, Tomcat 5.5.
Here the code I have for now:
<h:form id="treeForm"> <rich:dragIndicator id="treeDragIndicator" /> <a4j:outputPanel id="userTreeWrapper"> <rich:tree dragIndicator=":treeForm:treeDragIndicator" dropListener="#{appBean.processTreeDrop}" style="width:300px" value="#{appBean.userGroupTree}" var="item" nodeFace="#{item.type}" ajaxSubmitSelection="true"> <rich:treeNode type="userGroup" dragType="userGroup" acceptedTypes="userGroup, user" reRender="userTreeWrapper"> <h:outputText value="#{item.name}" /> <rich:dndParam name="label" type="drag" value="userGroup: #{item.name}" /> </rich:treeNode> <rich:treeNode type="user" dragType="user" reRender="userTreeWrapper"> <h:outputText value="#{item.name}" /> <rich:dndParam name="label" type="drag" value="userGroup: #{item.name}" /> </rich:treeNode> </rich:tree> </a4j:outputPanel> </h:form>
Bean code:
public class AppBean { private TreeNode userGroupTree; public AppBean() { userGroupTree = new TreeNodeImpl(); UserGroupTreeNode group1 = new UserGroupTreeNode("Group 1"); userGroupTree.addChild("Group 1", group1); group1.setParent(userGroupTree); UserGroupTreeNode group2 = new UserGroupTreeNode("Group 2"); userGroupTree.addChild("Group 2", group2); group2.setParent(userGroupTree); UserGroupTreeNode group3 = new UserGroupTreeNode("Group 3"); userGroupTree.addChild("Group 3", group3); group3.setParent(userGroupTree); UserTreeNode user1 = new UserTreeNode("User 1"); group1.addChild("User 1", user1); user1.setParent(group1); UserTreeNode user2 = new UserTreeNode("User 2"); group2.addChild("User 2", user2); user2.setParent(group2); UserTreeNode user3 = new UserTreeNode("User 3"); group3.addChild("User 3", user3); user3.setParent(group3); } public void processTreeDrop(DropEvent event) { System.out.println("Start Drop Processing"); TreeNode targetGroup = getLeaf(event.getDropValue().toString()); TreeNode sourceGroup = getLeafParent(event.getDragValue().toString()); AbstractTreeNode ddItem = (AbstractTreeNode) getLeaf(event.getDragValue().toString()); sourceGroup.removeChild(ddItem.getName()); targetGroup.addChild(ddItem.getName(), ddItem); ddItem.setParent(targetGroup); System.out.println("Drop complete"); } private TreeNode getLeaf(String id) { StringTokenizer tokenizer = new StringTokenizer(id, ":"); TreeNode result = userGroupTree; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); System.out.println("Token: " + token); result = result.getChild(token); } if (result.equals(userGroupTree)) { throw new IllegalArgumentException("Node not found"); } else { return result; } } private TreeNode getLeafParent(String id) { return getLeaf(id).getParent(); } public TreeNode getUserGroupTree() { return userGroupTree; } public void setUserGroupTree(TreeNode userGroupTree) { this.userGroupTree = userGroupTree; } }
The node classes
public abstract class AbstractTreeNode extends TreeNodeImpl { @Override public Object getData() { // This method has to be overriden like this. // TODO Analyze why and how to get a more reasoned solution. // Update (14.02.08) Nick Belaevski said, it is a bug // Forum: http://www.jboss.org/index.html?module=bb&op=viewtopic&t=129597 // JIRA: http://jira.jboss.com/jira/browse/RF-2227 return this; } public abstract String getType(); public abstract String getName(); } public class UserTreeNode extends AbstractTreeNode { private String name; public UserTreeNode(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String getType() { return "user"; } } public class UserGroupTreeNode extends AbstractTreeNode { private String name; public UserGroupTreeNode(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String getType() { // TODO Auto-generated method stub return "userGroup"; } @Override public String toString() { return "UserGroupTreeNode(" + name + ")"; } }