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.html;
016
017import org.apache.tapestry.IMarkupWriter;
018
019/**
020 * Defines a number of ways to format multi-line text for proper renderring.
021 * 
022 * @author Howard Lewis Ship
023 */
024
025public abstract class InsertTextMode
026{
027    /**
028     * Mode where each line (after the first) is preceded by a <br> tag.
029     */
030
031    public static final InsertTextMode BREAK = new BreakMode();
032
033    /**
034     * Mode where each line is wrapped with a <p> element.
035     */
036
037    public static final InsertTextMode PARAGRAPH = new ParagraphMode();
038
039    private final String _name;
040
041    protected InsertTextMode(String name)
042    {
043        _name = name;
044    }
045
046    public String toString()
047    {
048        return "InsertTextMode[" + _name + "]";
049    }
050
051    /**
052     * Invoked by the {@link InsertText} component to write the next line.
053     * 
054     * @param lineNumber
055     *            the line number of the line, starting with 0 for the first line.
056     * @param line
057     *            the String for the current line.
058     * @param writer
059     *            the {@link IMarkupWriter} to send output to.
060     * @param raw
061     *            if true, then the output should be unfiltered
062     */
063
064    public abstract void writeLine(int lineNumber, String line, IMarkupWriter writer, boolean raw);
065
066    private static class BreakMode extends InsertTextMode
067    {
068        private BreakMode()
069        {
070            super("BREAK");
071        }
072
073        public void writeLine(int lineNumber, String line, IMarkupWriter writer, boolean raw)
074        {
075            if (lineNumber > 0)
076                writer.beginEmpty("br");
077
078            writer.print(line, raw);
079        }
080    }
081
082    private static class ParagraphMode extends InsertTextMode
083    {
084        private ParagraphMode()
085        {
086            super("PARAGRAPH");
087        }
088
089        public void writeLine(int lineNumber, String line, IMarkupWriter writer, boolean raw)
090        {
091            writer.begin("p");
092
093            writer.print(line, raw);
094
095            writer.end();
096        }
097    }
098
099}