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/09/24 19:30:34 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing TitledBorderTag.java BorderTagSupport.java SwingTagLibrary.java ComponentTag.java FontTag.java

jstrachan    2002/09/24 10:30:34

  Modified:    jelly/src/test/org/apache/commons/jelly/swing example.jelly
               jelly/src/java/org/apache/commons/jelly/tags/swing
                        SwingTagLibrary.java ComponentTag.java FontTag.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/swing
                        TitledBorderTag.java BorderTagSupport.java
  Log:
  Added support for <border> tags via an abstract base class that we can use to implement all the various different kinds of borders.
  
  Right now I've implemented the <titledBorder> but the others should be fairly simple also, implemented along a similar pattern.
  
  Revision  Changes    Path
  1.6       +1 -0      jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/swing/example.jelly
  
  Index: example.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/swing/example.jelly,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- example.jelly	24 Sep 2002 16:48:54 -0000	1.5
  +++ example.jelly	24 Sep 2002 17:30:33 -0000	1.6
  @@ -43,6 +43,7 @@
       
   		<splitPane>    
         <panel>
  +      	<titledBorder title="Sample Border Title"/>
           <label text="Enter your name"/>
           <textField text="James" font="${bigfont}"/>
           <button>
  
  
  
  1.7       +5 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/SwingTagLibrary.java
  
  Index: SwingTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/SwingTagLibrary.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SwingTagLibrary.java	24 Sep 2002 16:48:54 -0000	1.6
  +++ SwingTagLibrary.java	24 Sep 2002 17:30:33 -0000	1.7
  @@ -110,6 +110,11 @@
           registerTag( "action", ActionTag.class );
           registerTag( "font", FontTag.class );
           registerTag( "windowListener", WindowListenerTag.class );
  +        
  +        // the border tags...
  +        registerTag( "titledBorder", TitledBorderTag.class );
  +        
  +        // the layout tags...
       }
   
       /** Creates a new script to execute the given tag name and attributes */
  
  
  
  1.7       +12 -0     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/ComponentTag.java
  
  Index: ComponentTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/ComponentTag.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ComponentTag.java	24 Sep 2002 16:48:54 -0000	1.6
  +++ ComponentTag.java	24 Sep 2002 17:30:33 -0000	1.7
  @@ -72,6 +72,7 @@
   import java.util.Map;
   
   import javax.swing.*;
  +import javax.swing.border.Border;
   
   import org.apache.commons.beanutils.BeanUtils;
   import org.apache.commons.beanutils.ConvertUtils;
  @@ -171,6 +172,17 @@
           if ( component != null ) {
               // lets just try set the 'font' property
               BeanUtils.setProperty( component, "font", font );
  +        }
  +    }
  +
  +    /**
  +     * Sets the Border of this component
  +     */
  +    public void setBorder(Border border) throws Exception {
  +        Component component = getComponent();
  +        if ( component != null ) {
  +            // lets just try set the 'border' property
  +            BeanUtils.setProperty( component, "border", border );
           }
       }
   
  
  
  
  1.2       +5 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/FontTag.java
  
  Index: FontTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/FontTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FontTag.java	24 Sep 2002 16:48:54 -0000	1.1
  +++ FontTag.java	24 Sep 2002 17:30:33 -0000	1.2
  @@ -91,14 +91,18 @@
   
       // Tag interface
       //-------------------------------------------------------------------------                    
  +/*    
  + * maybe do some type conversions or name mapping code...
  + * 
       public void setAttribute(String name, Object value) {
           if (name.equals("size")) {
  -            super.setAttribute(name, ConvertUtils.value);
  +            super.setAttribute(name, ConvertUtils.convert(Integer.class, value));
           }
           else {
               super.setAttribute(name, value);
           }
       }
  +*/    
       
       public void doTag(final XMLOutput output) throws Exception {
           Map attributes = getAttributes();
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/TitledBorderTag.java
  
  Index: TitledBorderTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $
   * $Revision: 1.7 $
   * $Date: 2002/05/17 15:18:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-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/>.
   * 
   * $Id: DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.swing;
  
  import java.awt.Color;
  import java.awt.Font;
  import javax.swing.BorderFactory;
  import javax.swing.border.Border;
  import javax.swing.border.TitledBorder;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.XMLOutput;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /** 
   * Creates a titled border.
   * The border will either be exported as a variable defined by the 'var' attribute
   * or will be set on the parent widget's border property
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.7 $
   */
  public class TitledBorderTag extends BorderTagSupport {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(TitledBorderTag.class);
  
      private String title;
      private String titleJustification;
      private String titlePosition;
      private Border border;
      private Font font;
      private Color color;
  
  
      // Tag interface
      //-------------------------------------------------------------------------                    
      public void doTag(final XMLOutput output) throws Exception {
          if ( title == null) {
              throw new MissingAttributeException("title");
          }
          super.doTag(output);
      }
      
      // Properties
      //-------------------------------------------------------------------------                    
  
      /**
       * Sets the color of the title for this border. Can be set via a nested <color> tag.
       */
      public void setColor(Color color) {
          this.color = color;
      }
  
      /**
       * Sets the Font to be used by the title. Can be set via a nested <font> tag.
       */
      public void setFont(Font font) {
          this.font = font;
      }
  
      /**
       * Sets the title text for this border.
       */
      public void setTitle(String title) {
          this.title = title;
      }
  
      /**
       * Sets the justification of the title. The String is case insensitive.
       * Possible values are {LEFT, CENTER, RIGHT, LEADING, TRAILING}
       */
      public void setTitleJustification(String titleJustification) {
          this.titleJustification = titleJustification;
      }
  
      /**
       * Sets the position of the title. The String is case insensitive.
       * Possible values are {ABOVE_TOP, TOP, BELOW_TOP, ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM}
       */
      public void setTitlePosition(String titlePosition) {
          this.titlePosition = titlePosition;
      }
  
  
  
      // Implementation methods
      //-------------------------------------------------------------------------                    
      
      /**
       * Factory method to create a new Border instance.
       */
      protected Border createBorder() throws Exception {
          if (border != null) {
              if (titleJustification != null && titlePosition != null) {
                  int justification = asTitleJustification(titleJustification);
                  int position = asTitlePosition(titlePosition);
                  
                  if (font != null) {
                      if (color != null) {
                          return BorderFactory.createTitledBorder(border, title, justification, position, font, color);
                      }
                      else {
                          return BorderFactory.createTitledBorder(border, title, justification, position, font);
                      }
                  }
                  return BorderFactory.createTitledBorder(border, title, justification, position);
              }
              return BorderFactory.createTitledBorder(border, title);
          }
          return BorderFactory.createTitledBorder(title);
      }
  
      /** 
       * @return the enumeration for the title justification
       */
      protected int asTitleJustification(String text) {    
          if (text.equalsIgnoreCase("LEFT")) {
              return TitledBorder.LEFT;
          }
          else if (text.equalsIgnoreCase("CENTER")) {
              return TitledBorder.CENTER;
          }
          else if (text.equalsIgnoreCase("RIGHT")) {
              return TitledBorder.RIGHT;
          }
          else if (text.equalsIgnoreCase("LEADING")) {
              return TitledBorder.LEADING;
          }
          else if (text.equalsIgnoreCase("TRAILING")) {
              return TitledBorder.TRAILING;
          }
          else {
              return TitledBorder.DEFAULT_JUSTIFICATION;
          }
      }
  
      /** 
       * @return the enumeration for the title position
       */
      protected int asTitlePosition(String text) {    
          if (text.equalsIgnoreCase("ABOVE_TOP")) {
              return TitledBorder.ABOVE_TOP;
          }
          else if (text.equalsIgnoreCase("TOP")) {
              return TitledBorder.TOP;
          }
          else if (text.equalsIgnoreCase("BELOW_TOP")) {
              return TitledBorder.BELOW_TOP;
          }
          else if (text.equalsIgnoreCase("ABOVE_BOTTOM")) {
              return TitledBorder.ABOVE_BOTTOM;
          }
          else if (text.equalsIgnoreCase("BOTTOM")) {
              return TitledBorder.BOTTOM;
          }
          else if (text.equalsIgnoreCase("BELOW_BOTTOM")) {
              return TitledBorder.BELOW_BOTTOM;
          }
          else {
              return TitledBorder.DEFAULT_POSITION;
          }
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/BorderTagSupport.java
  
  Index: BorderTagSupport.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $
   * $Revision: 1.7 $
   * $Date: 2002/05/17 15:18:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-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/>.
   * 
   * $Id: DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.swing;
  
  import javax.swing.border.Border;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /** 
   * An abstract base class used for concrete border tags which create new Border implementations
   * and either export them as variables or set them on parent widgets.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.7 $
   */
  public abstract class BorderTagSupport extends TagSupport {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(BorderTagSupport.class);
  
      private String var;
  
      public BorderTagSupport() {
      }
  
      // Tag interface
      //-------------------------------------------------------------------------                    
      public void doTag(final XMLOutput output) throws Exception {
  
          Border border = createBorder();
  
          // allow some nested tags to set properties        
          invokeBody(output);
          
          if (var != null) {
              context.setVariable(var, border);
          }
          else {
              ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
              if ( tag != null ) {
                  tag.setBorder(border);
              }
              else {
                  throw new JellyException( "Either the 'var' attribute must be specified to export this Border or this tag must be nested within a JellySwing widget tag" );
              }
          }
      }
      
      // Properties
      //-------------------------------------------------------------------------                    
  
  
      /**
       * Sets the name of the variable to use to expose the new Border object. 
       * If this attribute is not set then the parent widget tag will have its 
       * border property set.
       */
      public void setVar(String var) {
          this.var = var;
      }
      
      // Implementation methods
      //-------------------------------------------------------------------------                    
      
      /**
       * Factory method to create a new Border instance.
       */
      protected abstract Border createBorder() throws Exception;   
  }
  
  
  

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