KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > html > Text


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.html;
66
67 /**
68  * Text.java
69  *
70  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
71  */

72
73 import java.io.PrintWriter JavaDoc;
74
75
76 /**
77  * @author Michael Nash
78  * @version $Revision: 1.10 $ $Date: 2004/11/18 02:03:32 $
79  */

80 public class Text
81         extends HtmlElement {
82     private String JavaDoc thisClass = (this.getClass().getName() + ".");
83
84     /* Defaults - static, so the next Text comes out in this style once they're set */
85     private static String JavaDoc defaultStyle = null;
86     private static String JavaDoc defaultFontColor = null;
87     private static String JavaDoc defaultFontFace = null;
88     private static boolean defaultSmall = false;
89     protected String JavaDoc contentString = ("&nbsp;");
90     private String JavaDoc style = defaultStyle;
91     private boolean isSmall = defaultSmall;
92     private String JavaDoc fontColor = defaultFontColor;
93     private String JavaDoc fontFace = defaultFontFace;
94
95     /**
96      * Constructor
97      */

98     public Text()
99             throws HtmlException {
100         super();
101     } /* Text() */
102
103     /**
104      * Constructor
105      *
106      * @param newString new text value
107      */

108     public Text(String JavaDoc newString)
109             throws HtmlException {
110         super(newString);
111
112         String JavaDoc myName = (thisClass + "Text(String)");
113
114         if (contentString == null) {
115             throw new HtmlException(myName +
116                     ":Unable to defined a null text string for " +
117                     getName());
118         }
119
120         contentString = newString;
121     } /* Text(String) */
122
123     /**
124      * Constructor
125      *
126      * @param newString value of text
127      * @param newStyle CSS style
128      */

129     public Text(String JavaDoc newString, String JavaDoc newStyle)
130             throws HtmlException {
131         this(newString);
132         setDefaultStyle(newStyle);
133     } /* Text(String, String) */
134
135     /**
136      * @param out the print writer
137      * @param depth the number of tabs to indent
138      */

139     protected void display(PrintWriter JavaDoc out, int depth)
140             throws HtmlException {
141         boolean terminate = false;
142
143         if ((cSSClass != null) && (!cSSClass.equals("jc-default"))) {
144             terminate = true;
145             this.padWithTabs(out, depth);
146             out.print("<span");
147             out.print(" class=\"" + cSSClass + "\"");
148
149             if (cSSID != null) {
150                 out.print(" id=\"" + cSSID + "\"");
151             }
152
153             out.print(">");
154         } else {
155             this.padWithTabs(out, depth);
156
157             if (cSSID != null) {
158                 out.print("<span id=\"" + cSSID + "\">");
159                 terminate = true;
160             }
161         }
162         if (style != null) {
163             if (style.equals("bold")) {
164                 out.print("<strong>");
165             } else if (style.equals("italic")) {
166                 out.print("<i>");
167             }
168         }
169         if (fontColor != null) {
170             out.print("<font color=\"" + fontColor + "\">");
171         }
172         if (isSmall) {
173             out.print("<small>");
174         }
175
176         out.print(contentString);
177
178         if (isSmall) {
179             out.print("</small>");
180         }
181         if (fontColor != null) {
182             out.print("</font>");
183         }
184         if (style != null) {
185             if (style.equals("bold")) {
186                 out.print("</strong>");
187             } else if (style.equals("italic")) {
188                 out.print("</i>");
189             }
190         }
191         if (terminate) {
192             out.print("</SPAN>");
193         }
194
195         setDisplayed();
196     } /* display(PrintWriter) */
197
198
199     /**
200      * @throws HtmlException
201      */

202     public void resetDefaults()
203             throws HtmlException {
204         cSSClass = null;
205         setDefaultFontColor(null);
206         setDefaultFontFace(null);
207         setDefaultStyle(null);
208     } /* resetDefaults() */
209
210
211     /**
212      * @param newFontColor font color
213      */

214     public static void setDefaultFontColor(String JavaDoc newFontColor)
215             throws HtmlException {
216         defaultFontColor = newFontColor;
217     } /* setDefaultFontColor(String) */
218
219
220     /**
221      * @param newFontFace font face
222      */

223     public static void setDefaultFontFace(String JavaDoc newFontFace)
224             throws HtmlException {
225         defaultFontFace = newFontFace;
226     } /* setDeaultFontFace(String) */
227
228
229     /**
230      * @param newSmall set to true for small
231      */

232     public static void setDefaultSmall(boolean newSmall)
233             throws HtmlException {
234         defaultSmall = newSmall;
235     } /* setDefaultSmall(boolean) */
236
237
238     /**
239      * @param newStyle sets the default style
240      */

241     public static void setDefaultStyle(String JavaDoc newStyle) {
242         defaultStyle = newStyle;
243     } /* setDefaultStyle(String) */
244
245     /**
246      * @param newColor new font color
247      */

248     public void setFontColor(String JavaDoc newColor)
249             throws HtmlException {
250         fontColor = newColor;
251     } /* setFontColor(String) */
252
253
254     /**
255      * @param newFace new font face
256      */

257     public void setFontFace(String JavaDoc newFace)
258             throws HtmlException {
259         fontFace = newFace;
260     } /* setFontFace(String) */
261
262
263     /**
264      * @param newSmall set to true for small font
265      */

266     public void setSmall(boolean newSmall)
267             throws HtmlException {
268         isSmall = newSmall;
269     } /* setSmall(boolean) */
270
271
272     /**
273      * @param newStyle set the new style
274      */

275     public void setStyle(String JavaDoc newStyle)
276             throws HtmlException {
277         String JavaDoc myName = (thisClass + "setStyle(String)");
278
279         if (newStyle.equalsIgnoreCase("bold")) {
280             style = ("bold");
281         } else if (newStyle.equalsIgnoreCase("italic")) {
282             style = ("italic");
283         } else {
284             throw new HtmlException(myName +
285                     ":Style must be 'bold' or 'italic', '" +
286                     newStyle + "' is not valid for " +
287                     getName());
288         }
289     } /* setStyle(String) */
290
291
292     /**
293      * @param newText new text for the item
294      */

295     public void setText(String JavaDoc newText)
296             throws HtmlException {
297         String JavaDoc myName = (thisClass + "setText(String)");
298
299         if (newText == null) {
300             throw new HtmlException(myName +
301                     ":Can't set a null string for context of a Text for " +
302                     getName());
303         }
304
305         contentString = newText;
306     } /* setText(String) */
307
308
309 } /* Text */
310
Popular Tags