Search in sources :

Example 6 with ComponentEvent

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

the class PageActivationContextWorker method createActivationHandler.

private static ComponentEventHandler createActivationHandler(final FieldHandle[] handles, final String[] fieldTypes) {
    return new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            int count = Math.min(handles.length, event.getEventContext().getCount());
            for (int i = 0; i < count; ++i) {
                String fieldType = fieldTypes[i];
                FieldHandle handle = handles[i];
                Object value = event.coerceContext(i, fieldType);
                handle.set(instance, value);
            }
        }
    };
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) FieldHandle(org.apache.tapestry5.plastic.FieldHandle)

Example 7 with ComponentEvent

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

the class ActivationRequestParameterWorker method decorateLinks.

@SuppressWarnings("all")
private static void decorateLinks(TransformationSupport support, String fieldName, final FieldHandle handle, final String parameterName, final ValueEncoder encoder, final URLEncoder urlEncoder) {
    ComponentEventHandler handler = new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            Object value = handle.get(instance);
            if (value == null) {
                return;
            }
            Link link = event.getEventContext().get(Link.class, 0);
            String clientValue = encoder.toClient(value);
            // TAP5-1768: escape special characters
            clientValue = urlEncoder.encode(clientValue);
            link.addParameter(parameterName, clientValue);
        }
    };
    support.addEventHandler(EventConstants.DECORATE_COMPONENT_EVENT_LINK, 0, String.format("ActivationRequestParameterWorker decorate component event link event handler for field %s as query parameter '%s'", fieldName, parameterName), handler);
    support.addEventHandler(EventConstants.DECORATE_PAGE_RENDER_LINK, 0, String.format("ActivationRequestParameterWorker decorate page render link event handler for field %s as query parameter '%s'", fieldName, parameterName), handler);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) Link(org.apache.tapestry5.http.Link)

Example 8 with ComponentEvent

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

the class ActivationRequestParameterWorker method preallocateName.

private static void preallocateName(TransformationSupport support, final String parameterName) {
    ComponentEventHandler handler = new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            IdAllocator idAllocator = event.getEventContext().get(IdAllocator.class, 0);
            idAllocator.allocateId(parameterName);
        }
    };
    support.addEventHandler(EventConstants.PREALLOCATE_FORM_CONTROL_NAMES, 1, "ActivationRequestParameterWorker preallocate form control name '" + parameterName + "' event handler", handler);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component)

Example 9 with ComponentEvent

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

the class ActivationRequestParameterWorker method setValueFromInitializeEventHandler.

@SuppressWarnings("all")
private void setValueFromInitializeEventHandler(final TransformationSupport support, final String fieldName, final boolean required, final FieldHandle handle, final String parameterName, final ValueEncoder encoder, final URLEncoder urlEncoder) {
    ComponentEventHandler handler = new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            String clientValue = request.getParameter(parameterName);
            if (clientValue == null) {
                if (required) {
                    throw new TapestryException(String.format("Activation request parameter field %s is marked as required, but query parameter '%s' is null.", fieldName, parameterName), null);
                }
                return;
            }
            // TAP5-1768: unescape encoded value
            clientValue = urlEncoder.decode(clientValue);
            Object value = encoder.toValue(clientValue);
            handle.set(instance, value);
        }
    };
    support.addEventHandler(EventConstants.ACTIVATE, 0, String.format("Restoring field %s from query parameter '%s'", fieldName, parameterName), handler);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 10 with ComponentEvent

use of org.apache.tapestry5.runtime.ComponentEvent 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)

Aggregations

ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)18 Logger (org.slf4j.Logger)13 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)12 ComponentModel (org.apache.tapestry5.model.ComponentModel)12 Test (org.testng.annotations.Test)12 EventContext (org.apache.tapestry5.EventContext)8 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 Component (org.apache.tapestry5.runtime.Component)5 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)3 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)2 FieldHandle (org.apache.tapestry5.plastic.FieldHandle)2 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 Location (org.apache.tapestry5.commons.Location)1 Link (org.apache.tapestry5.http.Link)1 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)1 ComponentEventImpl (org.apache.tapestry5.internal.services.ComponentEventImpl)1 NotificationEventCallback (org.apache.tapestry5.internal.util.NotificationEventCallback)1 IdAllocator (org.apache.tapestry5.ioc.util.IdAllocator)1 JSONObject (org.apache.tapestry5.json.JSONObject)1