Search in sources :

Example 1 with WLink

use of com.github.bordertech.wcomponents.WLink in project wcomponents by BorderTech.

the class WLinkRenderer method doRender.

/**
 * Paints the given {@link WLink}.
 *
 * @param component the WLink to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WLink link = (WLink) component;
    XmlStringBuilder xml = renderContext.getWriter();
    String text = link.getText();
    String targetWindowName = link.getOpenNewWindow() ? link.getTargetWindowName() : null;
    String imageUrl = link.getImageUrl();
    xml.appendTagOpen("ui:link");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("disabled", link.isDisabled(), "true");
    xml.appendOptionalAttribute("hidden", component.isHidden(), "true");
    xml.appendOptionalAttribute("toolTip", link.getToolTip());
    xml.appendOptionalAttribute("accessibleText", link.getAccessibleText());
    xml.appendUrlAttribute("url", link.getUrl());
    xml.appendOptionalAttribute("rel", link.getRel());
    xml.appendOptionalAttribute("accessKey", Util.upperCase(link.getAccessKeyAsString()));
    if (imageUrl != null) {
        xml.appendUrlAttribute("imageUrl", imageUrl);
        ImagePosition imagePosition = link.getImagePosition();
        if (imagePosition != null) {
            switch(imagePosition) {
                case NORTH:
                    xml.appendAttribute("imagePosition", "n");
                    break;
                case EAST:
                    xml.appendAttribute("imagePosition", "e");
                    break;
                case SOUTH:
                    xml.appendAttribute("imagePosition", "s");
                    break;
                case WEST:
                    xml.appendAttribute("imagePosition", "w");
                    break;
                default:
                    throw new SystemException("Unknown image position: " + imagePosition);
            }
        }
        // we have an image. We must have a text equivalent
        if (Util.empty(text) && Util.empty(link.getToolTip()) && Util.empty(link.getAccessibleText())) {
            // If the link has an umageUrl but no text equivalent get the text equivalent off of the image
            WImage linkImage = link.getImageHolder();
            if (null != linkImage) {
                xml.appendOptionalAttribute("toolTip", linkImage.getAlternativeText());
            }
        }
    }
    if (link.isRenderAsButton()) {
        xml.appendAttribute("type", "button");
    }
    xml.appendClose();
    if (targetWindowName != null) {
        xml.appendTagOpen("ui:windowAttributes");
        xml.appendAttribute("name", targetWindowName);
        WLink.WindowAttributes attributes = link.getWindowAttrs();
        if (attributes != null) {
            xml.appendOptionalAttribute("top", attributes.getTop() >= 0, attributes.getTop());
            xml.appendOptionalAttribute("left", attributes.getLeft() >= 0, attributes.getLeft());
            xml.appendOptionalAttribute("width", attributes.getWidth() > 0, attributes.getWidth());
            xml.appendOptionalAttribute("height", attributes.getHeight() > 0, attributes.getHeight());
            xml.appendOptionalAttribute("resizable", attributes.isResizable(), "true");
            xml.appendOptionalAttribute("showMenubar", attributes.isMenubar(), "true");
            xml.appendOptionalAttribute("showToolbar", attributes.isToolbars(), "true");
            xml.appendOptionalAttribute("showLocation", attributes.isLocation(), "true");
            xml.appendOptionalAttribute("showStatus", attributes.isStatus(), "true");
            xml.appendOptionalAttribute("showScrollbars", attributes.isScrollbars(), "true");
            xml.appendOptionalAttribute("showDirectories", attributes.isDirectories(), "true");
        }
        xml.appendEnd();
    }
    if (text != null) {
        xml.appendEscaped(text);
    }
    xml.appendEndTag("ui:link");
    // Paint the AJAX trigger if the link has an action
    if (link.getAction() != null) {
        paintAjaxTrigger(link, xml);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) ImagePosition(com.github.bordertech.wcomponents.WLink.ImagePosition) WImage(com.github.bordertech.wcomponents.WImage) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) WLink(com.github.bordertech.wcomponents.WLink)

Example 2 with WLink

use of com.github.bordertech.wcomponents.WLink in project wcomponents by BorderTech.

the class WLinkRenderer_Test method testRendererCorrectlyConfigured.

@Test
public void testRendererCorrectlyConfigured() {
    WLink component = new WLink();
    Assert.assertTrue("Incorrect renderer supplied", getWebXmlRenderer(component) instanceof WLinkRenderer);
}
Also used : WLink(com.github.bordertech.wcomponents.WLink) Test(org.junit.Test)

Example 3 with WLink

use of com.github.bordertech.wcomponents.WLink in project wcomponents by BorderTech.

the class WLinkRenderer_Test method testButtonImageToolTipRender.

@Test
public void testButtonImageToolTipRender() throws IOException, SAXException, XpathException {
    WLink link = new WLink();
    String expected = "alt text";
    WImage buttonImage = new WImage("http://localhost/image.png", expected);
    link.setImage(buttonImage.getImage());
    assertXpathEvaluatesTo(expected, "//ui:link/@toolTip", link);
}
Also used : WImage(com.github.bordertech.wcomponents.WImage) WLink(com.github.bordertech.wcomponents.WLink) Test(org.junit.Test)

Example 4 with WLink

use of com.github.bordertech.wcomponents.WLink in project wcomponents by BorderTech.

the class WLinkRenderer_Test method testWindowSizeAttributes.

@Test
public void testWindowSizeAttributes() throws IOException, SAXException, XpathException {
    WLink link = new WLink.Builder(TEXT, LINK_URL).build();
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(TEXT, "normalize-space(//ui:link)", link);
    assertXpathUrlEvaluatesTo(LINK_URL, "//ui:link/@url", link);
    assertXpathNotExists("//ui:link/ui:windowAttributes/@top", link);
    assertXpathNotExists("//ui:link/ui:windowAttributes/@left", link);
    assertXpathNotExists("//ui:link/ui:windowAttributes/@width", link);
    assertXpathNotExists("//ui:link/ui:windowAttributes/@height", link);
    final int top = 123;
    final int left = 234;
    final int width = 345;
    final int height = 456;
    link = new WLink.Builder(TEXT, LINK_URL).top(top).left(left).width(width).height(height).build();
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(TEXT, "normalize-space(//ui:link)", link);
    assertXpathUrlEvaluatesTo(LINK_URL, "//ui:link/@url", link);
    assertXpathEvaluatesTo(String.valueOf(top), "//ui:link/ui:windowAttributes/@top", link);
    assertXpathEvaluatesTo(String.valueOf(left), "//ui:link/ui:windowAttributes/@left", link);
    assertXpathEvaluatesTo(String.valueOf(width), "//ui:link/ui:windowAttributes/@width", link);
    assertXpathEvaluatesTo(String.valueOf(height), "//ui:link/ui:windowAttributes/@height", link);
}
Also used : WLink(com.github.bordertech.wcomponents.WLink) Test(org.junit.Test)

Example 5 with WLink

use of com.github.bordertech.wcomponents.WLink in project wcomponents by BorderTech.

the class WLinkRenderer_Test method testRenderedFormat.

@Test
public void testRenderedFormat() throws IOException, SAXException, XpathException {
    // Test individual options
    WLink link = new WLink();
    assertSchemaMatch(link);
    link = new WLink();
    link.setText(TEXT);
    link.setOpenNewWindow(false);
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(TEXT, "normalize-space(//ui:link)", link);
    assertXpathNotExists("//ui:link/@type", link);
    assertXpathNotExists("//ui:link/@imagePosition", link);
    link = new WLink();
    link.setAccessKey(ACCESS_KEY);
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(Character.toString(ACCESS_KEY), "//ui:link/@accessKey", link);
    link = new WLink();
    link.setToolTip(TITLE);
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(TITLE, "//ui:link/@toolTip", link);
    link = new WLink();
    link.setRel(REL);
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(REL, "//ui:link/@rel", link);
    link = new WLink(null, LINK_URL);
    assertSchemaMatch(link);
    assertXpathUrlEvaluatesTo(LINK_URL, "//ui:link/@url", link);
    link = new WLink(null, LINK_URL);
    link.setOpenNewWindow(false);
    assertSchemaMatch(link);
    assertXpathUrlEvaluatesTo(LINK_URL, "//ui:link/@url", link);
    // Test all attributes at once
    link = new WLink(TEXT, LINK_URL);
    link.setAccessKey(ACCESS_KEY);
    link.setToolTip(TITLE);
    link.setRel(REL);
    link.setOpenNewWindow(true);
    link.setImageUrl(IMAGE_URL);
    link.setRenderAsButton(true);
    link.setImagePosition(WLink.ImagePosition.EAST);
    assertSchemaMatch(link);
    assertXpathEvaluatesTo(TEXT, "normalize-space(//ui:link)", link);
    assertXpathEvaluatesTo(Character.toString(ACCESS_KEY), "//ui:link/@accessKey", link);
    assertXpathEvaluatesTo(TITLE, "//ui:link/@toolTip", link);
    assertXpathEvaluatesTo(REL, "//ui:link/@rel", link);
    assertXpathUrlEvaluatesTo(LINK_URL, "//ui:link/@url", link);
    assertXpathUrlEvaluatesTo(IMAGE_URL, "//ui:link/@imageUrl", link);
    assertXpathEvaluatesTo("button", "//ui:link/@type", link);
    assertXpathEvaluatesTo("e", "//ui:link/@imagePosition", link);
    assertXpathEvaluatesTo(link.getTargetWindowName(), "//ui:link/ui:windowAttributes/@name", link);
}
Also used : WLink(com.github.bordertech.wcomponents.WLink) Test(org.junit.Test)

Aggregations

WLink (com.github.bordertech.wcomponents.WLink)9 Test (org.junit.Test)7 WImage (com.github.bordertech.wcomponents.WImage)3 TestAction (com.github.bordertech.wcomponents.TestAction)1 WContainer (com.github.bordertech.wcomponents.WContainer)1 ImagePosition (com.github.bordertech.wcomponents.WLink.ImagePosition)1 WPanel (com.github.bordertech.wcomponents.WPanel)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1