1 16 package org.apache.cocoon.forms.event.impl; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 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 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 57 protected void callScript(WidgetEvent event) { 58 try { 59 60 HashMap values = new HashMap (2); 61 values.put("event", event); 62 65 Map objectModel = ContextHelper.getObjectModel(context); 66 67 Object 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 []{w, event}, objectModel); 76 77 } catch(RuntimeException re) { 78 throw re; 80 } catch(Exception 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 |