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 org.apache.hivemind.util.PropertyUtils;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.json.JSONObject;
021    
022    /**
023     * Abstract {@link FormComponentContributor} implementation that adds an optional static javscript
024     * method reference to the page.
025     * 
026     * @author Paul Ferraro
027     * @since 4.0
028     */
029    public abstract class AbstractFormComponentContributor implements FormComponentContributor
030    {
031        private String _script = defaultScript();
032    
033        public AbstractFormComponentContributor()
034        {
035        }
036    
037        // Needed until HIVEMIND-134 fix is available
038        public AbstractFormComponentContributor(String initializer)
039        {
040            PropertyUtils.configureProperties(this, initializer);
041        }
042    
043        /**
044         * Defines the default JavaScript file used by this contributor. Overriden by most subclasses
045         * that use JavaScript.
046         */
047        public String defaultScript()
048        {
049            return null;
050        }
051    
052        public String getScript()
053        {
054            return _script;
055        }
056    
057        public void setScript(String script)
058        {
059            _script = script;
060        }
061    
062        /**
063         * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter,
064         *      org.apache.tapestry.IRequestCycle, FormComponentContributorContext,
065         *      org.apache.tapestry.form.IFormComponent)
066         */
067        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
068                FormComponentContributorContext context, IFormComponent field)
069        {
070            if (_script != null)
071                context.includeClasspathScript(_script);
072        }
073        
074        /**
075         * Utility used to append onto an existing property represented as an
076         * object array. 
077         * @param profile
078         * @param key
079         * @param value
080         */
081        public void accumulateProperty(JSONObject profile, String key, Object value)
082        {
083            profile.accumulate(key, value);
084        }
085        
086        /**
087         * Utility method to store a field specific profile property which can later
088         * be used by client side validation. 
089         * 
090         * @param field
091         *          The field to store the property for, will key off of {@link IFormComponent#getClientId()}.
092         * @param profile
093         *          The profile for the form.
094         * @param key
095         *          The property key to store.
096         * @param property
097         *          The property to store.
098         */
099        public void accumulateProfileProperty(IFormComponent field, JSONObject profile, 
100                String key, Object property)
101        {
102            if (!profile.has(field.getClientId())) 
103                profile.put(field.getClientId(), new JSONObject());
104            
105            JSONObject fieldProps = profile.getJSONObject(field.getClientId());
106            
107            accumulateProperty(fieldProps, key, property);
108        }
109    }