KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > html > InsertTextMode


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.html;
16
17 import org.apache.tapestry.IMarkupWriter;
18
19 /**
20  * Defines a number of ways to format multi-line text for proper renderring.
21  *
22  * @author Howard Lewis Ship
23  */

24
25 public abstract class InsertTextMode
26 {
27     /**
28      * Mode where each line (after the first) is preceded by a <br> tag.
29      */

30
31     public static final InsertTextMode BREAK = new BreakMode();
32
33     /**
34      * Mode where each line is wrapped with a <p> element.
35      */

36
37     public static final InsertTextMode PARAGRAPH = new ParagraphMode();
38
39     private final String JavaDoc _name;
40
41     protected InsertTextMode(String JavaDoc name)
42     {
43         _name = name;
44     }
45
46     public String JavaDoc toString()
47     {
48         return "InsertTextMode[" + _name + "]";
49     }
50
51     /**
52      * Invoked by the {@link InsertText} component to write the next line.
53      *
54      * @param lineNumber
55      * the line number of the line, starting with 0 for the first line.
56      * @param line
57      * the String for the current line.
58      * @param writer
59      * the {@link IMarkupWriter} to send output to.
60      * @param raw
61      * if true, then the output should be unfiltered
62      */

63
64     public abstract void writeLine(int lineNumber, String JavaDoc line, IMarkupWriter writer, boolean raw);
65
66     private static class BreakMode extends InsertTextMode
67     {
68         private BreakMode()
69         {
70             super("BREAK");
71         }
72
73         public void writeLine(int lineNumber, String JavaDoc line, IMarkupWriter writer, boolean raw)
74         {
75             if (lineNumber > 0)
76                 writer.beginEmpty("br");
77
78             writer.print(line, raw);
79         }
80     }
81
82     private static class ParagraphMode extends InsertTextMode
83     {
84         private ParagraphMode()
85         {
86             super("PARAGRAPH");
87         }
88
89         public void writeLine(int lineNumber, String JavaDoc line, IMarkupWriter writer, boolean raw)
90         {
91             writer.begin("p");
92
93             writer.print(line, raw);
94
95             writer.end();
96         }
97     }
98
99 }
Popular Tags