Search in sources :

Example 1 with WebComponent

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

the class AbstractContainerHelper_Test method testGetUI.

@Test
public void testGetUI() {
    AbstractContainerHelper helper = new MyContainerHelper();
    WebComponent webComponent = new WTextField();
    helper.setWebComponent(webComponent);
    Assert.assertSame("getUI returned incorrect UI", webComponent, helper.getUI());
}
Also used : WebComponent(com.github.bordertech.wcomponents.WebComponent) WTextField(com.github.bordertech.wcomponents.WTextField) Test(org.junit.Test)

Example 2 with WebComponent

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

the class AbstractContainerHelper_Test method testSetWebComponent.

@Test
public void testSetWebComponent() {
    AbstractContainerHelper helper = new MyContainerHelper();
    WebComponent webComponent = new WTextField();
    // getUI throws a NPE
    Assert.assertNull("Default interceptor should be null", helper.getInterceptor());
    helper.setWebComponent(webComponent);
    Assert.assertSame("Incorrect UI set", webComponent, helper.getUI());
    try {
        helper = new MyContainerHelper();
        helper.setWebComponent(new MyWebComponent());
        Assert.fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        Assert.assertNull("Interceptor should be null", helper.getInterceptor());
    }
}
Also used : WebComponent(com.github.bordertech.wcomponents.WebComponent) WTextField(com.github.bordertech.wcomponents.WTextField) Test(org.junit.Test)

Example 3 with WebComponent

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

the class InterceptorComponent method replaceInterceptor.

/**
 * Utility method for replacing an individual interceptor within an existing chain.
 *
 * @param match the type of the interceptor to be replaced.
 * @param replacement the new interceptor to be used as a replacement.
 * @param chain the existing interceptor chain in which the replacement will take place.
 * @return the modified interceptor chain. If no match was found, the existing interceptor chain is returned
 * unchanged.
 */
public static InterceptorComponent replaceInterceptor(final Class match, final InterceptorComponent replacement, final InterceptorComponent chain) {
    if (chain == null) {
        return null;
    }
    InterceptorComponent current = chain;
    InterceptorComponent previous = null;
    InterceptorComponent updatedChain = null;
    while (updatedChain == null) {
        if (match.isInstance(current)) {
            // Found the interceptor that needs to be replaced.
            replacement.setBackingComponent(current.getBackingComponent());
            if (previous == null) {
                updatedChain = replacement;
            } else {
                previous.setBackingComponent(replacement);
                updatedChain = chain;
            }
        } else {
            previous = current;
            WebComponent next = current.getBackingComponent();
            if (next instanceof InterceptorComponent) {
                current = (InterceptorComponent) next;
            } else {
                // Reached the end of the chain. No replacement done.
                updatedChain = chain;
            }
        }
    }
    return updatedChain;
}
Also used : WebComponent(com.github.bordertech.wcomponents.WebComponent)

Example 4 with WebComponent

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

the class InterceptorComponent_Test method testAttachUIBadBacking.

@Test(expected = IllegalStateException.class)
public void testAttachUIBadBacking() {
    InterceptorComponent interceptor = new InterceptorComponent(new WebComponent() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void serviceRequest(final Request request) {
        // NO-OP
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void preparePaint(final Request request) {
        // NO-OP
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void paint(final RenderContext renderContext) {
        // NO-OP
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String getName() {
            return null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String getId() {
            return null;
        }
    });
    // This should throw an exception, as it doesn't know how to attach the UI
    // to the anonymous WebComponent implementation.
    interceptor.attachUI(new WLabel());
}
Also used : WebComponent(com.github.bordertech.wcomponents.WebComponent) RenderContext(com.github.bordertech.wcomponents.RenderContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Request(com.github.bordertech.wcomponents.Request) WLabel(com.github.bordertech.wcomponents.WLabel) Test(org.junit.Test)

Aggregations

WebComponent (com.github.bordertech.wcomponents.WebComponent)4 Test (org.junit.Test)3 WTextField (com.github.bordertech.wcomponents.WTextField)2 RenderContext (com.github.bordertech.wcomponents.RenderContext)1 Request (com.github.bordertech.wcomponents.Request)1 WLabel (com.github.bordertech.wcomponents.WLabel)1 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)1