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.FocusEvent;
19 import java.awt.event.FocusListener;
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 public class FocusListenerTag extends TagSupport
29 {
30 protected static final Log log = LogFactory.getLog(FocusListenerTag.class);
31
32 protected String var;
33 protected Script gained;
34 protected Script lost;
35
36 /***
37 *
38 */
39 public FocusListenerTag()
40 {
41 super();
42 }
43
44 /***
45 * @param var
46 */
47 public void setVar(String var)
48 {
49 this.var = var;
50 }
51
52 /***
53 * @param gained
54 */
55 public void setGained(Script gained)
56 {
57 this.gained = gained;
58 }
59
60 /***
61 * @param lost
62 */
63 public void setLost(Script lost)
64 {
65 this.lost = lost;
66 }
67
68 public void doTag(final XMLOutput output) throws JellyTagException
69 {
70
71 ComponentTag tag = (ComponentTag)findAncestorWithClass(ComponentTag.class);
72 if (tag != null)
73 {
74 FocusListener listener = new FocusListener()
75 {
76 public void focusGained(FocusEvent e)
77 {
78 invokeScript(output, e, gained);
79 }
80
81 public void focusLost(FocusEvent e)
82 {
83 invokeScript(output, e, lost);
84 }
85 };
86 tag.addFocusListener(listener);
87 }
88 }
89
90 protected void invokeScript(XMLOutput output, FocusEvent event, Script script)
91 {
92 if (var != null)
93 {
94
95 context.setVariable(var, event);
96 }
97
98 try
99 {
100 if (script != null)
101 {
102 script.run(context, output);
103 }
104 else
105 {
106
107 invokeBody(output);
108 }
109 }
110 catch (Exception e)
111 {
112 log.error("Caught exception processing window event: " + event, e);
113 }
114 }
115
116 }