@Override
public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
final UITreeNode node = (UITreeNode) component;
final UITreeMenu tree = ComponentUtils.findAncestor(node, UITreeMenu.class);
final boolean folder = node.isFolder();
final String id = node.getClientId(facesContext);
final int level = node.getLevel();
final boolean root = level == 0;
final boolean expanded = folder && node.isExpanded() || level == 0;
final boolean showRoot = tree.isShowRoot();
final boolean ie6
= VariableResolverUtils.resolveClientProperties(facesContext).getUserAgent().equals(UserAgent.MSIE_6_0);
// XXX todo: find a better way to determine the parentId
final String clientId = node.getClientId(facesContext);
final int colon2 = clientId.lastIndexOf(":");
final int colon1 = clientId.substring(0, colon2 - 1).lastIndexOf(":");
final String structure = clientId.substring(colon1 + 1, colon2);
String parentStructure = getParentStructure(structure);
final String parentId = root ? null : clientId.substring(0, colon1 + 1) + parentStructure + clientId.substring(colon2);
final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
if (expanded) {
tree.getExpandedCache().add(structure);
}
if (showRoot || !root) {
writer.startElement(HtmlElements.DIV, null);
writer.writeIdAttribute(id);
writer.writeClassAttribute(Classes.create(node));
writer.writeAttribute(DataAttributes.TREEPARENT, parentId, false);
HtmlRendererUtils.writeDataAttributes(facesContext, writer, node);
if (!root) {
while (parentStructure != null) {
if (!tree.getExpandedCache().contains(parentStructure)) {
Style style = new Style();
style.setDisplay(Display.NONE);
writer.writeStyleAttribute(style);
break;
}