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);
}
}
};
}
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);
}
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);
}
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);
}
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);
}
};
}
Aggregations