Search in sources :

Example 1 with ImagePosition

use of com.github.bordertech.wcomponents.WButton.ImagePosition in project wcomponents by BorderTech.

the class WButtonRenderer method doRender.

/**
 * Paints the given WButton.
 *
 * @param component the WButton to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    XmlStringBuilder xml = renderContext.getWriter();
    WButton button = (WButton) component;
    String text = button.getText();
    String imageUrl = button.getImageUrl();
    String accessibleText = button.getAccessibleText();
    String toolTip = button.getToolTip();
    if (Util.empty(text) && imageUrl == null && Util.empty(accessibleText) && Util.empty(toolTip)) {
        throw new SystemException("WButton text or imageUrl must be specified");
    }
    xml.appendTagOpen(getTagName(button));
    String buttonId = button.getId();
    ImagePosition pos = button.getImagePosition();
    if (Util.empty(text) && Util.empty(toolTip) && Util.empty(accessibleText)) {
        // If the button has an imageUrl but no text equivalent get the text equivalent off of the image
        WImage imgHolder = button.getImageHolder();
        if (null != imgHolder) {
            toolTip = imgHolder.getAlternativeText();
        }
    }
    xml.appendAttribute("id", buttonId);
    xml.appendAttribute("name", buttonId);
    xml.appendAttribute("value", "x");
    xml.appendAttribute("type", getButtonType(button));
    xml.appendAttribute("class", geHtmlClassName(button));
    xml.appendOptionalAttribute("disabled", button.isDisabled(), "disabled");
    xml.appendOptionalAttribute("hidden", button.isHidden(), "hidden");
    xml.appendOptionalAttribute("title", toolTip);
    xml.appendOptionalAttribute("aria-label", accessibleText);
    xml.appendOptionalAttribute("aria-haspopup", button.isPopupTrigger(), "true");
    xml.appendOptionalAttribute("accesskey", Util.upperCase(button.getAccessKeyAsString()));
    xml.appendOptionalAttribute("data-wc-btnmsg", button.getMessage());
    if (button.isCancel()) {
        xml.appendAttribute("formnovalidate", "formnovalidate");
    } else {
        Action action = button.getAction();
        if (action instanceof ValidatingAction) {
            WComponent validationTarget = ((ValidatingAction) action).getComponentToValidate();
            xml.appendAttribute("data-wc-validate", validationTarget.getId());
        }
    }
    xml.appendClose();
    if (imageUrl != null) {
        xml.appendTagOpen("span");
        String imageHolderClass = "wc_nti";
        if (pos != null) {
            StringBuffer imageHolderClassBuffer = new StringBuffer("wc_btn_img wc_btn_img");
            switch(pos) {
                case NORTH:
                    imageHolderClassBuffer.append("n");
                    break;
                case EAST:
                    imageHolderClassBuffer.append("e");
                    break;
                case SOUTH:
                    imageHolderClassBuffer.append("s");
                    break;
                case WEST:
                    imageHolderClassBuffer.append("w");
                    break;
                default:
                    throw new SystemException("Unknown image position: " + pos);
            }
            imageHolderClass = imageHolderClassBuffer.toString();
        }
        xml.appendAttribute("class", imageHolderClass);
        xml.appendClose();
        if (pos != null && text != null) {
            xml.appendTag("span");
            xml.appendEscaped(text);
            xml.appendEndTag("span");
        }
        xml.appendTagOpen("img");
        xml.appendUrlAttribute("src", imageUrl);
        String alternateText = pos == null ? text : "";
        xml.appendAttribute("alt", alternateText);
        xml.appendEnd();
        xml.appendEndTag("span");
    } else if (text != null) {
        xml.appendEscaped(text);
    }
    xml.appendEndTag(getTagName(button));
    if (button.isAjax()) {
        paintAjax(button, xml);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) ValidatingAction(com.github.bordertech.wcomponents.validation.ValidatingAction) Action(com.github.bordertech.wcomponents.Action) SystemException(com.github.bordertech.wcomponents.util.SystemException) ImagePosition(com.github.bordertech.wcomponents.WButton.ImagePosition) ValidatingAction(com.github.bordertech.wcomponents.validation.ValidatingAction) WImage(com.github.bordertech.wcomponents.WImage) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) WButton(com.github.bordertech.wcomponents.WButton)

Aggregations

Action (com.github.bordertech.wcomponents.Action)1 WButton (com.github.bordertech.wcomponents.WButton)1 ImagePosition (com.github.bordertech.wcomponents.WButton.ImagePosition)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 WImage (com.github.bordertech.wcomponents.WImage)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1 ValidatingAction (com.github.bordertech.wcomponents.validation.ValidatingAction)1