View Javadoc

1   // Copyright May 4, 2006 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  package net.sf.tacos.components.dojo.form;
15  
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  import org.apache.tapestry.IMarkupWriter;
21  import org.apache.tapestry.IRequestCycle;
22  import org.apache.tapestry.IScript;
23  import org.apache.tapestry.PageRenderSupport;
24  import org.apache.tapestry.TapestryUtils;
25  import org.apache.tapestry.dojo.form.AbstractFormWidget;
26  import org.apache.tapestry.engine.IEngineService;
27  import org.apache.tapestry.form.TranslatedField;
28  import org.apache.tapestry.form.TranslatedFieldSupport;
29  import org.apache.tapestry.form.ValidatableFieldSupport;
30  import org.apache.tapestry.json.JSONObject;
31  import org.apache.tapestry.services.DataSqueezer;
32  import org.apache.tapestry.valid.ValidatorException;
33  
34  /**
35   * An html field similar to a <code>textarea</code> field that 
36   * is wrapped by a dojo Editor2 widget.<p/>
37   * This component adds javascript that makes the dojo widget
38   * update the value of the textarea (to support async submits) it is bound to. 
39   * 
40   * @author andyhot
41   */
42  public abstract class HtmlArea extends AbstractFormWidget implements TranslatedField
43  {    
44      /**
45       * 
46       * {@inheritDoc}
47       */
48      protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
49      {
50          String value = getTranslatedFieldSupport().format(this, getValue());
51          
52          renderDelegatePrefix(writer, cycle);
53          
54          writer.begin("textarea");
55          writer.attribute("name", getName());
56          
57          if (isDisabled())
58              writer.attribute("disabled", "disabled");
59          
60          renderIdAttribute(writer, cycle);
61          
62          renderDelegateAttributes(writer, cycle);
63          
64          getTranslatedFieldSupport().renderContributions(this, writer, cycle);
65          getValidatableFieldSupport().renderContributions(this, writer, cycle);
66          
67          renderInformalParameters(writer, cycle);
68          
69          if (value != null) writer.print(value);
70  
71          writer.end();
72  
73          renderDelegateSuffix(writer, cycle);
74                
75          Map parms = new HashMap();
76          parms.put("id", getClientId());
77          
78          JSONObject json = new JSONObject();        
79          json.put((Object)"widgetId", getName());
80          json.put((Object)"name", getName());
81          json.put((Object)"disabled", isDisabled());
82          if (getToolbarTemplatePath()!=null)
83              json.put((Object)"toolbarTemplatePath", getToolbarTemplatePath());
84          
85          parms.put("props", json.toString());
86          parms.put("widget", this);
87          
88          PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this);
89          getScript().execute(this, cycle, prs, parms);
90      }
91      
92      /**
93       * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
94       */
95      protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
96      {
97          String value = cycle.getParameter(getName());
98          
99          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 }