I was ofcourse a bit quick with my solution. I didn't test it enough
after a bit of refactoring and I now get ClassCastExceptions probably
due to the AbstractField being enhanced a bit.

I got around that by some reflection-perfection but there is probably
a better solution to getting the clientId.

New implementation of the filter looks like this:
public class FieldFocusFilter implements PartialMarkupRendererFilter {
    private final JavaScriptSupport javaScriptSupport;
    private final Object field;

    public FieldFocusFilter(final JavaScriptSupport javaScriptSupport,
final Object field) {
        this.javaScriptSupport = javaScriptSupport;
        this.field = field;
    }

    @Override
    public void renderMarkup(final MarkupWriter writer, final JSONObject reply,
            final PartialMarkupRenderer renderer) {
        renderer.renderMarkup(writer, reply);
        final String clientId = this.getClientId();
        this.javaScriptSupport.autofocus(FieldFocusPriority.OVERRIDE, clientId);
    }

    private String getClientId() {
        String clientId;
        try {
            final Method method =
this.field.getClass().getMethod("getClientId");
            clientId = String.valueOf(method.invoke(this.field));
        } catch (final Exception e) {
            clientId = null;
        }

        return clientId;
    }
}

If someone have a better solution than the reflection-hack, please enlighten me.

/Joakim


On Sun, Mar 13, 2011 at 1:11 PM, Joakim Olsson <[email protected]> wrote:
> Hi,
>
> answering myself in case it helps someone else in the future.
>
> Inspired by a reply by Josh Canfield to another question in the same
> area I came up with the following solution.
>
> I created a PartialMarkupRendererFilter-implementation that takes a
> JavaScriptSupport and the field to put focus on:
> public class FieldFocusFilter implements PartialMarkupRendererFilter {
>    private final JavaScriptSupport javaScriptSupport;
>    private final AbstractField field;
>
>    public FieldFocusFilter(final JavaScriptSupport javaScriptSupport,
> final AbstractField field) {
>        this.javaScriptSupport = javaScriptSupport;
>        this.field = field;
>    }
>
>    @Override
>    public void renderMarkup(final MarkupWriter writer, final JSONObject reply,
>            final PartialMarkupRenderer renderer) {
>        renderer.renderMarkup(writer, reply);
>        this.javaScriptSupport.autofocus(FieldFocusPriority.OVERRIDE,
> this.field.getClientId());
>    }
> }
>
> In my page-class I then inject the PageRenderQueue, JavaScriptSupport
> and the field to put focus on:
>    @Inject
>    private PageRenderQueue pageRenderQueue;
>
>    @Inject
>    private JavaScriptSupport javaScriptSupport;
>
>    @Component
>    private TextField focusField;
>
> In my onAddRowFromXxx-method I add a new instance of the
> FieldFocusFilter to the PageRenderQueue:
>        this.pageRenderQueue.addPartialMarkupRendererFilter(new
> FieldFocusFilter(this.javaScriptSupport, this.focusField));
>
> Thanks for the inspiration Josh. :-)
>
> Regards,
> Joakim
>
>
> On Sat, Feb 26, 2011 at 6:16 PM, Joakim Olsson <[email protected]> wrote:
>> Hi,
>>
>> I would like to get focus on the first available field in the added
>> row in an AjaxFormLoop. Is it possible in an easy way?
>>
>> Regards,
>> Joakim
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to