KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.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.avalon.framework.context.Context;
23 import org.apache.cocoon.components.ContextHelper;
24 import org.apache.cocoon.components.flow.FlowHelper;
25 import org.apache.cocoon.forms.event.ActionEvent;
26 import org.apache.cocoon.forms.event.ActionListener;
27 import org.apache.cocoon.forms.event.CreateEvent;
28 import org.apache.cocoon.forms.event.CreateListener;
29 import org.apache.cocoon.forms.event.ValueChangedEvent;
30 import org.apache.cocoon.forms.event.ValueChangedListener;
31 import org.apache.cocoon.forms.event.WidgetEvent;
32 import org.apache.cocoon.forms.formmodel.Widget;
33 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionEvent;
34 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionListener;
35 import org.apache.cocoon.forms.util.JavaScriptHelper;
36 import org.mozilla.javascript.Function;
37
38 /**
39  * Listeners built by {@link org.apache.cocoon.forms.event.impl.JavaScriptWidgetListenerBuilder}
40  *
41  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
42  * @version $Id: JavaScriptWidgetListener.java 327146 2005-10-21 10:38:37Z sylvain $
43  */

44 public abstract class JavaScriptWidgetListener {
45     
46     private Function func;
47     private Context context;
48
49     public JavaScriptWidgetListener(Function func, Context context) {
50         this.func = func;
51         this.context = context;
52     }
53     
54     /**
55      * Call the script that implements the event handler
56      */

57     protected void callScript(WidgetEvent event) {
58         try {
59             
60             HashMap JavaDoc values = new HashMap JavaDoc(2);
61             values.put("event", event);
62             //FIXME(SW) it would be nice to have "this" be the widget, but I don't know how to define
63
//the "this" object for a script (this is easy for a function)
64

65             Map JavaDoc objectModel = ContextHelper.getObjectModel(context);
66
67             // Add the view data that was passed to showForm()
68
Object JavaDoc viewData = FlowHelper.getContextObject(objectModel);
69             if (viewData != null) {
70                 values.put("viewData", viewData);
71             }
72             
73             Widget w = event.getSourceWidget();
74             
75             JavaScriptHelper.callFunction(this.func, w, new Object JavaDoc[]{w, event}, objectModel);
76             
77         } catch(RuntimeException JavaDoc re) {
78             // rethrow
79
throw re;
80         } catch(Exception JavaDoc e) {
81             throw new CascadingRuntimeException("Error invoking JavaScript event handler", e);
82         }
83     }
84     
85     public static class JSActionListener extends JavaScriptWidgetListener implements ActionListener {
86
87         public JSActionListener(Function func, Context context) {
88             super(func, context);
89         }
90
91         public void actionPerformed(ActionEvent event) {
92             super.callScript(event);
93         }
94     }
95     
96     public static class JSValueChangedListener extends JavaScriptWidgetListener implements ValueChangedListener {
97
98         public JSValueChangedListener(Function func, Context context) {
99             super(func, context);
100         }
101
102         public void valueChanged(ValueChangedEvent event) {
103             super.callScript(event);
104         }
105     }
106     
107     public static class JSCreateListener extends JavaScriptWidgetListener implements CreateListener {
108
109         public JSCreateListener(Function func, Context context) {
110             super(func, context);
111         }
112
113         public void widgetCreated(CreateEvent event) {
114             super.callScript(event);
115         }
116     }
117     
118     public static class JSTreeSelectionListener extends JavaScriptWidgetListener implements TreeSelectionListener {
119
120         public JSTreeSelectionListener(Function func, Context context) {
121             super(func, context);
122         }
123
124         public void selectionChanged(TreeSelectionEvent event) {
125             super.callScript(event);
126         }
127     }
128 }
129
Popular Tags