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