KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > MessageTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: MessageTag.java,v 1.10 2007/01/07 06:14:09 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.core.www;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 import org.opensubsystems.core.util.Messages;
31
32 /**
33  * Custom tag to display messages defined using call to
34  * CallContext.getInstance().getMessages().addXYZ() on the UI processed using
35  * servlet derived from WebUIServlet. Place this tag in your JSP page at the
36  * place where you want to display informational or error messages generated by
37  * application for the user. This tag will generate DIV element with informational
38  * and error messages in it that you can format using CSS. The default css classes
39  * for this tag are included in <%=contextpath%>/core/css/message.css.
40  * If you do not specify your own css styles, you should include this default
41  * css file whenever you use this tag.
42  *
43  * @version $Id: MessageTag.java,v 1.10 2007/01/07 06:14:09 bastafidli Exp $
44  * @author Miro Halas
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.7 2006/02/17 06:54:09 bastafidli
47  */

48 public class MessageTag extends TagSupport JavaDoc
49 {
50    // Constants ////////////////////////////////////////////////////////////////
51

52    /**
53     * Messages collected during processing of current request.
54     */

55    public static final String JavaDoc MESSAGES_REQUEST_PARAM = "messages";
56    
57    // Attributes ///////////////////////////////////////////////////////////////
58

59    /**
60     * Generated serial version id for this class.
61     */

62    private static final long serialVersionUID = 9202964907351336679L;
63
64    /**
65     * Id of the DIV where messages will be displayed. Subarea where error
66     * messages are displayed will have id created by appending error and subarea
67     * for information messages will have id created by appending info. Required.
68     */

69    protected String JavaDoc m_strId;
70    
71    /**
72     * Class for the message labels.
73     */

74    protected String JavaDoc m_strCssclass;
75    
76    /**
77     * Flag signaling to also include the message area if the area would be empty.
78     * If the message area should be included even if it is empty this should say
79     * true or 1.
80     */

81    protected String JavaDoc m_strEmpty;
82
83    // Constructors /////////////////////////////////////////////////////////////
84

85    /**
86     * Constructor for custom tag.
87     */

88    public MessageTag()
89    {
90       super();
91       
92       m_strId = "";
93       m_strCssclass = "clsMessages";
94    }
95    
96    // Business logic ///////////////////////////////////////////////////////////
97

98    /**
99     * {@inheritDoc}
100     */

101    public int doStartTag(
102    ) throws JspException JavaDoc
103    {
104       Messages msgs = (Messages)pageContext.getRequest().getAttribute(
105                           MessageTag.MESSAGES_REQUEST_PARAM);
106       
107       if ((msgs != null) || (isEmptyArea()))
108       {
109          List JavaDoc lstErrors = null;
110          List JavaDoc lstInfos = null;
111          boolean bErrors;
112          boolean bInfo;
113          
114          if (msgs != null)
115          {
116             lstErrors = msgs.getAllErrorMessages();
117             lstInfos = msgs.getInfoMessages();
118          }
119          bErrors = ((lstErrors != null) && (!lstErrors.isEmpty()));
120          bInfo = ((lstInfos != null) && (!lstInfos.isEmpty()));
121
122          if ((bErrors) || (bInfo) || (isEmptyArea()))
123          {
124             Iterator JavaDoc iterator;
125             String JavaDoc strMessage;
126             StringBuffer JavaDoc sbHtml = null;
127             
128             sbHtml = new StringBuffer JavaDoc();
129             sbHtml.append("<div id=\"");
130             sbHtml.append(m_strId);
131             sbHtml.append("\"");
132             if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
133             {
134                sbHtml.append(" class=\"");
135                sbHtml.append(m_strCssclass);
136                if ((msgs == null)
137                   || (((lstErrors == null) || (lstErrors.isEmpty()))
138                   && ((lstInfos == null) || (lstInfos.isEmpty()))))
139                {
140                   sbHtml.append("Empty");
141                }
142                sbHtml.append("\"");
143             }
144             sbHtml.append(">");
145
146             if ((bErrors) || (isEmptyArea()))
147             {
148                sbHtml.append("<div id=\"");
149                sbHtml.append(m_strId);
150                sbHtml.append("error\"");
151                if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
152                {
153                   sbHtml.append(" class=\"");
154                   sbHtml.append(m_strCssclass);
155                   sbHtml.append("Error\"");
156                }
157                sbHtml.append(">");
158                
159                if (lstErrors != null)
160                {
161                   for (iterator = lstErrors.iterator(); iterator.hasNext();)
162                   {
163                      strMessage = (String JavaDoc) iterator.next();
164                      sbHtml.append("<div>");
165                      sbHtml.append(strMessage);
166                      sbHtml.append("</div>\n");
167                   }
168                }
169                
170                sbHtml.append("</div>\n");
171             }
172             if ((bInfo) || (isEmptyArea()))
173             {
174                sbHtml.append("<div id=\"");
175                sbHtml.append(m_strId);
176                sbHtml.append("info\"");
177                if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
178                {
179                   sbHtml.append(" class=\"");
180                   sbHtml.append(m_strCssclass);
181                   sbHtml.append("Info\"");
182                }
183                sbHtml.append(">");
184                
185                if (lstInfos != null)
186                {
187                   for (iterator = lstInfos.iterator(); iterator.hasNext();)
188                   {
189                      strMessage = (String JavaDoc) iterator.next();
190                      sbHtml.append("<div>");
191                      sbHtml.append(strMessage);
192                      sbHtml.append("</div>\n");
193                   }
194                }
195                
196                sbHtml.append("</div>\n");
197             }
198             sbHtml.append("</div>\n");
199             TagUtils.write(pageContext, sbHtml.toString());
200          }
201       }
202       
203       return (SKIP_BODY);
204    }
205
206    /**
207     * {@inheritDoc}
208     */

209    public int doEndTag(
210    ) throws JspException JavaDoc
211    {
212       return (EVAL_PAGE);
213    }
214
215    /**
216     * @return String - Id of the DIV where messages will be displayed. Subarea
217     * where error messages are displayed will have id created
218     * by appending error and subarea for information messages
219     * will have id created by appending info.
220     */

221    public String JavaDoc getId(
222    )
223    {
224       return m_strId;
225    }
226
227    /**
228     * @return String - CSS class for the area where messages are displayed.
229     * Subarea where error messages are displayed will have class
230     * created by appending Error and subarea for information
231     * messages will have class created by appending Info
232     */

233    public String JavaDoc getCssclass(
234    )
235    {
236       return m_strCssclass;
237    }
238
239    /**
240     * @param string - Id of the DIV where messages will be displayed. Subarea
241     * where error messages are displayed will have id created
242     * by appending error and subarea for information messages
243     * will have id created by appending info.
244     */

245    public void setId(
246       String JavaDoc string
247    )
248    {
249       m_strId = string;
250    }
251
252    /**
253     * @param string - CSS class for the area where messages are displayed.
254     * Subarea where error messages are displayed will have class
255     * created by appending Error and subarea for information
256     * messages will have class created by appending Info.
257     */

258    public void setCssclass(String JavaDoc string)
259    {
260       m_strCssclass = string;
261    }
262
263    /**
264     * @return String - If the message area should be included even if it is empty
265     * this should say true or 1.
266     */

267    public String JavaDoc getEmpty(
268    )
269    {
270       return m_strEmpty;
271    }
272
273    /**
274     * @param strEmpty - If the message area should be included even if it is empty
275     * this should say true or 1.
276     */

277    public void setEmpty(
278       String JavaDoc strEmpty
279    )
280    {
281       m_strEmpty = strEmpty;
282    }
283    
284    /**
285     * @param bEmpty - If the message area should be included even if it is empty
286     * this should say true or 1.
287     */

288    public void setEmpty(
289       boolean bEmpty
290    )
291    {
292       m_strEmpty = Boolean.toString(bEmpty);
293    }
294
295    /**
296     * @return boolean - true if the message area should be included even if empty
297     */

298    public boolean isEmptyArea(
299    )
300    {
301       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strEmpty))
302              || ("1".equals(m_strEmpty)));
303    }
304 }
305
Popular Tags