1 16 package org.apache.cocoon.woody.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.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 38 public abstract class JavaScriptWidgetListener { 39 40 private Script script; 41 42 public JavaScriptWidgetListener(Script script) { 43 this.script = script; 44 } 45 46 49 protected void callScript(WidgetEvent event) { 50 try { 51 52 HashMap values = new HashMap (2); 53 values.put("event", event); 54 55 Map objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel(); 58 59 Object 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 re) { 68 throw re; 70 } catch(Exception 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 |