1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swing;
17
18 import java.awt.event.WindowEvent;
19 import java.awt.event.WindowListener;
20
21 import org.apache.commons.jelly.JellyTagException;
22 import org.apache.commons.jelly.Script;
23 import org.apache.commons.jelly.TagSupport;
24 import org.apache.commons.jelly.XMLOutput;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /***
29 * Creates a WindowListener which is attached to its parent window control which will invoke
30 * named Jelly scripts as window events are fired, or will invoke its body if there is no script
31 * specified for the named event type.
32 *
33 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
34 * @version $Revision: 155420 $
35 */
36 public class WindowListenerTag extends TagSupport {
37
38 /*** The Log to which logging calls will be made. */
39 private static final Log log = LogFactory.getLog(WindowListenerTag.class);
40
41 private String var;
42 private Script activated;
43 private Script closed;
44 private Script closing;
45 private Script deactivated;
46 private Script deiconified;
47 private Script iconified;
48 private Script opened;
49
50 public WindowListenerTag() {
51 }
52
53
54
55 public void doTag(final XMLOutput output) throws JellyTagException {
56
57
58 ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
59 if ( tag != null ) {
60 WindowListener listener = new WindowListener() {
61 public void windowActivated(WindowEvent e) {
62 invokeScript( output, e, activated );
63 }
64
65 public void windowClosed(WindowEvent e) {
66 invokeScript( output, e, closed );
67 }
68
69 public void windowClosing(WindowEvent e) {
70 invokeScript( output, e, closing );
71 }
72
73 public void windowDeactivated(WindowEvent e) {
74 invokeScript( output, e, deactivated );
75 }
76
77 public void windowDeiconified(WindowEvent e) {
78 invokeScript( output, e, deiconified );
79 }
80
81 public void windowIconified(WindowEvent e) {
82 invokeScript( output, e, iconified );
83 }
84
85 public void windowOpened(WindowEvent e) {
86 invokeScript( output, e, opened );
87 }
88 };
89 tag.addWindowListener(listener);
90 }
91 }
92
93
94
95
96
97 /***
98 * Sets the name of the variable to use to expose the Event object
99 */
100 public void setVar(String var) {
101 this.var = var;
102 }
103
104 /***
105 * Sets the Script to be executed when the window is activated.
106 */
107 public void setActivated(Script activated) {
108 this.activated = activated;
109 }
110
111 /***
112 * Sets the Script to be executed when the window is closed.
113 */
114 public void setClosed(Script closed) {
115 this.closed = closed;
116 }
117
118 /***
119 * Sets the Script to be executed when the window is closing.
120 */
121 public void setClosing(Script closing) {
122 this.closing = closing;
123 }
124
125 /***
126 * Sets the Script to be executed when the window is deactivated.
127 */
128 public void setDeactivated(Script deactivated) {
129 this.deactivated = deactivated;
130 }
131
132 /***
133 * Sets the Script to be executed when the window is deiconified.
134 */
135 public void setDeiconified(Script deiconified) {
136 this.deiconified = deiconified;
137 }
138
139 /***
140 * Sets the Script to be executed when the window is iconified.
141 */
142 public void setIconified(Script iconified) {
143 this.iconified = iconified;
144 }
145
146 /***
147 * Sets the Script to be executed when the window is opened.
148 */
149 public void setOpened(Script opened) {
150 this.opened = opened;
151 }
152
153
154
155
156
157 protected void invokeScript(XMLOutput output, WindowEvent event, Script script) {
158 if ( var != null ) {
159
160 context.setVariable(var, event);
161 }
162
163 try {
164 if ( script != null ) {
165 script.run(context, output );
166 }
167 else {
168
169 invokeBody(output);
170 }
171 }
172 catch (Exception e) {
173 log.error( "Caught exception processing window event: " + event, e );
174 }
175 }
176
177 }