KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > event > impl > JavaScriptWidgetListener


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 java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.CascadingRuntimeException;
22 import org.apache.cocoon.components.CocoonComponentManager;
23 import org.apache.cocoon.components.flow.FlowHelper;
24 import org.apache.cocoon.woody.event.ActionEvent;
25 import org.apache.cocoon.woody.event.ActionListener;
26 import org.apache.cocoon.woody.event.ValueChangedEvent;
27 import org.apache.cocoon.woody.event.ValueChangedListener;
28 import org.apache.cocoon.woody.event.WidgetEvent;
29 import org.apache.cocoon.woody.util.JavaScriptHelper;
30 import org.mozilla.javascript.Script;
31
32 /**
33  * Listeners built by {@link org.apache.cocoon.woody.event.impl.JavaScriptWidgetListenerBuilder}
34  *
35  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
36  * @version CVS $Id: JavaScriptWidgetListener.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38 public abstract class JavaScriptWidgetListener {
39     
40     private Script script;
41     
42     public JavaScriptWidgetListener(Script script) {
43         this.script = script;
44     }
45     
46     /**
47      * Call the script that implements the event handler
48      */

49     protected void callScript(WidgetEvent event) {
50         try {
51             
52             HashMap JavaDoc values = new HashMap JavaDoc(2);
53             values.put("event", event);
54             
55             // FIXME: remove this ugly hack and get the request from the Avalon context once
56
// listener builder are real components
57
Map JavaDoc objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel();
58             
59             // Add the biz data that was passed to showForm()
60
Object JavaDoc viewData = FlowHelper.getContextObject(objectModel);
61             if (viewData != null) {
62                 values.put("viewData", viewData);
63             }
64             
65             JavaScriptHelper.execScript(this.script, values, objectModel);
66             
67         } catch(RuntimeException JavaDoc re) {
68             // rethrow
69
throw re;
70         } catch(Exception JavaDoc e) {
71             throw new CascadingRuntimeException("Error invoking JavaScript event handler", e);
72         }
73     }
74     
75     public static class JSActionListener extends JavaScriptWidgetListener implements ActionListener {
76
77         public JSActionListener(Script script) {
78             super(script);
79         }
80
81         public void actionPerformed(ActionEvent event) {
82             super.callScript(event);
83         }
84     }
85     
86     public static class JSValueChangedListener extends JavaScriptWidgetListener implements ValueChangedListener {
87
88         public JSValueChangedListener(Script script) {
89             super(script);
90         }
91
92         public void valueChanged(ValueChangedEvent event) {
93             super.callScript(event);
94         }
95     }
96 }
97
Popular Tags