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