KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > rssticker > HtmlRssTickerRenderer


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

16 package org.apache.myfaces.custom.rssticker;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.context.ResponseWriter;
24
25 import org.apache.commons.digester.rss.Channel;
26 import org.apache.commons.digester.rss.Item;
27
28 import org.apache.myfaces.renderkit.RendererUtils;
29 import org.apache.myfaces.renderkit.html.HTML;
30 import org.apache.myfaces.renderkit.html.HtmlRenderer;
31 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
32 import org.apache.myfaces.util.MessageUtils;
33
34 /**
35  * @author mwessendorf (latest modification by $Author: matzew $)
36  * @version $Revision: 1.7 $ $Date: 2005/02/22 13:41:10 $ $Log:
37  * HtmlRssTickerRenderer.java,v $ Revision 1.4 2004/07/01 21:53:10
38  * mwessendorf ASF switch
39  *
40  * Revision 1.3 2004/06/27 22:06:25 mwessendorf Log
41  *
42  *
43  */

44 public class HtmlRssTickerRenderer extends HtmlRenderer {
45
46     public static final String JavaDoc RENDERER_TYPE = "org.apache.myfaces.Ticker";
47     private static final String JavaDoc NO_CONNECTION = "org.apache.myfaces.ticker.NOCONNECTION";
48
49     public void encodeEnd(FacesContext facesContext, UIComponent component)
50             throws IOException JavaDoc {
51         RendererUtils.checkParamValidity(facesContext, component,
52                 HtmlRssTicker.class);
53
54         HtmlRssTicker tickerComponent = (HtmlRssTicker) component;
55
56         ResponseWriter writer = facesContext.getResponseWriter();
57
58         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
59
60         tableBegin(facesContext, tickerComponent, writer);
61         rowBody(facesContext, tickerComponent, writer);
62         tableEnd(facesContext, tickerComponent, writer);
63
64     }
65
66     //-------------------------------------------------private Methods
67
/**
68      * @param facesContext
69      * @param tickerComponent
70      * @param writer
71      */

72     private void tableEnd(FacesContext facesContext,
73             HtmlRssTicker tickerComponent, ResponseWriter writer)
74             throws IOException JavaDoc {
75         writer.endElement(HTML.TBODY_ELEM);
76         writer.endElement(HTML.TABLE_ELEM);
77     }
78
79     /**
80      * @param facesContext
81      * @param tickerComponent
82      * @param writer
83      */

84     private void rowBody(FacesContext facesContext,
85             HtmlRssTicker tickerComponent, ResponseWriter writer)
86             throws IOException JavaDoc {
87
88         Channel channel = tickerComponent.getChannel();
89
90         //check if a connection to the internet is possible
91
if (channel != null) {
92             writer.startElement(HTML.THEAD_ELEM, tickerComponent);
93             writer.startElement(HTML.TR_ELEM, tickerComponent);
94             writer.startElement(HTML.TH_ELEM, tickerComponent);
95
96             writer.write((channel.getTitle() != null) ? channel.getTitle() : "");
97
98             writer.endElement(HTML.TH_ELEM);
99             writer.endElement(HTML.TR_ELEM);
100             writer.endElement(HTML.THEAD_ELEM);
101
102             Item[] columns = tickerComponent.items();
103             for (int i = 0; i < columns.length; i++) {
104
105                 rowBegin(facesContext, tickerComponent, writer);
106
107                 writer.startElement(HTML.TD_ELEM, tickerComponent);
108                 writer.write("<a HREF=\"" + columns[i].getLink()
109                         + "\" target=\"_new\">");
110                 writer.write(columns[i].getTitle());
111                 writer.write("</a>");
112                 writer.endElement(HTML.TD_ELEM);
113                 rowEnd(facesContext, tickerComponent, writer);
114             }
115         } else {
116             //perhaps we are behind a firewall,
117
//no connection - so we going to send a i18n info to the user-
118
FacesMessage facesMessage = MessageUtils.getMessage(FacesMessage.SEVERITY_INFO,NO_CONNECTION,null);
119             writer.write(facesMessage.getSummary());
120             writer.write("<br/>");
121             writer.write(facesMessage.getDetail());
122         }
123     }
124
125     /**
126      * @param facesContext
127      * @param tickerComponent
128      * @param writer
129      */

130     private void tableBegin(FacesContext facesContext,
131             HtmlRssTicker tickerComponent, ResponseWriter writer)
132             throws IOException JavaDoc {
133         writer.startElement(HTML.TABLE_ELEM, tickerComponent);
134         writer.startElement(HTML.TBODY_ELEM, tickerComponent);
135     }
136
137     /**
138      * @param facesContext
139      * @param tickerComponent
140      * @param writer
141      */

142     private void rowEnd(FacesContext facesContext,
143             HtmlRssTicker tickerComponent, ResponseWriter writer)
144             throws IOException JavaDoc {
145         writer.endElement(HTML.TR_ELEM);
146     }
147
148     /**
149      * @param facesContext
150      * @param tickerComponent
151      * @param writer
152      */

153     private void rowBegin(FacesContext facesContext,
154             HtmlRssTicker tickerComponent, ResponseWriter writer)
155             throws IOException JavaDoc {
156         writer.startElement(HTML.TR_ELEM, tickerComponent);
157     }
158
159 }
Popular Tags