1 14 package org.wings.script; 15 16 import org.wings.SComponent; 17 import org.wings.SContainer; 18 import org.wings.SFrame; 19 import org.wings.io.Device; 20 import org.wings.resource.DynamicResource; 21 import org.wings.util.ComponentVisitor; 22 23 import java.io.IOException ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 34 public class DynamicScriptResource extends DynamicResource { 35 public DynamicScriptResource(SFrame frame) { 36 super(frame, "js", "application/x-javascript"); 37 } 38 39 public void write(Device out) 40 throws IOException { 41 try { 42 ScriptWriter visitor = new ScriptWriter(out); 43 getFrame().invite(visitor); 44 } catch (IOException e) { 45 throw e; 46 } catch (Exception e) { 47 throw new IOException (e.getMessage()); } 49 } 50 51 protected static class ScriptWriter 52 implements ComponentVisitor { 53 Device out; 54 Set set = new HashSet (); 55 56 public ScriptWriter(Device out) { 57 this.out = out; 58 } 59 60 private void writeListenersFrom(SComponent component) 61 throws IOException { 62 ScriptListener[] listeners = component.getScriptListeners(); 63 if (listeners.length == 0) 64 return; 65 66 for (int i = 0; i < listeners.length; i++) { 67 ScriptListener listener = listeners[i]; 68 if (listener.getScript() != null) { 69 if (set.contains(listener.getScript())) 70 continue; 71 set.add(listener.getScript()); 72 out.print("\n"); 73 out.print("// "); 74 out.print(component.getName()); 75 out.print("."); 76 out.print(listener.getEvent()); 77 out.print("\n"); 78 out.print(listener.getScript()); 79 } 80 } 81 } 82 83 public void visit(SComponent component) throws IOException { 84 writeListenersFrom(component); 85 if (component.hasComponentPopupMenu()) { 87 writeListenersFrom(component.getComponentPopupMenu()); 88 } 89 } 90 91 public void visit(SContainer container) throws Exception { 92 writeListenersFrom(container); 93 container.inviteEachComponent(this); 94 } 95 } 96 } 97 98 99 | Popular Tags |