Search in sources :

Example 6 with ComponentEventCallback

use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.

the class ComponentEventImplTest method no_match_one_event_is_aborted.

/**
 * @since 5.2.0
 */
@Test
public void no_match_one_event_is_aborted() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    Logger logger = mockLogger();
    Object result = new Object();
    ComponentPageElementResources resources = mockResources();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, false);
    train_handleResult(handler, result, true);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    event.storeResult(result);
    assertFalse(event.matches("eventType", "someId", 0));
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 7 with ComponentEventCallback

use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.

the class AjaxFormLoop method onInjectRow.

Object onInjectRow(EventContext context) {
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            value = result;
            return true;
        }
    };
    resources.triggerContextEvent(EventConstants.ADD_ROW, context, callback);
    if (value == null)
        throw new IllegalArgumentException(String.format("Event handler for event 'addRow' from %s should have returned a non-null value.", resources.getCompleteId()));
    ajaxResponseRenderer.addFilter(new PartialMarkupRendererFilter() {

        public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
            pushContext();
            renderer.renderMarkup(writer, reply);
            popContext();
        }
    });
    return ajaxResponse;
}
Also used : PartialMarkupRendererFilter(org.apache.tapestry5.services.PartialMarkupRendererFilter) JSONObject(org.apache.tapestry5.json.JSONObject) PartialMarkupRenderer(org.apache.tapestry5.services.PartialMarkupRenderer) JSONObject(org.apache.tapestry5.json.JSONObject)

Example 8 with ComponentEventCallback

use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.

the class PageActivationContextCollectorImpl method collectPageActivationContext.

public Object[] collectPageActivationContext(String pageName) {
    Boolean hasHandler = cache.get(pageName);
    if (hasHandler == null) {
        ComponentModel model = modelSource.getPageModel(pageName);
        hasHandler = model.handlesEvent(EventConstants.PASSIVATE);
        cache.put(pageName, hasHandler);
    }
    if (!hasHandler)
        return EMPTY;
    // Get or create a page instance and trigger the event.
    Page page = requestPageCache.get(pageName);
    ComponentPageElement element = page.getRootElement();
    final Holder<Object[]> holder = Holder.create();
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            holder.put(typeCoercer.coerce(result, Object[].class));
            return true;
        }
    };
    element.triggerEvent(EventConstants.PASSIVATE, null, callback);
    if (!holder.hasValue())
        return EMPTY;
    return holder.get();
}
Also used : ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentModel(org.apache.tapestry5.model.ComponentModel) Page(org.apache.tapestry5.internal.structure.Page) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback)

Example 9 with ComponentEventCallback

use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.

the class ComponentEventImplTest method unable_to_coerce.

@Test
public void unable_to_coerce() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    ComponentPageElementResources resources = mockComponentPageElementResources();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, true);
    logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
    train_toClass(resources, Integer.class.getName(), Integer.class);
    train_getCount(context, 1);
    expect(context.get(Integer.class, 0)).andThrow(new NumberFormatException("Not so easy, is it?"));
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    event.setMethodDescription("foo.Bar.baz()");
    try {
        event.coerceContext(0, "java.lang.Integer");
        unreachable();
    } catch (IllegalArgumentException ex) {
        // Different JVMs will report the conversion error slightly differently,
        // so we don't try to check that part of the error message.
        assertTrue(ex.getMessage().startsWith("Exception in method foo.Bar.baz(), parameter #1:"));
    }
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 10 with ComponentEventCallback

use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.

the class ComponentEventImplTest method store_result_and_abort.

@Test
public void store_result_and_abort() {
    Object result = new Object();
    String methodDescription = "foo.Bar.baz()";
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, true);
    EasyMock.expectLastCall().atLeastOnce();
    ComponentPageElementResources resources = mockResources();
    logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
    ComponentEventCallback handler = mockComponentEventHandler();
    train_handleResult(handler, result, true);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", null, handler, resources, false, model, logger);
    event.setMethodDescription(methodDescription);
    assertFalse(event.isAborted());
    assertTrue(event.storeResult(result));
    assertTrue(event.isAborted());
    verify();
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Aggregations

ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)14 ComponentModel (org.apache.tapestry5.model.ComponentModel)14 Logger (org.slf4j.Logger)13 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)12 Test (org.testng.annotations.Test)12 EventContext (org.apache.tapestry5.EventContext)8 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 List (java.util.List)1 Location (org.apache.tapestry5.commons.Location)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 ComponentEventImpl (org.apache.tapestry5.internal.services.ComponentEventImpl)1 NotificationEventCallback (org.apache.tapestry5.internal.util.NotificationEventCallback)1 PartialMarkupRenderer (org.apache.tapestry5.services.PartialMarkupRenderer)1 PartialMarkupRendererFilter (org.apache.tapestry5.services.PartialMarkupRendererFilter)1 IAnswer (org.easymock.IAnswer)1