KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > scripting > BScript


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: BScript.java,v 1.7 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp.scripting;
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.*;
29 import org.enhydra.barracuda.core.comp.renderer.*;
30 import org.enhydra.barracuda.core.comp.renderer.html.*;
31 import org.enhydra.barracuda.core.comp.renderer.xml.*;
32 import org.enhydra.barracuda.core.util.dom.DOMUtil;
33 import org.enhydra.barracuda.core.view.*;
34 import org.enhydra.barracuda.plankton.*;
35 import org.enhydra.barracuda.plankton.data.*;
36
37
38 //csc_102201.1 - created
39
/**
40  * BScript is used to attach a javascript command to a DOM element
41  * attribute.
42  *
43  * <p>In most cases you will not actually need to bind the component
44  * to a view in order to use it--if you return it from a model, this
45  * will be done for you automatically. If however, you intend to use
46  * the component <em>standalone</em> (ie. manually attaching it to a
47  * specific node in the DOM) or <em>inline</em> (ie. in a toString()),
48  * then you MUST BIND IT TO A VIEW before rendering, or an error will
49  * be generated.
50  */

51 public class BScript extends BComponent {
52
53     //public vars
54
protected static final Logger logger = Logger.getLogger(BScript.class.getName());
55
56     //how should the command be inserted
57
public static final int REPLACE = 0;
58     public static final int PREPEND = 1;
59     public static final int APPEND = 2;
60
61     //target actions
62
//...std events
63
public static final String JavaDoc ON_CLICK = "onclick";
64     public static final String JavaDoc ON_DBL_CLICK = "ondblclick";
65     public static final String JavaDoc ON_MOUSE_DOWN = "onmousedown";
66     public static final String JavaDoc ON_MOUSE_UP = "onmouseup";
67     public static final String JavaDoc ON_MOUSE_OVER = "onmouseover";
68     public static final String JavaDoc ON_MOUSE_MOVE = "onmousemove";
69     public static final String JavaDoc ON_MOUSE_OUT = "onmouseout";
70     public static final String JavaDoc ON_KEY_PRESS = "onkeypress";
71     public static final String JavaDoc ON_KEY_DOWN = "onkeydown";
72     public static final String JavaDoc ON_KEY_UP = "onkeyup";
73     //..<body> specific
74
public static final String JavaDoc ON_LOAD = "onload";
75     public static final String JavaDoc ON_UNLOAD = "onunload";
76     //..<a>,<area>,<label>,<input>,<select>,<textarea>,<button> specific
77
public static final String JavaDoc ON_FOCUS = "onfocus";
78     public static final String JavaDoc ON_BLUR = "onblur";
79     //..<input>,<textarea> specific
80
public static final String JavaDoc ON_SELECT = "onselect";
81     //..<input>,<select> specific
82
public static final String JavaDoc ON_CHANGE = "onchange";
83     //..<form> specific
84
public static final String JavaDoc ON_SUBMIT = "onsubmit";
85     public static final String JavaDoc ON_RESET = "onreset";
86
87     //private vars
88
protected String JavaDoc jscmd = null;
89     protected String JavaDoc jsattr = null;
90     protected int mode = REPLACE;
91     protected List resources = null;
92
93     //--------------- Constructors -------------------------------
94
/**
95      * Public noargs constructor
96      */

97     public BScript() {}
98
99     /**
100      * Public constructor which creates the component. When rendered,
101      * it will take the specified command and REPLACE it in the target
102      * scripting attribute
103      *
104      * <p>You should generally only use this constructor when returning
105      * BScript from a Model, as the model components will automatically
106      * bind the component to a view for you. If you use this constructor
107      * in some other situation, you should manually bind the component
108      * to the proper view.
109      *
110      * @param jsattr the target script attribute
111      * @param jscmd the script command
112      */

113     public BScript(String JavaDoc jsattr, String JavaDoc jscmd) {
114         this(jsattr, jscmd, REPLACE);
115     }
116
117     /**
118      * Public constructor which creates the component. When rendered,
119      * it will take the specified command and render it in the target
120      * scripting attribute, using the specified replacement mode.
121      *
122      * @param jsattr the target script attribute
123      * @param jscmd the script command
124      * @param mode the cmd replacement mode (REPLACE, PREPEND, or APPEND)
125      */

126     public BScript(String JavaDoc jsattr, String JavaDoc jscmd, int mode) {
127         this (jsattr, jscmd, mode, null);
128     }
129
130     /**
131      * Public constructor which creates the component. When rendered,
132      * it will take the specified command and render it in the target
133      * scripting attribute, using the specified replacement mode.
134      *
135      * @param jsattr the target script attribute
136      * @param jscmd the script command
137      * @param mode the cmd replacement mode (REPLACE, PREPEND, or APPEND)
138      * @param resource any necessary resource scripts
139      */

140     public BScript(String JavaDoc jsattr, String JavaDoc jscmd, int mode, String JavaDoc resource) {
141         if (jsattr!=null) setAttr(jsattr);
142         if (jscmd!=null) setCmd(jscmd);
143         setMode(mode);
144         if (resource!=null) addResource(resource);
145     }
146
147     //--------------- Renderer -----------------------------------
148
/**
149      * Default component renderer factory registrations
150      */

151     static {
152         HTMLRendererFactory rfHTML = new HTMLRendererFactory();
153         installRendererFactory(rfHTML, BScript.class, HTMLElement.class);
154         installRendererFactory(rfHTML, BScript.class, HTMLDocument.class);
155 /*
156         WMLRendererFactory rfWML = new WMLRendererFactory();
157         installRendererFactory(rfWML, BScript.class, HTMLElement.class);
158         installRendererFactory(rfWML, BScript.class, HTMLDocument.class);
159 */

160     }
161
162     /**
163      * HTML RendererFactory
164      */

165     static class HTMLRendererFactory implements RendererFactory {
166         public Renderer getInstance() {return new HTMLScriptRenderer();}
167     }
168
169
170
171     //--------------- BComponent ---------------------------------
172
/**
173      * Specify the JavaScript attr the command should be bound to
174      *
175      * @param ijsattr the JavaScript attr the command should be bound to
176      */

177     public void setAttr(String JavaDoc ijsattr) {
178         jsattr = ijsattr;
179         invalidate();
180     }
181
182     /**
183      * Get the JavaScript attr the command is be bound to
184      *
185      * @return the JavaScript attr the command is be bound to
186      */

187     public String JavaDoc getAttr() {
188         return jsattr;
189     }
190
191     /**
192      * Set the JavaScript command for this particular component
193      *
194      * @param ijscmd the JavaScript command that backs this component
195      */

196     public void setCmd(String JavaDoc ijscmd) {
197         jscmd = ijscmd;
198         invalidate();
199     }
200
201     /**
202      * Get the JavaScript command for this particular component
203      *
204      * @return the JavaScript command for this particular component
205      */

206     public String JavaDoc getCmd() {
207         return jscmd;
208     }
209
210     /**
211      * Set the mode for the action (defaults to REPLACE). In most cases,
212      * you will simply REPLACE whatever the current cmd is in the template.
213      * Sometimes, however, you may want to keep what's already there. In such
214      * a situation, you would specify either APPEND (tack it on to what's
215      * already there) or PREPEND (stick it in front of what's there)
216      *
217      * @param imode the cmd replacement mode
218      */

219     public void setMode(int imode) {
220         if (imode!=PREPEND && imode!=APPEND) imode = REPLACE;
221         mode = imode;
222         invalidate();
223     }
224
225     /**
226      * Get the replacement mode for this script
227      *
228      * @return the replacement mode for this script
229      */

230     public int getMode() {
231         return mode;
232     }
233
234     /**
235      * Add a script resource (resources specified will automatically
236      * create BScriptResource delegates at render time)
237      *
238      * @param resource a script resource
239      */

240     public void addResource(String JavaDoc resource) {
241         if (resources==null) resources = new ArrayList();
242         resources.add(resource);
243         invalidate();
244     }
245
246     /**
247      * Remove a script resource
248      *
249      * @param resource a script resource
250      */

251     public void removeResource(String JavaDoc resource) {
252         if (resources==null) return;
253         resources.remove(resource);
254         invalidate();
255     }
256
257     /**
258      * Get a list of script resource
259      *
260      * @return a list of script resources
261      */

262     public List getResources() {
263         return resources;
264     }
265
266
267     /**
268      * Render a specific view for the component.
269      *
270      * @param view View to be rendered
271      * @param vc ViewContext for the client view
272      * @throws RenderException if the particular View is not supported
273      * @param list a List of all the views for this component
274      */

275 /*
276 //021102.3_csc - removed, because now its in BComponent
277     protected void renderView (View view, ViewContext vc, int depth) throws RenderException {
278         if (logger.isInfoEnabled()) logger.info("rendering view: "+view);
279
280         //actually render the view according to known interfaces
281         try {
282             Renderer r = getRenderer(view);
283             r.renderComponent(this, view, vc);
284         } catch (DOMException e) {
285             logger.warn("DOM Error:", e);
286             throw new DOMAccessException("Error rendering component in view:"+e, e);
287         }
288     }
289 */

290     /**
291      * Get a String representation of the component
292      */

293     public String JavaDoc toString() {
294         return jsattr+"=\""+jscmd+"\"";
295     }
296
297 }
Popular Tags