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.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      // now lets add this action to its parent if we have one
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        // define a variable of the event
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         // invoke the body
107         invokeBody(output);
108       }
109     }
110     catch (Exception e)
111     {
112       log.error("Caught exception processing window event: " + event, e);
113     }
114   }
115 
116 }