protected void encodeScript(FacesContext facesContext, Tree tree) throws IOException {
ResponseWriter writer = facesContext.getResponseWriter();
String clientId = tree.getClientId(facesContext);
String formClientId = null;
String treeVar = createUniqueWidgetVar(facesContext, tree);
TreeNode root = (TreeNode) tree.getValue();
UIComponent parentForm = ComponentUtils.findParentForm(facesContext, tree);
if(parentForm != null)
formClientId = parentForm.getClientId(facesContext);
else
throw new FacesException("Tree:" + clientId + " needs to be enclosed in a form");
writer.startElement("script", null);
writer.writeAttribute("type", "text/javascript", null);
//Nodes
writer.write(treeVar + " = new PrimeFaces.widget.TreeView('" + clientId + "', [\n");
if(root != null) {
int rowIndex = 0;
for(Iterator<TreeNode> iterator = root.getChildren().iterator(); iterator.hasNext();) {
encodeTreeNode(facesContext, tree, iterator.next(), String.valueOf(rowIndex));
rowIndex ++;
if(iterator.hasNext())
writer.write(",");