final Measure iconSmallHeight = resources.getThemeMeasure(facesContext, toolBar, "custom.icon-small-height");
final Measure iconBigWidth = resources.getThemeMeasure(facesContext, toolBar, "custom.icon-big-width");
final Measure iconSmallWidth = resources.getThemeMeasure(facesContext, toolBar, "custom.icon-small-width");
// label style
final Style labelStyle;
if (showLabel) {
labelStyle = new Style();
labelStyle.setLeft(paddingLeft);
labelStyle.setTop(paddingTop);
labelStyle.setWidth(RenderUtils.calculateStringWidth(facesContext, toolBar, label.getText()));
labelStyle.setHeight(resources.getThemeMeasure(facesContext, toolBar, "custom.label-height"));
} else {
labelStyle = null;
}
// button style
final Style buttonStyle = new Style();
buttonStyle.setLeft(Measure.ZERO);
buttonStyle.setTop(Measure.ZERO);
buttonStyle.setWidth(paddingLeft.add(paddingRight));
buttonStyle.setHeight(paddingBottom.add(paddingTop));
// icon style
final Style iconStyle;
if (showIcon) {
iconStyle = new Style();
iconStyle.setLeft(paddingLeft);
iconStyle.setTop(paddingTop);
iconStyle.setHeight(iconBig ? iconBigHeight : iconSmallHeight);
if (lackImage && showLabelRight && StringUtils.isNotBlank(label.getText())) {
iconStyle.setWidth(Measure.valueOf(1));
} else {
iconStyle.setWidth(iconBig ? iconBigWidth : iconSmallWidth);
}
if (showLabelBottom) {
labelStyle.setTop(labelStyle.getTop().add(iconStyle.getHeight()).add(paddingMiddle));
if (labelStyle.getWidth().lessThan(iconStyle.getWidth())) {
// label smaller than icon
labelStyle.setLeft(labelStyle.getLeft().add(iconStyle.getWidth().subtract(labelStyle.getWidth()).divide(2)));
buttonStyle.setWidth(buttonStyle.getWidth().add(iconStyle.getWidth()));
} else {
// label bigger than icon
iconStyle.setLeft(iconStyle.getLeft().add(labelStyle.getWidth().subtract(iconStyle.getWidth()).divide(2)));
buttonStyle.setWidth(buttonStyle.getWidth().add(labelStyle.getWidth()));
}
buttonStyle.setHeight(
buttonStyle.getHeight().add(iconStyle.getHeight()).add(paddingMiddle).add(labelStyle.getHeight()));
} else if (showLabelRight) {
labelStyle.setTop(labelStyle.getTop().add(iconStyle.getHeight().subtract(labelStyle.getHeight()).divide(2)));
labelStyle.setLeft(labelStyle.getLeft().add(iconStyle.getWidth()).add(paddingCenter));
buttonStyle.setWidth(
buttonStyle.getWidth().add(iconStyle.getWidth()).add(paddingCenter).add(labelStyle.getWidth()));
buttonStyle.setHeight(buttonStyle.getHeight().add(iconStyle.getHeight()));
} else {
buttonStyle.setWidth(buttonStyle.getWidth().add(iconStyle.getWidth()));
buttonStyle.setHeight(buttonStyle.getHeight().add(iconStyle.getHeight()));
}
} else {
iconStyle = null;
if (showLabel) {
// only label
buttonStyle.setWidth(buttonStyle.getWidth().add(labelStyle.getWidth()));
if (StringUtils.isBlank(label.getText())) {
buttonStyle.setWidth(buttonStyle.getWidth().add(iconSmallWidth));
}
buttonStyle.setHeight(buttonStyle.getHeight().add(labelStyle.getHeight()));
} else {
// both off: use some reasonable defaults
buttonStyle.setWidth(buttonStyle.getWidth().add(iconSmallWidth));
buttonStyle.setHeight(buttonStyle.getHeight().add(iconSmallWidth));
}
}
// opener style (for menu popup)
final Style openerStyle = new Style();
openerStyle.setWidth(resources.getThemeMeasure(facesContext, toolBar, "custom.opener-width"));
openerStyle.setHeight(resources.getThemeMeasure(facesContext, toolBar, "custom.opener-height"));
final Style menuStyle = new Style();
menuStyle.setLeft(buttonStyle.getWidth());
menuStyle.setTop(Measure.ZERO);
menuStyle.setWidth(paddingLeft.add(openerStyle.getWidth()).add(paddingRight));
menuStyle.setHeight(buttonStyle.getHeight());
// opener style (for menu popup)
openerStyle.setLeft(menuStyle.getWidth().subtract(openerStyle.getWidth()).divide(2));
openerStyle.setTop(menuStyle.getHeight().subtract(openerStyle.getHeight()).divide(2));
// item style
final Style itemStyle = new Style();
if (isRightAligned(toolBar)) { // overrides the default in the CSS file.
itemStyle.setLeft(resources.getThemeMeasure(facesContext, toolBar, "css.border-right-width"));
}
itemStyle.setWidth(
dropDownMenu != null ? buttonStyle.getWidth().add(menuStyle.getWidth()) : buttonStyle.getWidth());
itemStyle.setHeight(buttonStyle.getHeight());
// XXX hack
if (dropDownMenu != null && lackImage && !showLabel) {
itemStyle.setWidth(openerStyle.getWidth());
buttonStyle.setWidth(openerStyle.getWidth());
}
// change values when only have one button
if (dropDownMenu != null && !separateButtons && (!lackImage || StringUtils.isNotBlank(label.getText()))) {
openerStyle.setLeft(openerStyle.getLeft().add(buttonStyle.getWidth()));
buttonStyle.setWidth(buttonStyle.getWidth().add(menuStyle.getWidth()));
}
// start rendering
writer.startElement(HtmlElements.SPAN, command);
Markup itemMarkup = Markup.NULL;
if (selected) {
itemMarkup = itemMarkup.add(Markup.SELECTED);
}
if (disabled) {
itemMarkup = itemMarkup.add(Markup.DISABLED);
}
writer.writeClassAttribute(Classes.create(toolBar, "item", itemMarkup));
HtmlRendererUtils.renderTip(command, writer);
writer.writeStyleAttribute(itemStyle);
writer.startElement(HtmlElements.SPAN, command);
if (separateButtons || dropDownMenu == null) {
writer.writeClassAttribute(Classes.create(toolBar, "button", selected ? Markup.SELECTED : Markup.NULL));
} else {
writer.writeClassAttribute(Classes.create(toolBar, "menu"));
}
writer.writeStyleAttribute(buttonStyle);
if (!toolBar.isTransient()) {
writer.writeIdAttribute(command.getClientId(facesContext));
}
if (map != null) {
writer.writeAttribute(DataAttributes.COMMANDS, JsonUtils.encode(map), true);
}
HtmlRendererUtils.writeDataAttributes(facesContext, writer, command);
if (value != null) {
writer.writeAttribute(DataAttributes.VALUE, value, true);
}
// render icon
if (showIcon && iconName != null) {
writer.startElement(HtmlElements.IMG, command);
writer.writeAttribute(HtmlAttributes.SRC, image, false);
String imageHover
= ResourceManagerUtils.getImageWithPath(facesContext, HtmlRendererUtils.createSrc(iconName, "Hover"), true);
if (imageHover != null) {
writer.writeAttribute(DataAttributes.SRC_DEFAULT, image, false);
writer.writeAttribute(DataAttributes.SRC_HOVER, imageHover, false);
}
writer.writeAttribute(HtmlAttributes.ALT, label.getText(), true);
writer.writeStyleAttribute(iconStyle);
writer.endElement(HtmlElements.IMG);
}
// render label
if (showLabel) {
writer.startElement(HtmlElements.SPAN, command);
writer.writeClassAttribute(Classes.create(toolBar, "label"));
writer.writeStyleAttribute(labelStyle);
if (label.getText() != null) {
HtmlRendererUtils.writeLabelWithAccessKey(writer, label);
}
writer.endElement(HtmlElements.SPAN);
}
if (separateButtons) {
writer.endElement(HtmlElements.SPAN);
writer.startElement(HtmlElements.SPAN, command);
writer.writeClassAttribute(Classes.create(toolBar, "menu"));
writer.writeStyleAttribute(menuStyle);
// todo: span has not type: use data-tobago-type here (TOBAGO-1004)
writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.BUTTON, false);
}
// render sub menu popup button
if (dropDownMenu != null) {
writer.startElement(HtmlElements.IMG, command);
String menuImage = ResourceManagerUtils.getImageWithPath(facesContext, "image/toolbarButtonMenu.gif");
writer.writeAttribute(HtmlAttributes.SRC, menuImage, false);
writer.writeStyleAttribute(openerStyle);
writer.endElement(HtmlElements.IMG);
renderDropDownMenu(facesContext, writer, dropDownMenu);
}
writer.endElement(HtmlElements.SPAN);
writer.endElement(HtmlElements.SPAN);
return width.add(itemStyle.getWidth()).add(2); // XXX
// computation of the width of the toolBar will not be used in the moment.
}