KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2004, 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 java.io.IOException JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.tapestry.IActionListener;
22 import org.apache.tapestry.IForm;
23 import org.apache.tapestry.IMarkupWriter;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.Tapestry;
26 import org.apache.tapestry.coerce.ValueConverter;
27 import org.apache.tapestry.listener.ListenerInvoker;
28 import org.apache.tapestry.services.DataSqueezer;
29
30 /**
31  * A specialized component used to edit a list of items within a form; it is similar to a
32  * {@link org.apache.tapestry.components.Foreach}but leverages hidden inputs within the
33  * &lt;form&gt; to store the items in the list. [ <a
34  * HREF="../../../../../ComponentReference/ListEdit.html">Component Reference </a>]
35  *
36  * @author Howard Lewis Ship
37  * @since 1.0.2
38  */

39
40 public abstract class ListEdit extends AbstractFormComponent
41 {
42     /**
43      * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
44      */

45     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
46     {
47         this.render(writer, cycle, getSource());
48     }
49
50     /**
51      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
52      */

53     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
54     {
55         String JavaDoc[] values = cycle.getParameters(getName());
56         
57         this.render(writer, cycle, (Iterator JavaDoc) getValueConverter().coerceValue(values, Iterator JavaDoc.class));
58     }
59
60     protected void render(IMarkupWriter writer, IRequestCycle cycle, Iterator JavaDoc i)
61     {
62         // If the source (when rendering), or the submitted values (on submit)
63
// are null, then skip the remainder (nothing to update, nothing to
64
// render).
65

66         if (i == null)
67             return;
68
69         int index = 0;
70
71         String JavaDoc element = getElement();
72
73         boolean indexBound = isParameterBound("index");
74
75         while (i.hasNext())
76         {
77             Object JavaDoc value = null;
78
79             if (indexBound)
80                 setIndex(index++);
81
82             if (cycle.isRewinding())
83                 value = convertValue((String JavaDoc) i.next());
84             else
85             {
86                 value = i.next();
87                 writeValue(getForm(), getName(), value);
88             }
89
90             setValue(value);
91
92             getListenerInvoker().invokeListener(getListener(), this, cycle);
93
94             if (element != null)
95             {
96                 writer.begin(element);
97                 renderInformalParameters(writer, cycle);
98             }
99
100             renderBody(writer, cycle);
101
102             if (element != null)
103                 writer.end();
104         }
105     }
106
107     private void writeValue(IForm form, String JavaDoc name, Object JavaDoc value)
108     {
109         String JavaDoc externalValue;
110
111         try
112         {
113             externalValue = getDataSqueezer().squeeze(value);
114         }
115         catch (IOException JavaDoc ex)
116         {
117             throw new ApplicationRuntimeException(Tapestry.format(
118                     "ListEdit.unable-to-convert-value",
119                     value), this, null, ex);
120         }
121
122         form.addHiddenValue(name, externalValue);
123     }
124
125     private Object JavaDoc convertValue(String JavaDoc value)
126     {
127         try
128         {
129             return getDataSqueezer().unsqueeze(value);
130         }
131         catch (IOException JavaDoc ex)
132         {
133             throw new ApplicationRuntimeException(Tapestry.format(
134                     "ListEdit.unable-to-convert-string",
135                     value), this, null, ex);
136         }
137     }
138
139     public abstract String JavaDoc getElement();
140
141     /** @since 2.2 * */
142
143     public abstract IActionListener getListener();
144
145     /** @since 3.0 * */
146
147     public boolean isDisabled()
148     {
149         return false;
150     }
151
152     /** @since 4.0 */
153
154     public abstract Iterator JavaDoc getSource();
155
156     /** @since 4.0 */
157
158     public abstract void setValue(Object JavaDoc value);
159
160     /** @since 4.0 */
161
162     public abstract void setIndex(int index);
163
164     /** @since 4.0 */
165
166     public abstract DataSqueezer getDataSqueezer();
167
168     /** @since 4.0 */
169
170     public abstract ValueConverter getValueConverter();
171
172     /**
173      * Injected.
174      *
175      * @since 4.0
176      */

177
178     public abstract ListenerInvoker getListenerInvoker();
179 }
Popular Tags