}
@Override
public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
final UITreeNode node = (UITreeNode) component;
final AbstractUITree tree = ComponentUtils.findAncestor(node, AbstractUITree.class);
final boolean folder = node.isFolder();
final String id = node.getClientId(facesContext);
final int level = node.getLevel();
final boolean root = level == 0;
final boolean showRoot = ((UITree) tree).isShowRoot();
// if the root is hidden, the root node must be expanded (otherwise you will see nothing)
final boolean expanded = folder && node.isExpanded() || !showRoot && root;
// XXX todo: find a better way to determine the parentId
final String clientId = node.getClientId(facesContext);
final int colon = clientId.lastIndexOf(":");
final int underscore = clientId.substring(0, colon).lastIndexOf("_");
final String parentId = root ? null : clientId.substring(0, underscore) + clientId.substring(colon);
final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
if (expanded) {
tree.getExpandedCache().add(id);
}
if (showRoot || !root) {
writer.startElement(HtmlElements.DIV, null);
// div id
writer.writeIdAttribute(id);
if (!folder) {
HtmlRendererUtils.renderDojoDndItem(node, writer, true);
}
writer.writeClassAttribute(Classes.create(node));
writer.writeAttribute(DataAttributes.TREEPARENT, parentId, false);
if (!root && !tree.getExpandedCache().contains(parentId)) {
Style style = new Style();
style.setDisplay(Display.NONE);
writer.writeStyleAttribute(style);
}
// div style (width)
Style style = new Style(facesContext, tree);
String widthString;
if (style.getWidth() != null) {
widthString = "width: " + Integer.toString(style.getWidth().getPixel() - 22); // fixme: 4 + 18 for scrollbar
} else {
widthString = "100%";
}
writer.writeStyleAttribute(widthString);
if (folder) {
encodeExpandedHidden(writer, node, id, expanded);
}
for (UIComponent child : (List<UIComponent>) node.getChildren()) {
// encode all content but not the nodes and data.
if (!(child instanceof UITreeNode) && !(child instanceof UITreeData)) {
RenderUtils.encode(facesContext, child);
}
}