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