private static final Logger LOG = LoggerFactory.getLogger(MenuCommandRenderer.class);
@Override
public void encodeBegin(final FacesContext facesContext, final UIComponent component) throws IOException {
final UIMenuCommand menu = (UIMenuCommand) component;
final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
final boolean disabled = menu.isDisabled();
final boolean firstLevel = RendererTypes.MENU_BAR.equals(menu.getParent().getRendererType());
final LabelWithAccessKey label = new LabelWithAccessKey(menu);
if (menu.getFacet(Facets.CHECKBOX) != null) {
// checkbox menu
final UISelectBooleanCheckbox checkbox = (UISelectBooleanCheckbox) menu.getFacet(Facets.CHECKBOX);
final boolean checked = ComponentUtils.getBooleanAttribute(checkbox, Attributes.VALUE);
final String image = checked ? "image/MenuCheckmark.gif" : null;
final String hiddenId = checkbox.getClientId(facesContext);
final CommandMap map = new CommandMap(new Command());
encodeItem(facesContext, writer, menu, label, map, disabled, firstLevel, image, null, "selectBoolean");
encodeHidden(writer, hiddenId, checked);
} else if (menu.getFacet(Facets.RADIO) != null) {
// radio menu
final String clientId = menu.getClientId(facesContext);
final UISelectOne radio = (UISelectOne) menu.getFacet(Facets.RADIO);
final List<SelectItem> items = RenderUtils.getSelectItems(radio);
final String hiddenId = radio.getClientId(facesContext);
for (final SelectItem item : items) {
final boolean checked = item.getValue().equals(radio.getValue());
final String image = checked ? "image/MenuRadioChecked.gif" : null;
final String labelText = item.getLabel();
label.reset();
if (labelText != null) {
if (labelText.indexOf(LabelWithAccessKey.INDICATOR) > -1) {
label.setup(labelText);
} else {
label.setText(labelText);
}
} else {
LOG.warn("Menu item has label=null where clientId=" + clientId);
}
final String formattedValue = RenderUtils.getFormattedValue(facesContext, radio, item.getValue());
final CommandMap map = new CommandMap(
new Command(clientId, null, null, null, null, null, null, null, null, null));
encodeItem(facesContext, writer, null, label, map, disabled, firstLevel, image, formattedValue, "selectOne");
}
encodeHidden(writer, hiddenId, getCurrentValue(facesContext, radio));
} else {
// normal menu command
final String image = menu.getImage();
final CommandMap map = new CommandMap(new Command(facesContext, menu));
encodeItem(facesContext, writer, menu, label, map, disabled, firstLevel, image, null, null);
}
}