KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > BInput


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
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: BInput.java,v 1.22 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.util.*;
23
24 import org.apache.log4j.*;
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28 import org.enhydra.barracuda.core.comp.renderer.*;
29 import org.enhydra.barracuda.core.comp.renderer.html.*;
30 import org.enhydra.barracuda.core.comp.renderer.xml.*;
31 import org.enhydra.barracuda.core.event.*;
32 import org.enhydra.barracuda.core.util.dom.DOMUtil;
33 import org.enhydra.barracuda.core.view.*;
34 import org.enhydra.barracuda.plankton.*;
35
36 /**
37  * BInput is used to manipulate the <input> element in a DOM
38  * template.
39  *
40  * <p>In most cases you will not actually need to bind the component
41  * to a view in order to use it--if you return it from a model, this
42  * will be done for you automatically. If however, you intend to use
43  * the component <em>standalone</em> (ie. manually attaching it to a
44  * specific node in the DOM) or <em>inline</em> (ie. in a toString()),
45  * then you MUST BIND IT TO A VIEW before rendering, or an error will
46  * be generated.
47  */

48 public class BInput extends BComponent {
49
50     //public vars
51
protected static final Logger logger = Logger.getLogger(BInput.class.getName());
52
53     //valid types
54
public static final String JavaDoc TEXT = "text";
55     public static final String JavaDoc PASSWORD = "password";
56     public static final String JavaDoc SUBMIT = "submit";
57     public static final String JavaDoc RESET = "reset";
58     public static final String JavaDoc FILE = "file";
59     public static final String JavaDoc HIDDEN = "hidden";
60     public static final String JavaDoc IMAGE = "image";
61     public static final String JavaDoc BUTTON = "button";
62     public static final String JavaDoc RADIO = "radio";
63     public static final String JavaDoc CHECKBOX = "checkbox";
64     
65     //private vars
66
protected List listeners = null;
67     protected String JavaDoc type = null;
68     protected String JavaDoc value = null;
69     protected boolean disableBackButton = false;
70     protected BAction baction = null; //csc_041403.2
71

72     /**
73      * @clientCardinality 0..*
74      */

75     private ListenerFactory lnkListenerFactory;
76
77     //--------------- Constructors -------------------------------
78
/**
79      * Public noargs constructor
80      */

81     public BInput() {}
82     
83     /**
84      * Public constructor which creates the component and
85      * sets the initial data.
86      *
87      * <p>Null values may be passed in for any parameters,
88      * but if you do so you will need manually provide these
89      * values (via the accessor methods) prior to actually
90      * rendering the component
91      *
92      * @param itype valid input type. May be null (indicating don't render
93      * this attribute)
94      * @param iname the name of the button, or null (indicating
95      * don't render this attribute)
96      * @param ivalue a String value for the input. May be null (indicating
97      * don't render this attribute)
98      */

99     public BInput(String JavaDoc itype, String JavaDoc iname, String JavaDoc ivalue) {
100         this(itype, iname, ivalue, null, null);
101     }
102     
103     /**
104      * Public constructor which creates the component and
105      * sets the initial data. The component is also
106      * bound to the specified view.
107      *
108      * <p>Null values may be passed in for any parameters,
109      * but if you do so you will need manually provide these
110      * values (via the accessor methods) prior to actually
111      * rendering the component
112      *
113      * @param type valid input type. May be null (indicating don't render
114      * this attribute)
115      * @param name the name of the button, or null (indicating
116      * don't render this attribute)
117      * @param value a String value for the input. May be null (indicating
118      * don't render this attribute)
119      * @param view the View the component should be bound to
120      * @param dvc the default ViewContext (opt--its presence allows the
121      * component to be rendered as markup in toString())
122      */

123     BInput(String JavaDoc itype, String JavaDoc iname, String JavaDoc ivalue, View iview, ViewContext idvc) {
124         if (idvc!=null) setDefaultViewContext(idvc);
125         if (itype!=null) setType(itype);
126         if (iname!=null) setName(iname);
127         if (ivalue!=null) setValue(ivalue);
128         if (iview!=null) setView(iview);
129     }
130     
131
132
133
134     //--------------- Renderer -----------------------------------
135
/**
136      * Default component renderer factory registrations
137      */

138     static {
139         HTMLRendererFactory rfHTML = new HTMLRendererFactory();
140         installRendererFactory(rfHTML, BInput.class, HTMLElement.class);
141         installRendererFactory(rfHTML, BInput.class, HTMLDocument.class);
142
143     }
144
145     /**
146      * HTML RendererFactory
147      */

148     static class HTMLRendererFactory implements RendererFactory {
149         public Renderer getInstance() {return new HTMLInputRenderer();}
150     }
151
152
153
154
155     //--------------- BInput --------------------------------------
156
/**
157      * Set the input type (TEXT, PASSWORD, SUBMIT, RESET, FILE, HIDDEN,
158      * IMAGE, BUTTON, RADIO, or CHECKBOX). If this value remains null,
159      * the type will default to whatever is specified in the underlying
160      * markup. If you set this value, then the type will be overridden
161      * in all the views associated with this component.
162      *
163      * @param itype an string value representing the size.
164      */

165     public void setType(String JavaDoc itype) {
166         itype = itype.toLowerCase();
167         if (itype!=null && !itype.equals(TEXT) && !itype.equals(PASSWORD) &&
168                            !itype.equals(SUBMIT) && !itype.equals(RESET) &&
169                            !itype.equals(FILE) && !itype.equals(HIDDEN) &&
170                            !itype.equals(IMAGE) && !itype.equals(BUTTON) &&
171                            !itype.equals(RADIO) && !itype.equals(CHECKBOX)) {
172             itype = TEXT;
173         }
174         type = itype;
175         invalidate();
176     }
177     
178     /**
179      * Get the type of input. May return a null if the type has not been
180      * manually specified.
181      *
182      * @return the component type
183      */

184     public String JavaDoc getType() {
185         return type;
186     }
187
188     //csc_031003.1 - added
189
/**
190      * Convenience mechanism to set the value for this input using an Object.
191      *
192      * @param ivalue the value object
193      */

194     public void setValue(Object JavaDoc ivalue) {
195         setValue(ivalue!=null ? ivalue.toString() : (String JavaDoc) null);
196     }
197     
198     /**
199      * Set the value for this input. In most cases, the value object will
200      * simply be rendered as the 'value' attribute. If this value remains null,
201      * the type will default to whatever is specified in the underlying
202      * markup. If you set this value, then the type will be overridden in all
203      * the views associated with this component.
204      *
205      * @param ivalue the value object
206      */

207     public void setValue(String JavaDoc ivalue) {
208         value = ivalue;
209         invalidate();
210     }
211     
212     /**
213      * Get the value for this input. May return a null if the type has not been
214      * manually specified.
215      *
216      * @return the value for this input
217      */

218     public String JavaDoc getValue() {
219         return value;
220     }
221
222     //csc_041403.2 - added
223
/**
224      * Specify an action for this component (rather than adding an even listener)
225      *
226      * @param ibaction the action to be fired when the BSelect is activated on the client
227      */

228     public void setAction(BAction ibaction) {
229         baction = ibaction;
230     }
231     
232     //csc_041403.2 - added
233
/**
234      * Returns the action associated with this component (if any)
235      *
236      * @return the action associated with this component (if any)
237      */

238     public BAction getAction() {
239         return baction;
240     }
241     
242     /**
243      * Add an event listener to this component.
244      *
245      * @param lf the event listener to be added
246      */

247     public void addEventListener(ListenerFactory lf) {
248         addEventListener(lf, false);
249     }
250     
251     /**
252      * Add an event listener to this component.
253      *
254      * @param lf the event listener to be added
255      * @param idisableBackButton true if the back button should be
256      * disabled when the action occurs
257      */

258     public void addEventListener(ListenerFactory lf, boolean idisableBackButton) {
259         if (lf==null) return;
260         disableBackButton = idisableBackButton;
261         if (listeners==null) listeners = new ArrayList(5);
262         listeners.add(lf);
263         invalidate();
264     }
265     
266     /**
267      * Remove an event listener from this component
268      *
269      * @param lf the event listener to be removed
270      */

271     public void removeEventListener(ListenerFactory lf) {
272         if (lf==null) return;
273         if (listeners==null) return;
274         listeners.remove(lf);
275         invalidate();
276     }
277
278     /**
279      * Here in the pre-render phase we actually add
280      * BAction step children for any of the listeners
281      * that might have been added to this component
282      */

283     protected void preRender(ViewContext vc, int depth) {
284         //add in our BAction as a step child
285
if (baction!=null) this.addStepChild(baction, true); //csc_041403.2
286

287         //we want to actually add proxy components as step children
288
//for any event listeners we might have to support
289
if (listeners!=null) {
290             //run through our list of listeners
291
Iterator it = listeners.iterator();
292             while (it.hasNext()) {
293                 ListenerFactory lf = (ListenerFactory) it.next();
294             
295 //csc_041403.2_start
296
/*
297 The problem with this is that its creating a separate BAction for each view, and it really
298 doesn't need to do that...just create one and add in as many views as there are
299                 //run through our list of views
300                 Iterator it2 = getViews().iterator();
301                 while (it2.hasNext()) {
302                     View v = (View) it2.next();
303                     BAction baComp = new BAction();
304                     baComp.setView(v);
305                     baComp.setDisableBackButton(disableBackButton);
306                     baComp.addEventListener(lf);
307                     this.addStepChild(baComp, true);
308                 }
309                 
310                 //csc_101001.1 - added
311                 //run through our list of temp views too (these
312                 //will be set if the component was returned from a
313                 //model without being pre-bound to a view)
314                 if (tempViews!=null) {
315                     it2 = tempViews.iterator();
316                     while (it2.hasNext()) {
317                         View v = (View) it2.next();
318                         BAction baComp = new BAction();
319                         baComp.setView(v);
320                         baComp.setDisableBackButton(disableBackButton);
321                         baComp.addEventListener(lf);
322                         this.addStepChild(baComp, true);
323                     }
324                 }
325 */

326                 BAction baComp = new BAction();
327                 baComp.setDisableBackButton(disableBackButton);
328                 baComp.addEventListener(lf);
329                 this.addStepChild(baComp, true);
330 //csc_041403.2_end
331
}
332         }
333     }
334
335     /**
336      * Render a specific view for the component.
337      *
338      * @param view View to be rendered
339      * @param vc ViewContext for the client view
340      * @param vc ViewContext for the client view
341      * @throws RenderException if the particular View is not supported
342      */

343 /*
344 //021102.3_csc - removed, because now its in BComponent
345     protected void renderView (View view, ViewContext vc, int depth) throws RenderException {
346         if (logger.isInfoEnabled()) logger.info("rendering comp:"+this.toRef()+" view:"+view);
347
348         //actually render the view according to known interfaces
349         try {
350             Renderer r = getRenderer(view);
351             r.renderComponent(this, view, vc);
352         } catch (DOMException e) {
353             logger.warn("DOM Error:", e);
354             throw new DOMAccessException("Error rendering component in view:"+e, e);
355         }
356     }
357 */

358 }
Popular Tags