View Javadoc

1   /*
2   * Copyright 2002-2004 The Apache Software Foundation
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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      // now lets add this action to its parent if we have one
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        // define a variable of the event
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         // invoke the body
106         invokeBody(output);
107       }
108     }
109     catch (Exception e)
110     {
111       log.error("Caught exception processing window event: " + event, e);
112     }
113   }
114 
115 }