Search in sources :

Example 1 with ComponentEventHandler

use of org.apache.tapestry5.services.ComponentEventHandler 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 2 with ComponentEventHandler

use of org.apache.tapestry5.services.ComponentEventHandler 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 3 with ComponentEventHandler

use of org.apache.tapestry5.services.ComponentEventHandler 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 4 with ComponentEventHandler

use of org.apache.tapestry5.services.ComponentEventHandler 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 5 with ComponentEventHandler

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

the class PageActivationContextWorker method createPassivateHandler.

private static ComponentEventHandler createPassivateHandler(final FieldHandle[] handles) {
    return new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            Object result;
            if (handles.length == 1) {
                // simple / common case for a single @PageActivationContext
                result = handles[0].get(instance);
            } else {
                LinkedList<Object> list = CollectionFactory.newLinkedList();
                // iterate backwards
                for (int i = handles.length - 1; i > -1; i--) {
                    FieldHandle handle = handles[i];
                    Object value = handle.get(instance);
                    // ignore trailing nulls
                    if (value != null || !list.isEmpty()) {
                        list.addFirst(value);
                    }
                }
                result = list.isEmpty() ? null : list;
            }
            event.storeResult(result);
        }
    };
}
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)

Aggregations

Component (org.apache.tapestry5.runtime.Component)5 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)5 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)5 FieldHandle (org.apache.tapestry5.plastic.FieldHandle)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 Link (org.apache.tapestry5.http.Link)1 IdAllocator (org.apache.tapestry5.ioc.util.IdAllocator)1