commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core UseBeanTag.java
Date Tue, 24 Sep 2002 16:48:54 GMT
jstrachan    2002/09/24 09:48:54

  Modified:    jelly/src/test/org/apache/commons/jelly/swing example.jelly
               jelly/src/java/org/apache/commons/jelly/tags/swing
                        SwingTagLibrary.java ComponentTag.java
                        ActionTag.java
               jelly/src/java/org/apache/commons/jelly/tags/core
                        UseBeanTag.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/swing
                        FontTag.java
  Log:
  Refactored some of the code for JellySwing to reuse the new UseBeanTag which simplifies
the code quite a bit.
  This also allows JellySwing widgets to be customized such as
  
  	<table class="com.acme.MyTable" model="${mymodel}"/>
  
  Cleaned up the code so that either widgets/models are created and added to the parent widget,
or they are just exported as a variable if the 'var' attribute is specified.
  
  Also added an introductory <font> implementation. Though the createFont() factory
method doesn't seem to work very well :-(
  
  Revision  Changes    Path
  1.5       +17 -8     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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- example.jelly	9 Aug 2002 18:43:10 -0000	1.4
  +++ example.jelly	24 Sep 2002 16:48:54 -0000	1.5
  @@ -5,11 +5,18 @@
   	xmlns:log="jelly:log"
   	xmlns:define="jelly:define">
   
  -  <define:script var="onClosing">
  +  <action var="fooAction" name="Foo" tooltip="some text">
  +  	<log:info>Just evaluated the foo action!</log:info>
  +  </action>
  +
  +  <font var="bigfont" name="helvetica" family="helvetica" size="${20}"/>
  +
  +	<define:script var="onClosing">
       <log:info>The frame is closing via event: ${event}</log:info>
     </define:script>
     
     <frame title="This is a frame" var="frame" location="100,100" size="800,400">
  +  	<font family="helvetica" size="14"/>
       <menuBar>
         <menu text="File">
           <menuItem>
  @@ -24,25 +31,30 @@
           </menuItem>
           <menuItem text="Save" enabled="false" />
           <separator/>
  -        <menuItem text="Properties"/>
  +        <menuItem text="Properties" font="${bigfont}"/>
  +        <menuItem action="${fooAction}"/>
         </menu>
         <menu text="Help">
  +				<font family="Verdana" size="14"/>
  +
           <menuItem text="About"/>
         </menu>
       </menuBar>
       
  -	<splitPane>    
  +		<splitPane>    
         <panel>
           <label text="Enter your name"/>
  -        <textField text="James"/>
  +        <textField text="James" font="${bigfont}"/>
           <button>
             <action name="OK">
             	<log:info>Clicked on the OK button</log:info>
             </action>
           </button>
  +        <button action="${fooAction}"/>
         </panel>
         <scrollPane>
  -        <j:new className="org.apache.commons.jelly.swing.MyTableModel" var="tableModel"/>
  +      	<!-- this tag can set properties on the bean model too -->
  +        <j:useBean var="tableModel" class="org.apache.commons.jelly.swing.MyTableModel"/>
           <table model="${tableModel}"/>
         </scrollPane>
       </splitPane>
  @@ -53,6 +65,3 @@
     ${frame.show()}
     
   </j:jelly>
  -
  -
  -
  
  
  
  1.6       +1 -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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SwingTagLibrary.java	9 Aug 2002 18:42:21 -0000	1.5
  +++ SwingTagLibrary.java	24 Sep 2002 16:48:54 -0000	1.6
  @@ -108,6 +108,7 @@
           
       public SwingTagLibrary() {
           registerTag( "action", ActionTag.class );
  +        registerTag( "font", FontTag.class );
           registerTag( "windowListener", WindowListenerTag.class );
       }
   
  
  
  
  1.6       +89 -66    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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ComponentTag.java	19 Aug 2002 21:38:56 -0000	1.5
  +++ ComponentTag.java	24 Sep 2002 16:48:54 -0000	1.6
  @@ -61,25 +61,24 @@
    */
   package org.apache.commons.jelly.tags.swing;
   
  -
   import java.awt.Component;
   import java.awt.Container;
   import java.awt.Dimension;
  +import java.awt.Font;
   import java.awt.Point;
   import java.awt.Window;
   import java.awt.event.WindowListener;
  +import java.util.Iterator;
  +import java.util.Map;
   
   import javax.swing.*;
   
   import org.apache.commons.beanutils.BeanUtils;
  -import org.apache.commons.beanutils.ConvertingWrapDynaBean;
   import org.apache.commons.beanutils.ConvertUtils;
   
  -import org.apache.commons.collections.BeanMap;
  -
  -import org.apache.commons.jelly.DynaBeanTagSupport;
  +import org.apache.commons.jelly.JellyException;
   import org.apache.commons.jelly.XMLOutput;
  -import org.apache.commons.jelly.impl.BeanSource;
  +import org.apache.commons.jelly.tags.core.UseBeanTag;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -91,7 +90,7 @@
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
    */
  -public class ComponentTag extends DynaBeanTagSupport implements BeanSource {
  +public class ComponentTag extends UseBeanTag {
   
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog(ComponentTag.class);
  @@ -99,11 +98,8 @@
       /** the factory of widgets */
       private Factory factory;
       
  -    /** the current bean instance */
  -    private Object bean;
  -
  -    /** the current variable name that the bean should be exported as */
  -    private String var;
  +    public ComponentTag() {
  +    }
   
       public ComponentTag(Factory factory) {
           this.factory = factory;
  @@ -168,6 +164,17 @@
       }
   
       /**
  +     * Sets the Font of this component
  +     */
  +    public void setFont(Font font) throws Exception {
  +        Component component = getComponent();
  +        if ( component != null ) {
  +            // lets just try set the 'font' property
  +            BeanUtils.setProperty( component, "font", font );
  +        }
  +    }
  +
  +    /**
        * Adds a WindowListener to this component
        */
       public void addWindowListener(WindowListener listener) throws Exception {
  @@ -178,19 +185,79 @@
           }
       }
   
  -    // DynaTag interface
  +    // Properties
       //-------------------------------------------------------------------------       
            
  -    public void beforeSetAttributes() throws Exception {
  -        // create a new dynabean before the attributes are set
  -        bean = factory.newInstance();
  -        setDynaBean( new ConvertingWrapDynaBean( bean ) );
  +
  +    /**
  +     * @return the visible component, if there is one.
  +     */
  +    public Component getComponent() {
  +        Object bean = getBean();
  +        if ( bean instanceof Component ) {
  +            return (Component) bean;
  +        }
  +        return null;
  +    }    
  +    
  +    
  +    // Implementation methods
  +    //-------------------------------------------------------------------------       
            
  +
  +    /**
  +     * A class may be specified otherwise the Factory will be used.
  +     */
  +    protected Class convertToClass(Object classObject) throws Exception {
  +        if (classObject == null) {
  +            return null;
  +        }
  +        else {
  +            return super.convertToClass(classObject);
  +        }
       }
  +    
  +    /**
  +     * A class may be specified otherwise the Factory will be used.
  +     */
  +    protected Object newInstance(Class theClass, Map attributes, XMLOutput output) throws
Exception {
  +        if (theClass != null ) {
  +            return theClass.newInstance();
  +        }
  +        else {
  +            return factory.newInstance();
  +        }
  +    }
  +    
   
  -    public void setAttribute(String name, Object value) throws Exception {        
  -        if (name.equals( "var" ) ) {
  -            this.var = value.toString();
  +    /**
  +     * Either defines a variable or adds the current component to the parent
  +     */    
  +    protected void processBean(String var, Object bean) throws Exception {
  +        if (var != null) {
  +            context.setVariable(var, bean);
           }
           else {
  +            Component component = getComponent();
  +            if ( component != null ) {
  +                ComponentTag parentTag = (ComponentTag) findAncestorWithClass( ComponentTag.class
);
  +                if ( parentTag != null ) {
  +                    parentTag.addChild(component);
  +                }
  +                else {
  +                    throw new JellyException( "This tag must be used within a JellySwing
widget tag or the 'var' attribute must be specified" );
  +                }
  +            }
  +        }
  +    }
  +    
  +    /**
  +     * Patch to handle wierd properties that don't quite match the Java Beans contract
  +     */
  +    protected void setBeanProperties(Object bean, Map attributes) throws Exception {
  +        for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext(); ) {
  +            Map.Entry entry = (Map.Entry) iter.next();
  +            String name = (String) entry.getKey();
  +            Object value = entry.getValue();
  +
               // ### special hacks for properties that don't introspect properly        
   
               Component component = getComponent();
               if ( component != null ) {
  @@ -215,56 +282,12 @@
                       component.setSize(d);
                   }                    
                   else {
  -                    super.setAttribute(name, value);
  +                    BeanUtils.setProperty(component, name, value);
                   }
               }
               else {
  -                super.setAttribute(name, value);
  +                BeanUtils.setProperty(bean, name, value);
               }
           }
       }
  -
  -    // Tag interface
  -    //-------------------------------------------------------------------------       
            
  -    public void doTag(XMLOutput output) throws Exception {
  -
  -        // export the bean if required
  -        if ( var != null ) {
  -            context.setVariable(var, bean);
  -        }
  -        
  -        invokeBody(output);
  -
  -        // now we should add this component to its parent...
  -        // This is currently quite simplistic. 
  -        // We could well support layout managers and such like
  -
  -        Component component = getComponent();
  -        if ( component != null ) {
  -            ComponentTag parentTag = (ComponentTag) findAncestorWithClass( ComponentTag.class
);
  -            if ( parentTag != null ) {
  -                parentTag.addChild(component);
  -            }
  -        }
  -    }
  -    
  -    // Properties
  -    //-------------------------------------------------------------------------       
            
  -
  -    /**
  -     * @return the bean that has just been created 
  -     */
  -    public Object getBean() {
  -        return bean;
  -    }
  -
  -    /**
  -     * @return the visible component, if there is one.
  -     */
  -    public Component getComponent() {
  -        if ( bean instanceof Component ) {
  -            return (Component) bean;
  -        }
  -        return null;
  -    }    
   }
  
  
  
  1.2       +97 -69    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/ActionTag.java
  
  Index: ActionTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/ActionTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ActionTag.java	24 Jul 2002 17:34:43 -0000	1.1
  +++ ActionTag.java	24 Sep 2002 16:48:54 -0000	1.2
  @@ -67,78 +67,130 @@
   
   import javax.swing.*;
   
  -import org.apache.commons.beanutils.BeanUtils;
  -
   import org.apache.commons.jelly.JellyException;
   import org.apache.commons.jelly.MapTagSupport;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.tags.core.UseBeanTag;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
   /** 
  - * Creates a Swing Action and attaches it to the parent component. The This tag creates
a Swing component and adds it to its parent tag, optionally declaring this
  - * component as a variable if the <i>var</i> attribute is specified.</p>
  + * Creates a Swing Action which on invocation will execute the body of this tag.
  + * The Action is then output as a variable for reuse if the 'var' attribute is specified
  + * otherwise the action is added to the parent JellySwing widget.
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
    */
  -public class ActionTag extends MapTagSupport {
  +public class ActionTag extends UseBeanTag {
   
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog(ActionTag.class);
   
  -    /** the current action instance */
  -    private Action action;
  -
       public ActionTag() {
       }
   
       // Tag interface
       //-------------------------------------------------------------------------       
            
  -    public void doTag(final XMLOutput output) throws Exception {
  -
  -        Map map = getAttributes();
  -        action = (Action) map.remove( "action" );
  -        Object classObject = map.remove("className");
  -        String var = (String) map.remove( "var" );
  +    public void doTag(XMLOutput output) throws Exception {
  +        Map attributes = getAttributes();
  +        String var = (String) attributes.get( "var" );
  +        Object classObject = attributes.remove( "class" );
  +        
  +        // this method could return null in derived classes
  +        Class theClass = convertToClass(classObject);
  +        
  +        Object bean = newInstance(theClass, attributes, output);
  +        setBean(bean);
           
  +        setBeanProperties(bean, attributes);
  +        
  +        processBean(var, bean);
  +    }
  +
  +    
  +    // Properties
  +    //-------------------------------------------------------------------------       
            
  +
  +    /**
  +     * @return the Action object for this tag
  +     */
  +    public Action getAction() {
  +        return (Action) getBean();
  +    }
  +    
  +    
  +    // Implementation methods
  +    //-------------------------------------------------------------------------       
            
  +
  +
  +    /**
  +     * An existing Action could be specified via the 'action' attribute or an action class

  +     * may be specified via the 'class' attribute, otherwise a default Action class is
created.
  +     */
  +    protected Class convertToClass(Object classObject) throws Exception {
  +        if (classObject == null) {
  +            return null;
  +        }
  +        else {
  +            return super.convertToClass(classObject);
  +        }
  +    }
  +    
  +    /**
  +     * An existing Action could be specified via the 'action' attribute or an action class

  +     * may be specified via the 'class' attribute, otherwise a default Action class is
created.
  +     */
  +    protected Object newInstance(Class theClass, Map attributes, final XMLOutput output)
throws Exception {
  +        Action action = (Action) attributes.remove( "action" );
           if ( action == null ) {
  -            Class actionClass = null;
  -            if ( classObject != null ) {
  -                if ( classObject instanceof Class ) {
  -                    actionClass = (Class) classObject;
  -                }
  -                else {
  -                    String name = classObject.toString();
  -                    try {
  -                        actionClass = Class.forName( name );
  -                    }
  -                    catch (Throwable t) {
  -                        throw new JellyException( "Could not find class: " + name + " for
this <action> tag. Exception: " +t, t );
  +            if (theClass != null ) {
  +                return theClass.newInstance();
  +            }
  +            else {
  +                action = new AbstractAction() {
  +                    public void actionPerformed(ActionEvent event) {
  +                        context.setVariable( "event", event );
  +                        try {
  +                            invokeBody(output);
  +                        }
  +                        catch (Exception e) {
  +                            log.error( "Caught: " + e, e );
  +                        }
                       }
  -                }
  -                if ( actionClass != null ) {
  -                    action = (Action) actionClass.newInstance();
  -                }
  +                };
               }
           }
  -        if ( action == null ) {
  -            action = new AbstractAction() {
  -                public void actionPerformed(ActionEvent event) {
  -                    context.setVariable( "event", event );
  -                    try {
  -                        invokeBody(output);
  -                    }
  -                    catch (Exception e) {
  -                        log.error( "Caught: " + e, e );
  -                    }
  -                }
  -            };
  +        return action;
  +    }
  +    
  +
  +    /**
  +     * Either defines a variable or adds the current component to the parent
  +     */    
  +    protected void processBean(String var, Object bean) throws Exception {
  +        if (var != null) {
  +            context.setVariable(var, bean);
  +        }
  +        else {
  +            ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class
);
  +            if ( tag != null ) {
  +                tag.setAction((Action) bean);
  +            }
  +            else {
  +                throw new JellyException( "Either the 'var' attribute must be specified
to export this Action or this tag must be nested within a JellySwing widget tag" );
  +            }
           }
  +    }
  +    
           
  -        // now lets set the properties on the action object
  -        for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
  +    /**
  +     * Perform the strange setting of Action properties using its custom API
  +     */
  +    protected void setBeanProperties(Object bean, Map attributes) throws Exception {
  +        Action action = getAction();
  +        for ( Iterator iter = attributes.entrySet().iterator(); iter.hasNext(); ) {
               Map.Entry entry = (Map.Entry) iter.next();
               String name = (String) entry.getKey();
               
  @@ -148,32 +200,9 @@
               
               action.putValue( name, value );
           }
  -
  -        if ( var != null ) {
  -            context.setVariable( var, action );
  -        }
  -        
  -
  -        // now lets add this action to its parent if we have one
  -        ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
  -        if ( tag != null ) {
  -            tag.setAction(action);
  -        }
       }
  -    
  -    // Properties
  -    //-------------------------------------------------------------------------       
            
   
  -    /**
  -     * @return the Action object for this tag
  -     */
  -    public Action getAction() {
  -        return action;
  -    }
  -    
  -    
  -    // Implementation methods
  -    //-------------------------------------------------------------------------       
            
  +
       protected String capitalize(String text) {
           char ch = text.charAt(0);
           if ( Character.isUpperCase( ch ) ) {
  @@ -184,6 +213,5 @@
           buffer.append( text.substring(1) );
           return buffer.toString();
       }
  -        
           
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/swing/FontTag.java
  
  Index: FontTag.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.Font;
  import java.util.Map;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.MapTagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /** 
   * Creates an Font and attaches it to the parent component or exports the font as
   * a reusable variable that can be attached to multiple widgets. 
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.7 $
   */
  public class FontTag extends MapTagSupport {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(FontTag.class);
  
      /** the current font instance */
      private Font font;
  
      public FontTag() {
      }
  
      // Tag interface
      //-------------------------------------------------------------------------        
           
      public void setAttribute(String name, Object value) {
          if (name.equals("size")) {
              super.setAttribute(name, ConvertUtils.value);
          }
          else {
              super.setAttribute(name, value);
          }
      }
      
      public void doTag(final XMLOutput output) throws Exception {
          Map attributes = getAttributes();
          String var = (String) attributes.remove("var");
          
          font = createFont(attributes);
  
          if (var != null) {
              context.setVariable(var, font);
          }
          else {
              // now lets add this font to its parent if we have one
              ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class
);
              if ( tag != null ) {
                  tag.setFont(font);
              }
              else {
                  throw new JellyException( "this tag must be nested within a JellySwing widget
tag or the 'var' attribute must be specified" );
              }
          }
      }
      
      // Properties
      //-------------------------------------------------------------------------        
           
  
      /**
       * @return the Font object for this tag
       */
      public Font getFont() {
          return font;
      }
  
      
      // Implementation methods
      //-------------------------------------------------------------------------        
           
      
      /**
       * Factory method to create a new Font based on the given properties
       */
      protected Font createFont(Map map) {
          log.info( "Creating font from properties: " + map );
          Font font = new Font(map);
          //Font font = Font.getFont(map);
          log.info( "Created font: " + font );
          return font;
      }
  }
  
  
  
  1.2       +35 -6     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java
  
  Index: UseBeanTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UseBeanTag.java	24 Sep 2002 11:38:53 -0000	1.1
  +++ UseBeanTag.java	24 Sep 2002 16:48:54 -0000	1.2
  @@ -63,6 +63,7 @@
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.MapTagSupport;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.impl.BeanSource;
   
   /** 
    * A tag which instantiates an instance of the given class
  @@ -81,23 +82,41 @@
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
    */
  -public class UseBeanTag extends MapTagSupport  {
  +public class UseBeanTag extends MapTagSupport implements BeanSource {
  +
  +    /** the current bean instance */
  +    private Object bean;
   
       public UseBeanTag(){
       }
   
  +    //BeanSource interface
  +    //-------------------------------------------------------------------------       
            
  +
  +    /**
  +     * @return the bean that has just been created 
  +     */
  +    public Object getBean() {
  +        return bean;
  +    }
  +
  +
       // Tag interface
       //-------------------------------------------------------------------------       
            
       public void doTag(XMLOutput output) throws Exception {
           Map attributes = getAttributes();
           String var = (String) attributes.get( "var" );
           Object classObject = attributes.remove( "class" );
  -        if ( classObject == null ) {
  -            throw new MissingAttributeException("class");
  -        }
  +        
  +        // this method could return null in derived classes
           Class theClass = convertToClass(classObject);
  -        Object bean = newInstance(theClass, attributes);
  +        
  +        this.bean = newInstance(theClass, attributes, output);
           setBeanProperties(bean, attributes);
  +        
  +        // invoke body which could result in other properties being set
  +        invokeBody(output);
  +        
           processBean(var, bean);
       }
   
  @@ -105,6 +124,13 @@
       //-------------------------------------------------------------------------       
            
   
       /**
  +     * Allow derived classes to programatically set the bean
  +     */
  +    protected void setBean(Object bean) {
  +        this.bean = bean;
  +    }
  +    
  +    /**
        * Attempts to convert the given object to a Class instance.
        * If the classObject is already a Class it will be returned 
        * otherwise it will be converted to a String and loaded
  @@ -114,6 +140,9 @@
           if (classObject instanceof Class) {
               return (Class) classObject;
           }
  +        else if ( classObject == null ) {
  +            throw new MissingAttributeException("class");
  +        }
           else {
               String className = classObject.toString();
               return loadClass(className);
  @@ -139,7 +168,7 @@
        * default constructor.
        * Derived tags could do something different here.
        */
  -    protected Object newInstance(Class theClass, Map attributes) throws Exception {
  +    protected Object newInstance(Class theClass, Map attributes, XMLOutput output) throws
Exception {
           return theClass.newInstance();
       }
       
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message