DataTable as children in a rich:tree
cavaignac May 20, 2008 5:24 AMHi,
I would to display DataTable components as children in a richfaces tree instead of a simple string.
Here is my code for the moment.
But instead of displaying the dataTable, it displays the reference like "org.richfaces.component.html.HtmlDataTable@92a094"
My JSP:
<rich:tree style="width:300px" nodeSelectListener="#{SimpleTreeBean.processSelection}"
reRender="selectedNode" ajaxSubmitSelection="true" switchType="client" icon="../../commun/Images/24.gif"
iconCollapsed="../../commun/Images/iPlus.gif"
iconExpanded="../../commun/Images/iMoins.gif"
value="#{SimpleTreeBean.treeNode}" var="item">
<rich:treeNode type="simpleNode" >
<rich:dataTable value="#{item.hdt}" />
</rich:treeNode>
</rich:tree>
The class describing the tree:
/**
* License Agreement.
*
* JBoss RichFaces - Ajax4jsf Component Library
*
* Copyright (C) 2007 Exadel, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package uai.bbeans;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.richfaces.component.TreeNode;
import org.richfaces.component.TreeNodeImpl;
import org.richfaces.component.UITree;
import org.richfaces.component.events.NodeSelectedEvent;
import org.richfaces.component.html.HtmlDataTable;
public class SimpleTreeBean {
private TreeNode rootNode = null;
private String nodeTitle;
private HtmlDataTable hdt;
private static final String DATA_PATH = "../web/jsp/uai/simple-tree-data.properties";
private void addNodes(String path, TreeNode node, Properties properties) {
boolean end = false;
int counter = 1;
while (!end) {
String key = path != null ? path + '.' + counter : String.valueOf(counter);
String value = properties.getProperty(key);
hdt = new HtmlDataTable();
if (value != null) {
TreeNodeImpl nodeImpl = new TreeNodeImpl();
// HtmlDataTable hdt = nodeImpl.getHdt();
// hdt.setBorder("3");
// nodeImpl.setHdt(hdt);
hdt.setBorder("3");
nodeImpl.setData(hdt);
node.addChild(new Integer(counter), nodeImpl);
addNodes(key, nodeImpl, properties);
counter++;
} else {
end = true;
}
}
}
private void loadTree() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);
try {
Properties properties = new Properties();
properties.load(dataStream);
rootNode = new TreeNodeImpl();
addNodes(null, rootNode, properties);
} catch (IOException e) {
throw new FacesException(e.getMessage(), e);
} finally {
if (dataStream != null) {
try {
dataStream.close();
} catch (IOException e) {
externalContext.log(e.getMessage(), e);
}
}
}
}
public TreeNode getTreeNode() {
if (rootNode == null) {
loadTree();
}
return rootNode;
}
public void processSelection(NodeSelectedEvent event) {
UITree tree = (UITree) event.getComponent();
nodeTitle = (String) tree.getRowData();
}
public String getNodeTitle() {
return nodeTitle;
}
public void setNodeTitle(String nodeTitle) {
this.nodeTitle = nodeTitle;
}
}
My properties file:
1=Daniel Defo
1.1=Robinson Crusoe
1.1.1=Start In Life
1.1.2=Slavery And Escape
1.1.3=Wrecked On A Desert Island
1.1.4=First Weeks On The Island
1.1.5=Builds A House - The Journal
1.1.6=Ill And Conscience-Stricken
1.1.7=Agricultural Experience
1.1.8=Surveys His Position
1.1.9=A Boat
1.1.10=Tames Goats
1.1.11=Finds Print Of Man's Foot On The Sand
1.1.12=A Cave Retreat
1.1.13=Wreck Of A Spanish Ship
1.1.14=A Dream Realised
1.1.15=Friday's Education