KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > AbstractFormComponentContributor


1 // Copyright 2005 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

15 package org.apache.tapestry.form;
16
17 import org.apache.hivemind.Resource;
18 import org.apache.hivemind.util.ClasspathResource;
19 import org.apache.hivemind.util.PropertyUtils;
20 import org.apache.tapestry.IForm;
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.TapestryUtils;
24
25 /**
26  * Abstract {@link FormComponentContributor} implementation that adds an optional static
27  * javscript method reference to the page.
28  *
29  * @author Paul Ferraro
30  * @since 4.0
31  */

32 public abstract class AbstractFormComponentContributor implements FormComponentContributor
33 {
34     private String JavaDoc _script = defaultScript();
35     
36     public AbstractFormComponentContributor()
37     {
38     }
39     
40     // Needed until HIVEMIND-134 fix is available
41
public AbstractFormComponentContributor(String JavaDoc initializer)
42     {
43         PropertyUtils.configureProperties(this, initializer);
44     }
45     
46     /**
47      * Defines the default JavaScript file used by this contributor. Overriden by most subclasses
48      * that use JavaScript.
49      */

50     protected String JavaDoc defaultScript()
51     {
52         return null;
53     }
54     
55     public String JavaDoc getScript()
56     {
57         return _script;
58     }
59     
60     public void setScript(String JavaDoc script)
61     {
62         _script = script;
63     }
64     
65     /**
66      * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, FormComponentContributorContext, org.apache.tapestry.form.IFormComponent)
67      */

68     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field)
69     {
70         if (_script != null)
71         {
72             // TODO: cycle.getInfrastructure().getClassResolver()
73

74             Resource script = new ClasspathResource(cycle.getEngine().getClassResolver(), _script);
75             
76             TapestryUtils.getPageRenderSupport(cycle, field).addExternalScript(script);
77         }
78     }
79     
80     /**
81      * Helper method that adds the specified submit handler to to the specified form.
82      */

83     protected void addSubmitHandler(IForm form, String JavaDoc handler)
84     {
85         form.addEventHandler(FormEventType.SUBMIT, handler);
86     }
87 }
88
Popular Tags