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
015package org.apache.tapestry.components;
016
017import java.text.Format;
018
019import org.apache.hivemind.ApplicationRuntimeException;
020import org.apache.tapestry.AbstractComponent;
021import org.apache.tapestry.IMarkupWriter;
022import org.apache.tapestry.IRequestCycle;
023
024/**
025 * Used to insert some text (from a parameter) into the HTML. [ <a
026 * href="../../../../../ComponentReference/Insert.html">Component Reference </a>]
027 * 
028 * @author Howard Lewis Ship
029 */
030
031public abstract class Insert extends AbstractComponent
032{
033    /**
034     * Prints its value parameter, possibly formatted by its format parameter.
035     */
036
037    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
038    {
039        if (cycle.isRewinding())
040            return;
041
042        Object value = getValue();
043
044        if (value == null)
045            return;
046
047        String insert = null;
048
049        Format format = getFormat();
050
051        if (format == null)
052        {
053            insert = value.toString();
054        }
055        else
056        {
057            try
058            {
059                insert = format.format(value);
060            }
061            catch (Exception ex)
062            {
063                throw new ApplicationRuntimeException(ComponentMessages.unableToFormat(
064                        this,
065                        value,
066                        ex), this, getBinding("format").getLocation(), ex);
067            }
068        }
069
070        String styleClass = getStyleClass();
071
072        if (styleClass != null)
073        {
074            writer.begin("span");
075            writer.attribute("class", styleClass);
076
077            renderInformalParameters(writer, cycle);
078        }
079
080        writer.print(insert, getRaw());
081        
082        if (styleClass != null)
083            writer.end(); // <span>
084    }
085
086    public abstract Object getValue();
087
088    public abstract Format getFormat();
089
090    public abstract String getStyleClass();
091
092    public abstract boolean getRaw();
093
094}