KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > template > TemplatePageAttributes


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

18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.ecs.HtmlColor;
22 import org.apache.ecs.html.Link;
23 import org.apache.ecs.html.Meta;
24 import org.apache.ecs.html.Script;
25 import org.apache.ecs.html.Style;
26 import org.apache.ecs.html.Title;
27 import org.apache.turbine.services.pull.ApplicationTool;
28 import org.apache.turbine.util.RunData;
29
30 /**
31  * Template context tool that will set various attributes of the HTML
32  * page. It is automatically placed in the Template context as
33  * '$page'. Here's an example of some uses:
34  *
35  * <p><pre><code>
36  * $page.setBgColor("#ffffff");
37  * $page.setBgColor("white");
38  * $page.setBackground("/images/standardbg.jpeg");
39  * $page.setTitle("This is the title!");
40  * $page.setKeywords("turbine, cool, servlet framework");
41  * $page.setStyleSheet("/style.css");
42  * </code></pre></p>
43  *
44  * @author <a HREF="mailto:sean@somacity.com">Sean Legassick</a>
45  * @deprecated Use HtmlPageAttributes along with VelocityOnlyLayout instead
46  * @version $Id: TemplatePageAttributes.java,v 1.8.2.2 2004/05/20 03:27:23 seade Exp $
47  */

48 public class TemplatePageAttributes
49         implements ApplicationTool
50 {
51     /** Logging */
52     private static Log log = LogFactory.getLog(TemplatePageAttributes.class);
53
54     /** The RunData object. */
55     private RunData data = null;
56
57     /** The title. */
58     private String JavaDoc cachedTitle = null;
59
60     /**
61      * Default constructor. The init method must be called before use
62      */

63     public TemplatePageAttributes()
64     {
65     }
66
67     /**
68      * Construct a new instance with the given RunData object.
69      *
70      * @param data a RunData instance
71      */

72     public TemplatePageAttributes(RunData data)
73     {
74         this.data = data;
75     }
76
77     /**
78      * Initialise this instance with the given RunData object.
79      * (ApplicationTool method)
80      *
81      * @param data Assumed to be a RunData instance
82      */

83     public void init(Object JavaDoc data)
84     {
85         log.warn("This class is deprecated. Use HtmlPageAttributes instead.");
86
87         // we blithely cast to RunData as the runtime error thrown
88
// if data is null or not RunData is appropriate.
89
this.data = (RunData) data;
90
91         // clear cached title
92
this.cachedTitle = null;
93     }
94
95     /**
96      * Refresh method - does nothing
97      */

98     public void refresh()
99     {
100         // empty
101
}
102
103     /**
104      * Set the title in the page. This returns an empty String so
105      * that the template doesn't complain about getting a null return
106      * value.
107      *
108      * @param intitle A String with the title.
109      */

110     public TemplatePageAttributes setTitle(String JavaDoc intitle)
111     {
112         Title title = data.getPage().getTitle();
113         if (cachedTitle != null)
114         {
115             cachedTitle += intitle;
116         }
117         else
118         {
119             cachedTitle = intitle;
120         }
121         title.addElement(intitle);
122         return this;
123     }
124
125     /**
126      * Get the title in the page. This returns an empty String if
127      * empty so that the template doesn't complain about getting a null
128      * return value.
129      *
130      * @return A String with the title.
131      */

132     public String JavaDoc getTitle()
133     {
134         if (cachedTitle == null)
135         {
136             return "";
137         }
138         return cachedTitle;
139     }
140
141     /**
142      * Adds a LINK to a CSS styleshet to the HEAD of the page.
143      *
144      * @param url A String.
145      * @return A TemplatePageAttributes (self).
146      */

147     public TemplatePageAttributes setStyleSheet(String JavaDoc url)
148     {
149         data.getPage().getHead().addElement(new Link()
150                 .setRel("stylesheet").setType("text/css").setHref(url));
151         return this;
152     }
153
154     /**
155      * Adds a LINK to a CSS stylesheet to the HEAD of the page, allowing the
156      * media type to be specified.
157      *
158      * @param url The value for the <code>href</code> attribute.
159      * @param media The value for the <code>media</code> attribute.
160      * @return a <code>TemplatePageAttributes</code> (self).
161      */

162     public TemplatePageAttributes setStyleSheet(String JavaDoc url, String JavaDoc media)
163     {
164         data.getPage().getHead().addElement(new Link().setRel("stylesheet")
165                 .setType("text/css").setMedia(media).setHref(url));
166         return this;
167     }
168
169     /**
170      * Adds a STYLE element to the HEAD of the page with the provided content.
171      *
172      * @param styleText The contents of the <code>style</code> tag.
173      * @return a <code>TemplatePageAttributes</code> (self).
174      */

175     public TemplatePageAttributes setStyle(String JavaDoc styleText)
176     {
177         data.getPage().getHead().addElement(new Style("text/css", styleText));
178         return this;
179     }
180
181     /**
182      * Adds a LINK to a javascript file to the HEAD of the page.
183      *
184      * @param url A String.
185      * @return A TemplatePageAttributesEx (self).
186      */

187     public TemplatePageAttributes setScript(String JavaDoc url)
188     {
189         data.getPage().getHead().addElement(new Script().setSrc(url)
190                 .setType("text/javascript").setLanguage("JavaScript"));
191         return this;
192     }
193
194     /**
195      * Set a keywords META tag in the HEAD of the page.
196      *
197      * @param keywords A String.
198      * @return A TemplatePageAttributes (self).
199      */

200     public TemplatePageAttributes setKeywords(String JavaDoc keywords)
201     {
202         data.getPage().getHead().addElement(
203                 new Meta().setName("keywords").setContent(keywords));
204         return this;
205     }
206
207     /**
208      * Sets a HttpEquiv META tag in the HEAD of the page, usage:
209      * <br><code>setHttpEquiv("refresh", "5; URL=http://localhost/nextpage.html")</code>
210      * <br><code>setHttpEquiv("Expires", "Tue, 20 Aug 1996 14:25:27 GMT")</code>
211      *
212      * @param httpEquiv The value to use for the http-equiv attribute.
213      * @param content The text for the content attribute of the meta tag.
214      * @return A TemplatePageAttributes (self).
215      */

216     public TemplatePageAttributes setHttpEquiv(String JavaDoc httpEquiv, String JavaDoc content)
217     {
218         data.getPage().getHead().addElement(
219                 new Meta().setHttpEquiv(httpEquiv).setContent(content));
220         return this;
221     }
222
223     /**
224      * Add a description META tag to the HEAD of the page.
225      *
226      * @param description A String.
227      * @return A TemplatePageAttributes (self).
228      */

229     public TemplatePageAttributes setDescription(String JavaDoc description)
230     {
231         data.getPage().getHead().addElement(
232                 new Meta().setName("description").setContent(description));
233         return this;
234     }
235
236     /**
237      * Set the background image for the BODY tag.
238      *
239      * @param url A String.
240      * @return A TemplatePageAttributes (self).
241      */

242     public TemplatePageAttributes setBackground(String JavaDoc url)
243     {
244         data.getPage().getBody().setBackground(url);
245         return this;
246     }
247
248     /**
249      * Set the background color for the BODY tag. You can use either
250      * color names or color values (e.g. "white" or "#ffffff" or
251      * "ffffff").
252      *
253      * @param color A String.
254      * @return A TemplatePageAttributes (self).
255      */

256     public TemplatePageAttributes setBgColor(String JavaDoc color)
257     {
258         String JavaDoc hexColor = HtmlColor.getColor(color);
259         if (hexColor == null)
260         {
261             hexColor = color;
262         }
263         data.getPage().getBody().setBgColor(hexColor);
264         return this;
265     }
266
267     /**
268      * Set the text color for the BODY tag. You can use either color
269      * names or color values (e.g. "white" or "#ffffff" or "ffffff").
270      *
271      * @param color A String.
272      * @return A TemplatePageAttributes (self).
273      */

274     public TemplatePageAttributes setTextColor(String JavaDoc color)
275     {
276         String JavaDoc hexColor = HtmlColor.getColor(color);
277         if (hexColor == null)
278         {
279             hexColor = color;
280         }
281         data.getPage().getBody().setText(hexColor);
282         return this;
283     }
284
285     /**
286      * Set the link color for the BODY tag. You can use either color
287      * names or color values (e.g. "white" or "#ffffff" or "ffffff").
288      *
289      * @param color A String.
290      * @return A TemplatePageAttributes (self).
291      */

292     public TemplatePageAttributes setLinkColor(String JavaDoc color)
293     {
294         String JavaDoc hexColor = HtmlColor.getColor(color);
295         if (hexColor == null)
296         {
297             hexColor = color;
298         }
299         data.getPage().getBody().setLink(hexColor);
300         return this;
301     }
302
303     /**
304      * Set the visited link color for the BODY tag.
305      *
306      * @param color A String.
307      * @return A TemplatePageAttributes (self).
308      */

309     public TemplatePageAttributes setVlinkColor(String JavaDoc color)
310     {
311         String JavaDoc hexColor = HtmlColor.getColor(color);
312         if (hexColor == null)
313         {
314             hexColor = color;
315         }
316         data.getPage().getBody().setVlink(hexColor);
317         return this;
318     }
319
320     /**
321      * Adds an attribute to the BODY tag.
322      *
323      * @param name A String.
324      * @param value A String.
325      * @return A TemplatePageAttributes (self).
326      */

327     public TemplatePageAttributes addAttribute(String JavaDoc name, String JavaDoc value)
328     {
329         data.getPage().getBody().addAttribute(name, value);
330         return this;
331     }
332
333     /**
334      * A dummy toString method that returns an empty string.
335      *
336      * @return An empty String ("").
337      */

338     public String JavaDoc toString()
339     {
340         return "";
341     }
342 }
343
Popular Tags