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