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