KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > repopulation > RepopulationFormMap


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: RepopulationFormMap.java,v 1.6 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation;
21
22
23 import java.util.List JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.enhydra.barracuda.core.comp.TemplateModel;
31 import org.enhydra.barracuda.core.forms.DefaultFormMap;
32 import org.enhydra.barracuda.core.forms.FormMap;
33 import org.enhydra.barracuda.core.forms.FormElement;
34 import org.enhydra.barracuda.core.forms.FormType;
35 import org.enhydra.barracuda.core.forms.ParseException;
36 import org.enhydra.barracuda.core.comp.ViewContext;
37 import org.enhydra.barracuda.core.comp.TemplateDirective;
38 import org.enhydra.barracuda.core.comp.model.ModelListener;
39 import org.enhydra.barracuda.core.comp.BInput;
40 import org.enhydra.barracuda.core.comp.BSelect;
41 import org.enhydra.barracuda.core.comp.BComponent;
42 import org.apache.log4j.*;
43
44
45 /**
46  * The class <code>RepopulationFormMap</code> provides a convenient way to repopulate
47  * HTML form contents.
48  *
49  * It can be used as a TemplateModel. The values of the defined FormElements are
50  * wrapped into a BInput. The BInput sets the name of the FormElement.
51  * If a prefix has been given -- either explicit with setPrefix() or implicit by the
52  * overloaded map(..)-methods -- it will be prepended to the name.
53  *
54  * @author <a HREF="mailto:deets@web.de">Diez Roggisch</a>
55  * @version 1.0
56  */

57 public class RepopulationFormMap extends ErrorFormMap implements TemplateModel {
58     protected static final Logger logger = Logger.getLogger(RepopulationFormMap.class.getName());
59
60     /**
61      * The prefix can be used to limit the
62      *
63      */

64     protected String JavaDoc prefix;
65
66     /**
67      * Describe variable <code>modelName</code> here.
68      *
69      */

70     protected String JavaDoc modelName;
71
72     protected ViewContext viewContext = null;
73     protected List JavaDoc listeners = new ArrayList JavaDoc();
74
75     protected List JavaDoc _modelChilds = new ArrayList JavaDoc();
76
77     public void defineElement(FormElement element) {
78         super.defineElement(element);
79         if(element instanceof ChildModelElement) {
80             _modelChilds.add(element);
81         }
82     }
83
84     public void defineElement(java.lang.String JavaDoc key, FormElement element) {
85         super.defineElement(key, element);
86         if(element instanceof ChildModelElement) {
87             _modelChilds.add(element);
88         }
89     }
90
91     public List JavaDoc getChildModels() {
92         List JavaDoc res = new ArrayList JavaDoc();
93         for(Iterator JavaDoc it = _modelChilds.iterator(); it.hasNext();) {
94             ChildModelElement cme = (ChildModelElement)it.next();
95             res.add(cme.getChildModel(modelName, prefix));
96         }
97         return res;
98     }
99
100     /**
101      * Describe <code>setPrefix</code> method here.
102      *
103      * @param prefix a <code>String</code> value
104      */

105     public void setPrefix(String JavaDoc prefix) {
106         this.prefix = prefix;
107     }
108
109
110     /**
111      * Describe <code>getPrefix</code> method here.
112      *
113      * @return a <code>String</code> value
114      */

115     public String JavaDoc getPrefix() {
116         return this.prefix;
117     }
118
119
120
121     // These methods simply take care of th prefix
122
/**
123      * Describe <code>map</code> method here.
124      *
125      * @param req a <code>ServletRequest</code> value
126      * @return a <code>FormMap</code> value
127      */

128     public FormMap map(ServletRequest req) {
129         return super.map(req, prefix);
130     }
131
132     /**
133      * Describe <code>map</code> method here.
134      *
135      * @param req a <code>ServletRequest</code> value
136      * @param prefix a <code>String</code> value
137      * @return a <code>FormMap</code> value
138      */

139     public FormMap map(ServletRequest req, String JavaDoc prefix) {
140         this.prefix = prefix;
141         return super.map(req, prefix);
142     }
143
144     /**
145      * Describe <code>map</code> method here.
146      *
147      * @param req a <code>ServletRequest</code> value
148      * @param loc a <code>Locale</code> value
149      * @return a <code>FormMap</code> value
150      */

151     public FormMap map(ServletRequest req, Locale JavaDoc loc) {
152         return super.map(req, prefix, loc);
153     }
154
155     // implementation of org.enhydra.barracuda.core.comp.Contextual interface
156

157     /**
158      * Describe <code>getItem</code> method here.
159      *
160      * @param key
161      * @return <description>
162      */

163     public Object JavaDoc getItem(String JavaDoc key)
164     {
165         if(logger.isDebugEnabled()) {
166             logger.debug("getItem() with key: " + key);
167         }
168         if(getElement(key) != null) {
169             String JavaDoc name = prefix != null ? prefix + key: key;
170             BComponent bc = null;
171             FormElement el = getElement(key);
172             if(el instanceof RepopulationElement) {
173                 Object JavaDoc res = ((RepopulationElement)el).render(getViewContext());
174                 if(!(res instanceof BComponent)) {
175                     return res;
176                 }
177                 bc = (BComponent)res;
178             }
179             else {
180                 BInput bi = new BInput();
181                 if(logger.isDebugEnabled() && el.getOrigVal() != null) {
182                     logger.debug("Class: " + el.getOrigVal().getClass() + " Value: " + el.getOrigVal());
183                 }
184                 bi.setValue((String JavaDoc)el.getOrigVal());
185                 bc = bi;
186             }
187             if(bc != null) {
188                 bc.setName(name);
189                 return bc;
190             }
191         }
192         return super.getErrorComponent(key);
193     }
194
195     public Object JavaDoc getItem(TemplateDirective td) {
196         return getItem(td.getKeyName());
197     }
198
199     /**
200      *
201      * @param vc <description>
202      */

203     public void setViewContext(ViewContext vc)
204     {
205         viewContext = vc;
206     }
207
208     /**
209      *
210      * @return <description>
211      */

212     public ViewContext getViewContext()
213     {
214         return viewContext;
215     }
216     // implementation of org.enhydra.barracuda.core.comp.TemplateModel interface
217

218     /**
219      * Describe <code>getName</code> method here.
220      *
221      * @return <description>
222      */

223     public String JavaDoc getName()
224     {
225         return modelName;
226     }
227
228
229     /**
230      * Describe <code>setName</code> method here.
231      *
232      * @param name a <code>String</code> value
233      */

234     public void setName(String JavaDoc name)
235     {
236         modelName = name;
237     }
238
239     /**
240      *
241      * @param td <description>
242      * @return <description>
243      */

244     public boolean processDirective(TemplateDirective td)
245     {
246         // TODO: implement this org.enhydra.barracuda.core.comp.TemplateModel method
247
return true;
248     }
249
250     // implementation of org.enhydra.barracuda.core.comp.model.Model interface
251

252     /**
253      *
254      * @param ml <description>
255      */

256     public void addModelListener(ModelListener ml)
257     {
258         listeners.add(ml);
259     }
260
261     /**
262      *
263      * @param ml <description>
264      */

265     public void removeModelListener(ModelListener ml)
266     {
267         listeners.remove(ml);
268     }
269
270     /**
271      * Forwards the given notification event to all
272      * <code>TemplateModelListeners</code> that registered
273      * themselves as listeners for this template model.
274      */

275     public void fireModelChanged() {
276         Iterator JavaDoc it = listeners.iterator();
277         ModelListener ml = null;
278         while (it.hasNext()) {
279             ml = (ModelListener) it.next();
280             ml.modelChanged(this);
281         }
282     }
283
284 }
285
Popular Tags