KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: MessageListTag.java,v 1.7 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.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
30
31 import org.opensubsystems.core.util.Messages;
32
33 /**
34  * Custom tag to generate JavaScript array with list of messages, which should be
35  * displayed on the UI. The result will be two JavaScript arrays. The
36  * arrMessages will contain the messages and the arrMessageTypes will contain
37  * the types of messages (error, info).
38  *
39  * @version $Id: MessageListTag.java,v 1.7 2007/01/07 06:14:09 bastafidli Exp $
40  * @author Miro Halas
41  * @code.reviewer Miro Halas
42  * @code.reviewed 1.4 2006/02/17 06:54:09 bastafidli
43  */

44 public class MessageListTag extends TagSupport JavaDoc
45 {
46    // Attributes ///////////////////////////////////////////////////////////////
47

48    /**
49     * Generated serial version id for this class.
50     */

51    private static final long serialVersionUID = 2757149284634427196L;
52
53    // Constructors /////////////////////////////////////////////////////////////
54

55    /**
56     * Constructor for custom tag.
57     */

58    public MessageListTag()
59    {
60       super();
61    }
62    
63    // Business logic ///////////////////////////////////////////////////////////
64

65    /**
66     * {@inheritDoc}
67     */

68    public int doStartTag(
69    ) throws JspException JavaDoc
70    {
71       TagUtils.write(pageContext, generateMessageList(pageContext));
72       
73       return (SKIP_BODY);
74    }
75
76    /**
77     * {@inheritDoc}
78     */

79    public int doEndTag(
80    ) throws JspException JavaDoc
81    {
82       return (EVAL_PAGE);
83    }
84
85    /**
86     * Generate JavaScript representing list of messages.
87     *
88     * @param pageContext - context of the current page
89     * @return String - JavaScript representing list of messages
90     */

91    public static String JavaDoc generateMessageList(
92       PageContext JavaDoc pageContext
93    )
94    {
95       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
96       Messages msgs = (Messages)pageContext.getRequest().getAttribute(
97                           MessageTag.MESSAGES_REQUEST_PARAM);
98       
99       sbHtml.append("var arrMessages = new Array();\n" +
100                     "var arrMessageTypes = new Array();\n");
101       
102       if (msgs != null)
103       {
104          List JavaDoc lstErrors = msgs.getAllErrorMessages();
105          List JavaDoc lstInfos = msgs.getInfoMessages();
106          int iIndex = 0;
107          Iterator JavaDoc msgIter;
108
109          if ((lstErrors != null) && (!lstErrors.isEmpty()))
110          {
111             for (msgIter = lstErrors.iterator(); msgIter.hasNext(); iIndex++)
112             {
113                sbHtml.append("arrMessages[");
114                sbHtml.append(iIndex);
115                sbHtml.append("] = \"");
116                sbHtml.append(msgIter.next().toString());
117                sbHtml.append("\"\n");
118                sbHtml.append("arrMessageTypes[");
119                sbHtml.append(iIndex);
120                sbHtml.append("] = \"error\";\n");
121             }
122          }
123          if ((lstInfos != null) && (!lstInfos.isEmpty()))
124          {
125             for (msgIter = lstInfos.iterator(); msgIter.hasNext(); iIndex++)
126             {
127                sbHtml.append("arrMessages[");
128                sbHtml.append(iIndex);
129                sbHtml.append("] = \"");
130                sbHtml.append(msgIter.next().toString());
131                sbHtml.append("\"\n");
132                sbHtml.append("arrMessageTypes[");
133                sbHtml.append(iIndex);
134                sbHtml.append("] = \"info\";\n");
135             }
136          }
137       }
138       
139       return sbHtml.toString();
140    }
141 }
142
Popular Tags