001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.html;
016    
017    import org.apache.tapestry.AbstractComponent;
018    import org.apache.tapestry.IAsset;
019    import org.apache.tapestry.IMarkupWriter;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.Tapestry;
022    
023    /**
024     * Used to insert an image. To create a rollover image, use the {@link Rollover} class, which
025     * integrates a link with the image assets used with the button. [<a
026     * href="../../../../../ComponentReference/Image.html">Component Reference</a>]
027     * 
028     * @author Howard Lewis Ship
029     */
030    
031    public abstract class Image extends AbstractComponent
032    {
033        /**
034         * Renders the &lt;img&gt; element.
035         */
036    
037        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
038        {
039            // Doesn't contain a body so no need to do anything on rewind (assumes no
040            // sideffects to accessor methods via bindings).
041    
042            if (cycle.isRewinding())
043                return;
044    
045            IAsset imageAsset = getImage();
046    
047            if (imageAsset == null)
048                throw Tapestry.createRequiredParameterException(this, "image");
049    
050            String imageURL = imageAsset.buildURL();
051    
052            writer.beginEmpty("img");
053    
054            writer.attribute("src", imageURL);
055    
056            renderInformalParameters(writer, cycle);
057    
058            writer.closeTag();
059        }
060    
061        public abstract IAsset getImage();
062    }