001    // Copyright 2005 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.tapestry.form;
016    
017    import java.util.Locale;
018    
019    import org.apache.hivemind.ClassResolver;
020    import org.apache.hivemind.Resource;
021    import org.apache.hivemind.util.ClasspathResource;
022    import org.apache.tapestry.IComponent;
023    import org.apache.tapestry.IForm;
024    import org.apache.tapestry.IRequestCycle;
025    import org.apache.tapestry.PageRenderSupport;
026    import org.apache.tapestry.TapestryUtils;
027    import org.apache.tapestry.json.JSONObject;
028    
029    /**
030     * Implementation of {@link org.apache.tapestry.form.FormComponentContributorContext}.
031     * 
032     * @author Howard Lewis Ship
033     * @since 4.0
034     */
035    public class FormComponentContributorContextImpl extends ValidationMessagesImpl implements
036            FormComponentContributorContext
037    {
038        private final ClassResolver _resolver;
039    
040        private final PageRenderSupport _pageRenderSupport;
041    
042        private final IFormComponent _field;
043    
044        private final IForm _form;
045    
046        private final String _formId;
047        
048        /**
049         * Used only for testing.
050         */
051    
052        FormComponentContributorContextImpl(IFormComponent field)
053        {
054            super(field, Locale.ENGLISH);
055    
056            _field = field;
057            _resolver = null;
058            _formId = null;
059            _pageRenderSupport = null;
060            _form = null;
061        }
062    
063        public FormComponentContributorContextImpl(Locale locale, IRequestCycle cycle,
064                IFormComponent field)
065        {
066            super(field, locale);
067    
068            _field = field;
069            _form = field.getForm();
070            _formId = _form.getName();
071    
072            _resolver = cycle.getInfrastructure().getClassResolver();
073    
074            _pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, field);
075        }
076    
077        public void includeClasspathScript(String path)
078        {
079            Resource resource = new ClasspathResource(_resolver, path);
080            
081            _pageRenderSupport.addExternalScript(_form, resource);
082        }
083        
084        public void addSubmitHandler(String submitListener)
085        {
086            _pageRenderSupport.addInitializationScript(_form, "Tapestry.onsubmit('" + _formId + "', "
087                    + submitListener + ");");
088        }
089        
090        public void addInitializationScript(IComponent target, String script)
091        {
092            _pageRenderSupport.addInitializationScript(_form, script);
093        }
094        
095        public void registerForFocus(int priority)
096        {
097            _form.registerForFocus(_field, priority);
098        }
099    
100        public JSONObject getProfile()
101        {
102            return _form.getProfile();
103        }
104    }