001    // Copyright (c) 2011. The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.corelib.mixins;
016    
017    import org.apache.tapestry5.BindingConstants;
018    import org.apache.tapestry5.ClientElement;
019    import org.apache.tapestry5.Field;
020    import org.apache.tapestry5.FieldFocusPriority;
021    import org.apache.tapestry5.annotations.AfterRender;
022    import org.apache.tapestry5.annotations.Environmental;
023    import org.apache.tapestry5.annotations.InjectContainer;
024    import org.apache.tapestry5.annotations.Parameter;
025    import org.apache.tapestry5.corelib.components.Form;
026    import org.apache.tapestry5.ioc.annotations.Inject;
027    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
028    import org.slf4j.Logger;
029    
030    /**
031     * A mixin that instruments the outer {@link org.apache.tapestry5.corelib.components.Form} on which
032     * component the focus should be activated.
033     * <p/>
034     * This is meant to be used only with {@link org.apache.tapestry5.corelib.components.Form} component.
035     *
036     * @since 5.3
037     * @tapestrydoc
038     */
039    public class FormFieldFocus
040    {
041        @Inject
042        private Logger logger;
043    
044        /**
045         * The outer Form
046         */
047        @InjectContainer
048        private Form form;
049    
050        /**
051         * The {@link org.apache.tapestry5.Field} instance that will receive the focus within
052         * the {@link org.apache.tapestry5.corelib.components.Form}.
053         */
054        @Parameter(required = true, defaultPrefix = BindingConstants.COMPONENT, allowNull = false)
055        private Field focusField;
056    
057        @Environmental
058        private JavaScriptSupport javascriptSupport;
059    
060    
061        @AfterRender
062        void focusField()
063        {
064            javascriptSupport.autofocus(FieldFocusPriority.OVERRIDE, focusField.getControlName());
065    
066            logger.trace("Focus OVERRIDE done on field {}", focusField.getControlName());
067        }
068    
069    }