Search in sources :

Example 1 with AbstractEventContext

use of org.apache.tapestry5.internal.AbstractEventContext in project tapestry-5 by apache.

the class Select method onChange.

Object onChange(final EventContext context, @RequestParameter(value = "t:selectvalue", allowBlank = true) final String selectValue) throws ValidationException {
    final Object newValue = toValue(selectValue);
    CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
    EventContext newContext = new AbstractEventContext() {

        @Override
        public int getCount() {
            return context.getCount() + 1;
        }

        @Override
        public <T> T get(Class<T> desiredType, int index) {
            if (index == 0) {
                return typeCoercer.coerce(newValue, desiredType);
            }
            return context.get(desiredType, index - 1);
        }
    };
    this.resources.triggerContextEvent(EventConstants.VALUE_CHANGED, newContext, callback);
    this.value = newValue;
    return callback.getResult();
}
Also used : AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) CaptureResultCallback(org.apache.tapestry5.internal.util.CaptureResultCallback) AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext)

Example 2 with AbstractEventContext

use of org.apache.tapestry5.internal.AbstractEventContext in project tapestry-5 by apache.

the class Autocomplete method onAutocomplete.

Object onAutocomplete(final EventContext context, @RequestParameter("t:input") final String input) {
    final Holder<List> matchesHolder = Holder.create();
    // Default it to an empty list.
    matchesHolder.put(Collections.emptyList());
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            List matches = coercer.coerce(result, List.class);
            matchesHolder.put(matches);
            return true;
        }
    };
    EventContext newContext = new AbstractEventContext() {

        @Override
        public int getCount() {
            return context.getCount() + 1;
        }

        @Override
        public <T> T get(Class<T> desiredType, int index) {
            if (index == 0) {
                return coercer.coerce(input, desiredType);
            }
            return context.get(desiredType, index - 1);
        }
    };
    resources.triggerContextEvent(EventConstants.PROVIDE_COMPLETIONS, newContext, callback);
    JSONObject reply = new JSONObject();
    reply.put("matches", JSONArray.from(matchesHolder.get()));
    // A JSONObject response is always preferred, as that triggers the whole partial page render pipeline.
    return reply;
}
Also used : AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) JSONObject(org.apache.tapestry5.json.JSONObject) List(java.util.List) JSONObject(org.apache.tapestry5.json.JSONObject)

Aggregations

AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)2 List (java.util.List)1 CaptureResultCallback (org.apache.tapestry5.internal.util.CaptureResultCallback)1 JSONObject (org.apache.tapestry5.json.JSONObject)1