use of org.apache.tapestry5.annotations.Environmental in project tapestry-5 by apache.
the class EnvironmentalWorker method transform.
private void transform(final String componentClassName, PlasticField field) {
Environmental annotation = field.getAnnotation(Environmental.class);
field.claim(annotation);
final String fieldName = field.getName();
final Class fieldType = classCache.forName(field.getTypeName());
final boolean required = annotation.value();
ComputedValue<FieldConduit<Object>> provider = new ComputedValue<FieldConduit<Object>>() {
public FieldConduit<Object> get(InstanceContext context) {
return new EnvironmentalConduit(componentClassName, fieldName, fieldType, required);
}
public void set(Object instance, InstanceContext context, Object newValue) {
throw new RuntimeException(String.format("Field %s of component %s is read only.", fieldName, componentClassName));
}
};
field.setComputedConduit(provider);
}
use of org.apache.tapestry5.annotations.Environmental in project tapestry-5 by apache.
the class FormFragment method beginRender.
/**
* Renders a <div> tag and provides an override of the {@link org.apache.tapestry5.services.FormSupport}
* environmental.
*/
void beginRender(MarkupWriter writer) {
FormSupport formSupport = environment.peekRequired(FormSupport.class);
String clientId = getClientId();
hiddenFieldPositioner = new HiddenFieldPositioner(writer, rules);
Element element = writer.element(this.element, "id", clientId, "data-component-type", "core/FormFragment");
if (alwaysSubmit) {
element.attribute("data-always-submit", "true");
}
resources.renderInformalParameters(writer);
if (!visible) {
element.attribute("style", "display: none;");
if (!alwaysSubmit) {
javascriptSupport.require("t5/core/form-fragment").invoke("hide").with(clientId);
}
}
componentActions = new ComponentActionSink(logger, clientDataEncoder);
// Here's the magic of environmentals ... we can create a wrapper around
// the normal FormSupport environmental that intercepts some of the behavior.
// Here we're setting aside all the actions inside the FormFragment so that we
// can control whether those actions occur when the form is submitted.
FormSupport override = new FormSupportAdapter(formSupport) {
@Override
public <T> void store(T component, ComponentAction<T> action) {
componentActions.store(component, action);
}
@Override
public <T> void storeCancel(T component, ComponentAction<T> action) {
componentActions.storeCancel(component, action);
}
@Override
public <T> void storeAndExecute(T component, ComponentAction<T> action) {
componentActions.store(component, action);
action.execute(component);
}
};
// Tada! Now all the enclosed components will use our override of FormSupport,
// until we pop it off.
environment.push(FormSupport.class, override);
}
use of org.apache.tapestry5.annotations.Environmental in project tapestry-5 by apache.
the class FormFragment method afterRender.
/**
* Closes the <div> tag and pops off the {@link org.apache.tapestry5.services.FormSupport} environmental
* override.
*
* @param writer
*/
void afterRender(MarkupWriter writer) {
Element hidden = hiddenFieldPositioner.getElement();
hidden.attributes("type", "hidden", "name", Form.FORM_DATA, "value", componentActions.getClientData());
// div
writer.end();
environment.pop(FormSupport.class);
resetClientId();
}
use of org.apache.tapestry5.annotations.Environmental in project tapestry-5 by apache.
the class PropertyEditor method beginRender.
/**
* Returns a Block for rendering the property. The Block will be able to access the {@link PropertyEditContext} via
* the {@link Environmental} annotation.
*/
Block beginRender() {
Block override = overrides.getOverrideBlock(propertyModel.getId());
if (override != null) {
return override;
}
String dataType = propertyModel.getDataType();
if (dataType == null)
throw new RuntimeException(String.format("The data type for property '%s' of %s is null.", propertyModel.getPropertyName(), object));
try {
return beanBlockSource.getEditBlock(dataType);
} catch (RuntimeException ex) {
String message = messages.format("core-block-error", propertyModel.getPropertyName(), dataType, object, ex);
throw new TapestryException(message, resources.getLocation(), ex);
}
}
Aggregations