KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > script > DynamicScriptResource


1 /*
2  * $Id: DynamicScriptResource.java,v 1.7 2005/03/15 10:38:29 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 /**
28  * Traverses the component hierarchy of a frame and gathers the scripts of
29  * the ScriptListeners, that are attached to the components.
30  *
31  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
32  * @version $Revision: 1.7 $
33  */

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 JavaDoc {
41         try {
42             ScriptWriter visitor = new ScriptWriter(out);
43             getFrame().invite(visitor);
44         } catch (IOException JavaDoc e) {
45             throw e;
46         } catch (Exception JavaDoc e) {
47             throw new IOException JavaDoc(e.getMessage()); // UndeclaredThrowable
48
}
49     }
50
51     protected static class ScriptWriter
52             implements ComponentVisitor {
53         Device out;
54         Set JavaDoc set = new HashSet JavaDoc();
55
56         public ScriptWriter(Device out) {
57             this.out = out;
58         }
59
60         private void writeListenersFrom(SComponent component)
61                 throws IOException JavaDoc {
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 JavaDoc {
84             writeListenersFrom(component);
85             // Popup Menu needs Javascript, but is not in the component tree
86
if (component.hasComponentPopupMenu()) {
87                 writeListenersFrom(component.getComponentPopupMenu());
88             }
89         }
90
91         public void visit(SContainer container) throws Exception JavaDoc {
92             writeListenersFrom(container);
93             container.inviteEachComponent(this);
94         }
95     }
96 }
97
98
99
Popular Tags