001    // Copyright May 4, 2006 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    package net.sf.tacos.components.dojo.form;
015    
016    import java.util.ArrayList;
017    import java.util.HashMap;
018    import java.util.List;
019    import java.util.Map;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.IScript;
023    import org.apache.tapestry.PageRenderSupport;
024    import org.apache.tapestry.TapestryUtils;
025    import org.apache.tapestry.dojo.form.AbstractFormWidget;
026    import org.apache.tapestry.engine.IEngineService;
027    import org.apache.tapestry.form.TranslatedField;
028    import org.apache.tapestry.form.TranslatedFieldSupport;
029    import org.apache.tapestry.form.ValidatableFieldSupport;
030    import org.apache.tapestry.json.JSONObject;
031    import org.apache.tapestry.services.DataSqueezer;
032    import org.apache.tapestry.valid.ValidatorException;
033    
034    /**
035     * An html field similar to a <code>textarea</code> field that 
036     * is wrapped by a dojo Editor2 widget.<p/>
037     * This component adds javascript that makes the dojo widget
038     * update the value of the textarea (to support async submits) it is bound to. 
039     * 
040     * @author andyhot
041     */
042    public abstract class HtmlArea extends AbstractFormWidget implements TranslatedField
043    {    
044        /**
045         * 
046         * {@inheritDoc}
047         */
048        protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
049        {
050            String value = getTranslatedFieldSupport().format(this, getValue());
051            
052            renderDelegatePrefix(writer, cycle);
053            
054            writer.begin("textarea");
055            writer.attribute("name", getName());
056            
057            if (isDisabled())
058                writer.attribute("disabled", "disabled");
059            
060            renderIdAttribute(writer, cycle);
061            
062            renderDelegateAttributes(writer, cycle);
063            
064            getTranslatedFieldSupport().renderContributions(this, writer, cycle);
065            getValidatableFieldSupport().renderContributions(this, writer, cycle);
066            
067            renderInformalParameters(writer, cycle);
068            
069            if (value != null) writer.print(value);
070    
071            writer.end();
072    
073            renderDelegateSuffix(writer, cycle);
074                  
075            Map parms = new HashMap();
076            parms.put("id", getClientId());
077            
078            JSONObject json = new JSONObject();        
079            json.put((Object)"widgetId", getName());
080            json.put((Object)"name", getName());
081            json.put((Object)"disabled", isDisabled());
082            if (getToolbarTemplatePath()!=null)
083                json.put((Object)"toolbarTemplatePath", getToolbarTemplatePath());
084            
085            parms.put("props", json.toString());
086            parms.put("widget", this);
087            
088            PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this);
089            getScript().execute(this, cycle, prs, parms);
090        }
091        
092        /**
093         * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
094         */
095        protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
096        {
097            String value = cycle.getParameter(getName());
098            
099            try
100            {
101                String text = (String) getTranslatedFieldSupport().parse(this, value);
102                
103                getValidatableFieldSupport().validate(this, writer, cycle, text);
104                
105                setValue(text);
106            }
107            catch (ValidatorException e)
108            {
109                getForm().getDelegate().record(e);
110            }
111        }    
112        
113        public abstract String getToolbarTemplatePath();
114            
115        /** @since 2.2 * */
116        public abstract Object getValue();
117    
118        /** @since 2.2 * */
119        public abstract void setValue(Object value);
120        
121        /** Injected. */
122        public abstract DataSqueezer getDataSqueezer();
123        
124        /**
125         * Injected.
126         */
127        public abstract ValidatableFieldSupport getValidatableFieldSupport();
128        
129        /**
130         * Injected.
131         */
132        public abstract TranslatedFieldSupport getTranslatedFieldSupport();    
133    
134        /**
135         * Injected.
136         */
137        public abstract IEngineService getDirectService();
138        
139        /**
140         * Injected.
141         */
142        public abstract IScript getScript();
143        
144        /**
145         * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
146         */
147        public boolean isRequired()
148        {
149            return getValidatableFieldSupport().isRequired(this);
150        }
151    
152        /** 
153         * {@inheritDoc}
154         */
155        public List getUpdateComponents()
156        {
157            List comps = new ArrayList();
158            comps.add(getClientId());
159            
160            return comps;
161        }
162        
163        /** 
164         * {@inheritDoc}
165         */
166        public boolean isAsync()
167        {
168            return false;
169        }
170        
171        /** 
172         * {@inheritDoc}
173         */
174        public boolean isJson()
175        {
176            return false;
177        }
178    }