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.swt;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.MissingAttributeException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.commons.jelly.XMLOutput;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.widgets.Event;
26  import org.eclipse.swt.widgets.Listener;
27  import org.eclipse.swt.widgets.Widget;
28  
29  /***
30   * A tag which implements a Listener to allow events to be processed by
31   * Jelly scripts
32   *
33   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
34   * @version 1.1
35   */
36  public class OnEventTag extends TagSupport implements Listener {
37  
38      /*** The Log to which logging calls will be made. */
39      private static final Log log = LogFactory.getLog(OnEventTag.class);
40  
41      private String var = "event";
42      private String type;
43      private XMLOutput output;
44  
45      public OnEventTag() {
46      }
47  
48      // Tag interface
49      //-------------------------------------------------------------------------
50  
51      /***
52       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
53       */
54      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
55          if (var == null) {
56              throw new MissingAttributeException("var");
57          }
58          if (type == null) {
59              throw new MissingAttributeException("type");
60          }
61  
62          Widget widget = getParentWidget();
63          if (widget == null) {
64              throw new JellyTagException("This tag must be nested within a widget tag");
65          }
66  
67  
68          int eventType = getEventType(type);
69          if (eventType == 0) {
70              throw new JellyTagException("No event type specified, could not understand: " + type);
71          }
72  
73          this.output = output;
74          widget.addListener(eventType, this);
75      }
76  
77      // Listener interface
78      //-------------------------------------------------------------------------
79      /***
80       * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
81       */
82      public void handleEvent(Event event) {
83          try {
84              context.setVariable(var, event);
85              invokeBody(output);
86          }
87          catch (Exception e) {
88              log.error("Caught exception: " + e + " while processing event: " + event, e);
89          }
90      }
91  
92      // Properties
93      //-------------------------------------------------------------------------
94  
95      /***
96       * @return the parent widget which this widget will be added to.
97       */
98      public Widget getParentWidget() {
99          WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class);
100         if (tag != null) {
101             return tag.getWidget();
102         }
103         return null;
104     }
105 
106     /***
107      * Sets the name of the variable to use to expose the event object when
108      * it is fired. If not specified this defaults to "event"
109      */
110     public void setVar(String var) {
111         this.var = var;
112     }
113 
114     /***
115      * Returns the type.
116      * @return String
117      */
118     public String getType() {
119         return type;
120     }
121 
122     /***
123      * Sets the type of the event listener to listen for.
124      *
125      * @param type The type of the event to listen for
126      */
127     public void setType(String type) {
128         this.type = type;
129     }
130 
131     // Implementation methods
132     //-------------------------------------------------------------------------
133 
134     /***
135      * Parses the given event type String and returns the SWT event type code
136      *
137      * @param type is the String event type
138      * @return the SWT integer event type
139      */
140     protected int getEventType(String type) throws JellyTagException {
141         return SwtHelper.parseStyle(SWT.class, type, false);
142     }
143 
144 }