You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/12/18 19:42:29 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt OnEventTag.java SwtHelper.java SwtTagLibrary.java

jstrachan    2002/12/18 10:42:29

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/swt
                        SwtHelper.java SwtTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/swt
                        OnEventTag.java
  Log:
  Added support for listening to SWT events via a simple <onEvent> tag which simplifies responding to different events enormously. This SWT stuff is pretty elegant.
  
  Revision  Changes    Path
  1.2       +34 -14    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/SwtHelper.java
  
  Index: SwtHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/SwtHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SwtHelper.java	18 Dec 2002 15:27:49 -0000	1.1
  +++ SwtHelper.java	18 Dec 2002 18:42:29 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header$
  - * $Revision$
  - * $Date$
  + * /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/SwtHelper.java,v 1.1 2002/12/18 15:27:49 jstrachan Exp
  + * 1.1
  + * 2002/12/18 15:27:49
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id$
  + * SwtHelper.java,v 1.1 2002/12/18 15:27:49 jstrachan Exp
    */
   package org.apache.commons.jelly.tags.swt;
   
  @@ -73,27 +73,48 @@
    * </p>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  - * @version $Revision$
  + * @version 1.1
    */
   public class SwtHelper extends UseBeanTag {
   
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog(SwtHelper.class);
       
  -    
  +
       /**
        * Parses the comma delimited String of style codes which are or'd
        * together. The given class describes the integer static constants
  -     *  
  -     * @param text is a comma delimited text value such as "border, resize" 
  -     * @return int
  +     *
  +     * @param constantClass is the type to look for static fields
  +     * @param text is a comma delimited text value such as "border, resize"
  +     * @return the int code
        */
       public static int parseStyle(Class constantClass, String text) throws Exception {
  +        return parseStyle(constantClass, text, true);
  +    }
  +
  +    /**
  +     * Parses the comma delimited String of style codes which are or'd
  +     * together. The given class describes the integer static constants
  +     *
  +     * @param constantClass is the type to look for static fields
  +     * @param text is a comma delimited text value such as "border, resize"
  +     * @param toUpperCase is whether the text should be converted to upper case
  +     * before its compared against the reflection fields
  +     * 
  +     * @return the int code
  +     */
  +    public static int parseStyle(Class constantClass, String text, boolean toUpperCase) throws Exception {
           int answer = 0;
  -        StringTokenizer enum = new StringTokenizer(text, ",");
  -        while (enum.hasMoreTokens()) {
  -            String token = enum.nextToken().trim();
  -            answer |= getStyleCode(constantClass, token);
  +        if (text != null) {
  +            if (toUpperCase) {
  +                text = text.toUpperCase();
  +            }
  +            StringTokenizer enum = new StringTokenizer(text, ",");
  +            while (enum.hasMoreTokens()) {
  +                String token = enum.nextToken().trim();
  +                answer |= getStyleCode(constantClass, token);
  +            }
           }
           return answer;
       }
  @@ -103,7 +124,6 @@
        * valid style
        */
       public static int getStyleCode(Class constantClass,String text) throws Exception {
  -        text = text.toUpperCase();
           Field field = constantClass.getField(text);
           if (field == null) {
               log.warn( "Unknown style code: " + text +" will be ignored");
  
  
  
  1.2       +8 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/SwtTagLibrary.java
  
  Index: SwtTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/SwtTagLibrary.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SwtTagLibrary.java	18 Dec 2002 15:27:49 -0000	1.1
  +++ SwtTagLibrary.java	18 Dec 2002 18:42:29 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header$
  - * $Revision$
  - * $Date$
  + * /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/SwtTagLibrary.java,v 1.1 2002/12/18 15:27:49 jstrachan Exp
  + * 1.1
  + * 2002/12/18 15:27:49
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id$
  + * SwtTagLibrary.java,v 1.1 2002/12/18 15:27:49 jstrachan Exp
    */
   package org.apache.commons.jelly.tags.swt;
   
  @@ -81,7 +81,7 @@
    * A Jelly custom tag library that creates SWT user interfaces
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  - * @version $Revision$
  + * @version 1.1
    */
   public class SwtTagLibrary extends TagLibrary {
   
  @@ -139,6 +139,9 @@
           //registerWidgetTag( "directoryDialog", DirectoryDialog.class );
           //registerWidgetTag( "fileDialog", FileDialog.class );
           //registerWidgetTag( "fontDialog", FontDialog.class );
  +        
  +        // events
  +        registerTag("onEvent", OnEventTag.class);
       }
   
       /**
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/OnEventTag.java
  
  Index: OnEventTag.java
  ===================================================================
  /*
   * /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swt/LayoutTagSupport.java,v 1.1 2002/12/18 15:27:49 jstrachan Exp
   * 1.1
   * 2002/12/18 15:27:49
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * LayoutTagSupport.java,v 1.1 2002/12/18 15:27:49 jstrachan Exp
   */
  package org.apache.commons.jelly.tags.swt;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.widgets.Event;
  import org.eclipse.swt.widgets.Listener;
  import org.eclipse.swt.widgets.Widget;
  
  /** 
   * A tag which implements a Listener to allow events to be processed by
   * Jelly scripts
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version 1.1
   */
  public class OnEventTag extends TagSupport implements Listener {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(OnEventTag.class);
  
      private String var = "event";
      private String type; 
      private XMLOutput output;
  
      public OnEventTag() {
      }
  
      // Tag interface
      //-------------------------------------------------------------------------
      
      /**
       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
       */
      public void doTag(XMLOutput output) throws Exception {
          if (var == null) {
              throw new MissingAttributeException("var");
          }
          if (type == null) {
              throw new MissingAttributeException("type");
          }
          
          Widget widget = getParentWidget();
          if (widget == null) {
              throw new JellyException("This tag must be nested within a widget tag");
          }
  
          
          int eventType = getEventType(type);
          if (eventType == 0) {
              throw new JellyException("No event type specified, could not understand: " + type);
          }
          
          this.output = output;
          widget.addListener(eventType, this);
      }
      
      // Listener interface
      //-------------------------------------------------------------------------
      /**
       * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
       */
      public void handleEvent(Event event) {
          try {
              context.setVariable(var, event);
              invokeBody(output);
          }
          catch (Exception e) {
              log.error("Caught exception: " + e + " while processing event: " + event, e);
          }
      }
      
      // Properties
      //-------------------------------------------------------------------------
  
      /**
       * @return the parent widget which this widget will be added to.
       */
      public Widget getParentWidget() {
          WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class);
          if (tag != null) {
              return tag.getWidget();
          }
          return null;
      }
  
      /**
       * Sets the name of the variable to use to expose the event object when
       * it is fired. If not specified this defaults to "event"
       */
      public void setVar(String var) {
          this.var = var;
      }
      
      /**
       * Returns the type.
       * @return String
       */
      public String getType() {
          return type;
      }
  
      /**
       * Sets the type of the event listener to listen for.
       * 
       * @param type The type of the event to listen for
       */
      public void setType(String type) {
          this.type = type;
      }
  
      // Implementation methods
      //-------------------------------------------------------------------------
  
      /**
       * Parses the given event type String and returns the SWT event type code
       *
       * @param type is the String event type
       * @return the SWT integer event type
       */
      protected int getEventType(String type) throws Exception {
          return SwtHelper.parseStyle(SWT.class, type, false);
      }
      
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>