Search in sources :

Example 26 with EventContext

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

the class Autocomplete method onAutocomplete.

Object onAutocomplete(final EventContext context, @RequestParameter("t:input") final String input) {
    final Holder<List> matchesHolder = Holder.create();
    // Default it to an empty list.
    matchesHolder.put(Collections.emptyList());
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            List matches = coercer.coerce(result, List.class);
            matchesHolder.put(matches);
            return true;
        }
    };
    EventContext newContext = new AbstractEventContext() {

        @Override
        public int getCount() {
            return context.getCount() + 1;
        }

        @Override
        public <T> T get(Class<T> desiredType, int index) {
            if (index == 0) {
                return coercer.coerce(input, desiredType);
            }
            return context.get(desiredType, index - 1);
        }
    };
    resources.triggerContextEvent(EventConstants.PROVIDE_COMPLETIONS, newContext, callback);
    JSONObject reply = new JSONObject();
    reply.put("matches", JSONArray.from(matchesHolder.get()));
    // A JSONObject response is always preferred, as that triggers the whole partial page render pipeline.
    return reply;
}
Also used : AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) JSONObject(org.apache.tapestry5.json.JSONObject) List(java.util.List) JSONObject(org.apache.tapestry5.json.JSONObject)

Example 27 with EventContext

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

the class Form method onAction.

@SuppressWarnings({ "unchecked", "InfiniteLoopStatement" })
Object onAction(EventContext context) throws IOException {
    beforeProcessSubmit(context);
    tracker.clear();
    formSupport = new FormSupportImpl(resources, validationId);
    environment.push(ValidationTracker.class, tracker);
    environment.push(FormSupport.class, formSupport);
    Heartbeat heartbeat = new HeartbeatImpl();
    environment.push(Heartbeat.class, heartbeat);
    heartbeat.begin();
    boolean didPushBeanValidationContext = false;
    try {
        resources.triggerContextEvent(EventConstants.PREPARE_FOR_SUBMIT, context, eventCallback);
        if (eventCallback.isAborted())
            return true;
        resources.triggerContextEvent(EventConstants.PREPARE, context, eventCallback);
        if (eventCallback.isAborted())
            return true;
        if (isFormCancelled()) {
            executeStoredActions(true);
            resources.triggerContextEvent(EventConstants.CANCELED, context, eventCallback);
            if (eventCallback.isAborted())
                return true;
        }
        environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
        didPushBeanValidationContext = true;
        executeStoredActions(false);
        heartbeat.end();
        formSupport.executeDeferred();
        fireValidateEvent(EventConstants.VALIDATE, context, eventCallback);
        if (eventCallback.isAborted()) {
            return true;
        }
        afterValidate();
        if (!tracker.getHasErrors()) {
            tracker.clear();
        }
        String eventType = tracker.getHasErrors() ? EventConstants.FAILURE : EventConstants.SUCCESS;
        resources.triggerContextEvent(eventType, context, eventCallback);
        if (eventCallback.isAborted()) {
            return true;
        }
        // Lastly, tell anyone whose interested that the form is completely
        // submitted.
        resources.triggerContextEvent(EventConstants.SUBMIT, context, eventCallback);
        afterSuccessOrFailure();
        if (eventCallback.isAborted()) {
            return true;
        }
        if (tracker.getHasErrors() && !request.isXHR()) {
            return STREAM_ACTIVE_PAGE_CONTENT;
        }
        return false;
    } finally {
        environment.pop(Heartbeat.class);
        environment.pop(FormSupport.class);
        environment.pop(ValidationTracker.class);
        if (didPushBeanValidationContext) {
            environment.pop(BeanValidationContext.class);
        }
    }
}
Also used : Heartbeat(org.apache.tapestry5.services.Heartbeat) FormSupportImpl(org.apache.tapestry5.corelib.internal.FormSupportImpl) HeartbeatImpl(org.apache.tapestry5.internal.services.HeartbeatImpl) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 28 with EventContext

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

the class ComponentPageElementImpl method processEventTriggering.

@SuppressWarnings("all")
private boolean processEventTriggering(String eventType, EventContext context, ComponentEventCallback callback) {
    boolean result = false;
    ComponentPageElement component = this;
    String componentId = "";
    // Provide a default handler for when the provided handler is null.
    final ComponentEventCallback providedHandler = callback == null ? new NotificationEventCallback(eventType, completeId) : callback;
    ComponentEventCallback wrapped = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            if (result instanceof Boolean)
                return (Boolean) result;
            return providedHandler.handleResult(result);
        }
    };
    RuntimeException rootException = null;
    // Because I don't like to reassign parameters.
    String currentEventType = eventType;
    EventContext currentContext = context;
    // Track the location of the original component for the event, even as we work our way up
    // the hierarchy. This may not be ideal if we trigger an "exception" event ... or maybe
    // it's right (it's the location of the originally thrown exception).
    Location location = component.getComponentResources().getLocation();
    while (component != null) {
        try {
            Logger logger = component.getEventLogger();
            ComponentEvent event = new ComponentEventImpl(currentEventType, componentId, currentContext, wrapped, elementResources, exactParameterCountMatch, coreResources.getComponentModel(), logger);
            logger.debug(TapestryMarkers.EVENT_DISPATCH, "Dispatch event: {}", event);
            result |= component.dispatchEvent(event);
            if (event.isAborted())
                return result;
        }// not the JVM).
         catch (Exception ex) {
            if (rootException != null)
                throw rootException;
            // We know component is not null and therefore has a component resources that
            // should have a location.
            // Wrap it up to help ensure that a location is available to the event handler
            // method or,
            // more likely, to the exception report page.
            rootException = new ComponentEventException(ex.getMessage(), eventType, context, location, ex);
            // Switch over to triggering an "exception" event, starting in the component that
            // threw the exception.
            currentEventType = "exception";
            currentContext = createParameterContext(rootException);
            continue;
        }
        // On each bubble up, make the event appear to come from the previous component
        // in which the event was triggered.
        componentId = component.getId();
        component = component.getContainerElement();
    }
    if (rootException != null)
        throw rootException;
    return result;
}
Also used : ComponentEventImpl(org.apache.tapestry5.internal.services.ComponentEventImpl) Logger(org.slf4j.Logger) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) NotificationEventCallback(org.apache.tapestry5.internal.util.NotificationEventCallback) Location(org.apache.tapestry5.commons.Location)

Example 29 with EventContext

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

the class PageCallbackTest method callback_with_context.

@Test
public void callback_with_context() {
    EventContext context = new ArrayEventContext(typeCoercer, 1, 2);
    PageRenderLinkSource source = mockPageRenderLinkSource();
    Link link = mockLink();
    expect(source.createPageRenderLinkWithContext("bar", "1", "2")).andReturn(link);
    PageCallback pc = new PageCallback("bar", context);
    assertEquals(pc.toString(), "PageCallback[bar 1/2]");
    replay();
    assertSame(pc.toLink(source), link);
    verify();
}
Also used : ArrayEventContext(org.apache.tapestry5.internal.services.ArrayEventContext) EventContext(org.apache.tapestry5.EventContext) ArrayEventContext(org.apache.tapestry5.internal.services.ArrayEventContext) PageRenderLinkSource(org.apache.tapestry5.services.PageRenderLinkSource) PageCallback(org.apache.tapestry5.PageCallback) Link(org.apache.tapestry5.http.Link) Test(org.testng.annotations.Test)

Aggregations

EventContext (org.apache.tapestry5.EventContext)16 Test (org.testng.annotations.Test)15 Logger (org.slf4j.Logger)9 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)8 Link (org.apache.tapestry5.http.Link)8 ComponentModel (org.apache.tapestry5.model.ComponentModel)8 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)8 Page (org.apache.tapestry5.internal.structure.Page)5 ComponentEventRequestParameters (org.apache.tapestry5.services.ComponentEventRequestParameters)5 EmptyEventContext (org.apache.tapestry5.internal.EmptyEventContext)4 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)4 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)3 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)3 JSONObject (org.apache.tapestry5.json.JSONObject)3 ComponentClassResolver (org.apache.tapestry5.services.ComponentClassResolver)3 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)3 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)3 Request (org.apache.tapestry5.http.services.Request)2 PageRenderLinkSource (org.apache.tapestry5.services.PageRenderLinkSource)2 IOException (java.io.IOException)1