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
015package org.apache.tapestry.form;
016
017import org.apache.hivemind.util.PropertyUtils;
018import org.apache.tapestry.IMarkupWriter;
019import org.apache.tapestry.IRequestCycle;
020
021/**
022 * Abstract {@link FormComponentContributor} implementation that adds an optional static javscript
023 * method reference to the page.
024 * 
025 * @author Paul Ferraro
026 * @since 4.0
027 */
028public abstract class AbstractFormComponentContributor implements FormComponentContributor
029{
030    private String _script = defaultScript();
031
032    public AbstractFormComponentContributor()
033    {
034    }
035
036    // Needed until HIVEMIND-134 fix is available
037    public AbstractFormComponentContributor(String initializer)
038    {
039        PropertyUtils.configureProperties(this, initializer);
040    }
041
042    /**
043     * Defines the default JavaScript file used by this contributor. Overriden by most subclasses
044     * that use JavaScript.
045     */
046    protected String defaultScript()
047    {
048        return null;
049    }
050
051    public String getScript()
052    {
053        return _script;
054    }
055
056    public void setScript(String script)
057    {
058        _script = script;
059    }
060
061    /**
062     * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter,
063     *      org.apache.tapestry.IRequestCycle, FormComponentContributorContext,
064     *      org.apache.tapestry.form.IFormComponent)
065     */
066    public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
067            FormComponentContributorContext context, IFormComponent field)
068    {
069        if (_script != null)
070            context.includeClasspathScript(_script);
071    }
072}