KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.event.impl;
17
18 import org.apache.cocoon.woody.event.ActionListener;
19 import org.apache.cocoon.woody.event.ValueChangedListener;
20 import org.apache.cocoon.woody.event.WidgetListener;
21 import org.apache.cocoon.woody.event.WidgetListenerBuilder;
22 import org.apache.cocoon.woody.util.JavaScriptHelper;
23 import org.mozilla.javascript.Script;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * Builds a {@link WidgetListener} based on a JavaScript snippet.
28  * <p>
29  * The syntax for this listener is as follows :
30  * <pre>
31  * &lt;javascript&gt;
32  * var widget = event.sourceWidget;
33  * sourceWidget.setValue("Yeah");
34  * &lt;/javascript&gt;
35  * </pre>
36  * As shown above, the event that fired this listener is published as the <code>event</code>
37  * variable.
38  *
39  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
40  * @version CVS $Id: JavaScriptWidgetListenerBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public class JavaScriptWidgetListenerBuilder implements WidgetListenerBuilder {
43
44     public static final JavaScriptWidgetListenerBuilder INSTANCE = new JavaScriptWidgetListenerBuilder();
45
46     public WidgetListener buildListener(Element JavaDoc element, Class JavaDoc listenerClass) throws Exception JavaDoc {
47
48         Script script = JavaScriptHelper.buildScript(element);
49
50         if (listenerClass == ActionListener.class) {
51             return new JavaScriptWidgetListener.JSActionListener(script);
52         } else if (listenerClass == ValueChangedListener.class) {
53             return new JavaScriptWidgetListener.JSValueChangedListener(script);
54         } else {
55             throw new Exception JavaDoc("Unkonwn event class: " + listenerClass);
56         }
57     }
58 }
59
Popular Tags