KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > dialoglayout > www > DoubleSelectOptionsTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DoubleSelectOptionsTag.java,v 1.6 2007/01/07 06:14:28 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.patterns.dialoglayout.www;
23
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.net.URLDecoder JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
33
34 import org.opensubsystems.core.util.Log;
35 import org.opensubsystems.core.util.StringUtils;
36 import org.opensubsystems.core.www.BlockElementTag;
37 import org.opensubsystems.core.www.TagUtils;
38
39 /**
40  * Custom tag to generate all HTML code necessary to display options of the
41  * double select from a single string which contains concatenated and potentially
42  * encoded list of values and texts.
43  *
44  * @version $Id: DoubleSelectOptionsTag.java,v 1.6 2007/01/07 06:14:28 bastafidli Exp $
45  * @author Julian Legeny
46  * @code.reviewer Miro Halas
47  * @code.reviewed 1.3 2006/02/18 05:29:32 bastafidli
48  */

49 public class DoubleSelectOptionsTag extends BlockElementTag
50 {
51    // Attributes ///////////////////////////////////////////////////////////////
52

53    /**
54     * Generated serial version id for this class.
55     */

56    private static final long serialVersionUID = 2484149237972831841L;
57
58    /**
59     * Separator that will be used for separating of particular options,
60     * e.g. separator ',' will be used as: OPTION1, OPTION2, ... OPTIONn
61     */

62    protected String JavaDoc m_strSeparator;
63
64    /**
65     * Separator that will be used for separating items of particular options.
66     * If item separator is not specified, there will be applied values used
67     * as value and also as text to the option, e.g. separator ':' will be used
68     * as:
69     * ID_FOR_OPTION1:VALUE_FOR_OPTION1, ... ID_FOR_OPTIONn:VALUE_FOR_OPTIONn
70     */

71    protected String JavaDoc m_strItemSeparator;
72
73    /**
74     * Does value written in option has to be decoded or not. Value has to be
75     * decoded in case when there are used special characters and it will be hard
76     * to separate particular items with separators. If the value is encoded, it
77     * should be encoded using URLEncoder using UTF-8 character encoding.
78     */

79    protected String JavaDoc m_strDecode;
80
81    // Cached values ////////////////////////////////////////////////////////////
82

83    /**
84     * Logger for this class
85     */

86    private static Logger JavaDoc s_logger = Log.getInstance(DoubleSelectOptionsTag.class);
87
88    // Constructors /////////////////////////////////////////////////////////////
89

90    /**
91     * Constructor for custom tag.
92     */

93    public DoubleSelectOptionsTag()
94    {
95       super("", BlockElementTag.DIV_BLOCK_ELEMENT);
96       
97       m_strSeparator = null;
98       m_strItemSeparator = null;
99       m_strDecode = Boolean.FALSE.toString();
100    }
101    
102    // Business logic ///////////////////////////////////////////////////////////
103

104    /**
105     * {@inheritDoc}
106     */

107    public int doStartTag(
108    ) throws JspException JavaDoc
109    {
110       return (EVAL_BODY_BUFFERED);
111    }
112
113    /**
114     * {@inheritDoc}
115     */

116    public int doEndTag(
117    ) throws JspException JavaDoc
118    {
119       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
120       boolean bError;
121
122       BodyContent JavaDoc content = getBodyContent();
123       String JavaDoc strContent = content.getString().trim();
124
125       List JavaDoc lstFractions = null;
126       // create list of option fractions
127
lstFractions = StringUtils.parseStringToList(strContent, m_strSeparator);
128       
129       if (lstFractions != null && lstFractions.size() > 0)
130       {
131          Iterator JavaDoc itItem;
132          String JavaDoc strValue = null;
133          String JavaDoc strText = null;
134          String JavaDoc strFraction;
135
136          for (itItem = lstFractions.iterator(); itItem.hasNext();)
137          {
138             bError = false;
139             // get fraction string
140
strFraction = (String JavaDoc)itItem.next();
141             if (m_strItemSeparator != null)
142             {
143                // if item separator is defined we have to parse
144
// 2 things: value and text for option
145

146                // parse value for option
147
strValue = strFraction.substring(
148                              0, strFraction.indexOf(m_strItemSeparator));
149                // parse text for option
150
strText = strFraction.substring(
151                             strFraction.indexOf(m_strItemSeparator) + 1,
152                                                 strFraction.length());
153             }
154             else
155             {
156                // item separator is not defined and it means there are
157
// specified only values separated by separator and these
158
// values will be used both as VALUE and also as TEXT to
159
// the option
160
strValue = strFraction;
161                strText = strFraction;
162             }
163             
164             if (isDecodedOptionText())
165             {
166                try
167                {
168                   strText = URLDecoder.decode(strText, "UTF-8");
169                }
170                catch (UnsupportedEncodingException JavaDoc eUEExc)
171                {
172                   // log exception here and don't generate option
173
s_logger.log(Level.WARNING,
174                       "An error has occured while decoded text for option " + strText,
175                       eUEExc);
176
177                   bError = true;
178                }
179             }
180             // write option if there wasn't error occured
181
if (!bError)
182             {
183                sbHtml.append("<option value=\"");
184                sbHtml.append(strValue);
185                sbHtml.append("\">");
186                sbHtml.append(strText);
187                sbHtml.append("</option>\n");
188             }
189          }
190       }
191
192       TagUtils.write(pageContext, sbHtml.toString());
193       
194       return (EVAL_PAGE);
195    }
196    
197    /**
198     * @return String - Separator used for separating particular options
199     */

200    public String JavaDoc getSeparator()
201    {
202       return m_strSeparator;
203    }
204
205    /**
206     * @return String - Separator used for separating particular
207     * option items (value, text)
208     */

209    public String JavaDoc getItemSeparator()
210    {
211       return m_strItemSeparator;
212    }
213
214    /**
215     * @return String - Does text written in options has to be decoded or not
216     */

217    public String JavaDoc getDecode()
218    {
219       return m_strDecode;
220    }
221    
222    /**
223     * @param strSeparator - separator used for separating options
224     */

225    public void setSeparator(
226       String JavaDoc strSeparator
227    )
228    {
229       m_strSeparator = strSeparator;
230    }
231
232    /**
233     * @param strItemSeparator - separator used for separating
234     * option items (value, text)
235     */

236    public void setItemSeparator(
237       String JavaDoc strItemSeparator
238    )
239    {
240       m_strItemSeparator = strItemSeparator;
241    }
242
243    /**
244     * @param strDecode - If text for option has to be decoded say true or 1.
245     */

246    public void setDecode(
247       String JavaDoc strDecode
248    )
249    {
250       m_strDecode = strDecode;
251    }
252    
253    /**
254     * @param bDecode - Does text written in options has to be decoded or not
255     */

256    public void setDecode(
257       boolean bDecode
258    )
259    {
260       m_strDecode = Boolean.toString(bDecode);
261    }
262
263    /**
264     * @return boolean - true if text written in option is decoded
265     */

266    public boolean isDecodedOptionText(
267    )
268    {
269       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strDecode))
270              || ("1".equals(m_strDecode)));
271    }
272 }
273
Popular Tags