KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > event > impl > JavaScriptWidgetListenerBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.event.impl;
17
18 import org.apache.avalon.framework.context.Context;
19 import org.apache.avalon.framework.context.ContextException;
20 import org.apache.avalon.framework.context.Contextualizable;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.forms.event.ActionListener;
23 import org.apache.cocoon.forms.event.CreateListener;
24 import org.apache.cocoon.forms.event.ValueChangedListener;
25 import org.apache.cocoon.forms.event.WidgetListener;
26 import org.apache.cocoon.forms.event.WidgetListenerBuilder;
27 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionListener;
28 import org.apache.cocoon.forms.util.JavaScriptHelper;
29 import org.mozilla.javascript.Function;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Builds a {@link WidgetListener} based on a JavaScript snippet.
34  * <p>
35  * The syntax for this listener is as follows :
36  * <pre>
37  * &lt;fd:javascript&gt;
38  * var widget = event.sourceWidget;
39  * sourceWidget.setValue("Yeah");
40  * &lt;/fd:javascript&gt;
41  * </pre>
42  * As shown above, the event that fired this listener is published as the <code>event</code>
43  * variable.
44  *
45  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
46  * @version $Id: JavaScriptWidgetListenerBuilder.java 327146 2005-10-21 10:38:37Z sylvain $
47  */

48 public class JavaScriptWidgetListenerBuilder implements WidgetListenerBuilder, ThreadSafe, Contextualizable {
49
50     private Context context;
51
52     public void contextualize(Context context) throws ContextException {
53         this.context = context;
54     }
55
56     public WidgetListener buildListener(Element JavaDoc element, Class JavaDoc listenerClass) throws Exception JavaDoc {
57
58         Function func = JavaScriptHelper.buildFunction(element, "handleEvent", new String JavaDoc[]{"widget", "event"});
59
60         if (listenerClass == ActionListener.class) {
61             return new JavaScriptWidgetListener.JSActionListener(func, context);
62         } else if (listenerClass == CreateListener.class) {
63             return new JavaScriptWidgetListener.JSCreateListener(func, context);
64         } else if (listenerClass == ValueChangedListener.class) {
65             return new JavaScriptWidgetListener.JSValueChangedListener(func, context);
66         } else if (listenerClass == TreeSelectionListener.class) {
67             return new JavaScriptWidgetListener.JSTreeSelectionListener(func, context);
68         } else {
69             throw new Exception JavaDoc("Unkonwn event class: " + listenerClass);
70         }
71     }
72 }
73
Popular Tags