View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: OptionTag.java,v $
31   * Revision 1.3  2005/04/09 18:04:20  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.2  2005/01/19 13:14:04  colinmacleod
35   * Renamed CausedByException to SystemException.
36   *
37   * Revision 1.1  2005/01/06 23:10:06  colinmacleod
38   * Moved up a version number.
39   * Changed copyright notices to 2005.
40   * Updated the documentation:
41   *   - started working on multiproject:site docu.
42   *   - changed the logo.
43   * Added checkstyle and fixed LOADS of style issues.
44   * Added separate thirdparty subproject.
45   * Added struts (in web), util and webgui (in webtheme) from ivata op.
46   *
47   * Revision 1.4  2004/12/23 21:01:32  colinmacleod
48   * Updated Struts to v1.2.4.
49   * Changed base classes to use ivata masks.
50   *
51   * Revision 1.3  2004/11/03 16:12:09  colinmacleod
52   * Changed fieldPath to resourceFieldPath.
53   *
54   * Revision 1.2  2004/03/21 21:16:35  colinmacleod
55   * Shortened name to ivata op.
56   *
57   * Revision 1.1.1.1  2004/01/27 20:59:40  colinmacleod
58   * Moved ivata op to SourceForge.
59   *
60   * Revision 1.2  2003/10/16 15:43:03  jano
61   * Fixes problems with building and some problems with splitting to subprojects
62   *
63   * Revision 1.1.1.1  2003/10/13 20:49:26  colin
64   * Restructured portal into subprojects
65   *
66   * Revision 1.1  2003/03/03 16:57:12  colin
67   * converted localization to automatic paths
68   * added labels
69   * added mandatory fieldName attribute
70   * -----------------------------------------------------------------------------
71   */
72  package com.ivata.mask.web.tag.html;
73  import javax.servlet.jsp.JspException;
74  
75  import org.apache.struts.taglib.TagUtils;
76  
77  import com.ivata.mask.web.format.HTMLFormatter;
78  /***
79   * <p>
80   * Overrides an HTML <code>&lt;option&gt;</code> tag, by overriding the class
81   * from <strong>Struts </strong>.
82   * </p>
83   *
84   * <p>
85   * <b>Tag attributes: </b> <br/><table cellpadding='2' cellspacing='5'
86   * border='0' align='center' width='85%'>
87   * <tr class='TableHeadingColor'>
88   * <th>attribute</th>
89   * <th>reqd.</th>
90   * <th>param. class</th>
91   * <th width='100%'>description</th>
92   * </tr>
93   * <tr>
94   * <td>title</td>
95   * <td>false</td>
96   * <td><code>java.lang.String</code></td>
97   * <td>Title or tool-tip to be displayed when the mouse is over this option.
98   * </td>
99   * </tr>
100  * <tr>
101  * <td>readOnly</td>
102  * <td>false</td>
103  * <td><code>boolean</code></td>
104  * <td>Specifies the form elements should be displayed only and cannot be
105  * altered.</td>
106  * </tr>
107  * <tr>
108  * <td>valueKey</td>
109  * <td>true</td>
110  * <td><code>java.lang.String</code></td>
111  * <td>Specifies the localized text to use when the field is read-only.</td>
112  * </tr>
113  * </table>
114  * </p>
115  *
116  * <p>
117  * <b>Note: </b> all the tag attributes from {@link
118  * org.apache.struts.taglib.html.OptionTag Stuts} are also included.
119  * </p>
120  *
121  * @since ivata masks 0.4 (2003-01-15)
122  * @author Colin MacLeod
123  * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
124  * @version $Revision: 1.3 $ TODO: add titleKey
125  */
126 public class OptionTag extends org.apache.struts.taglib.html.OptionTag {
127     /***
128      * <p>
129      * Tooltip or title to be displayed when the mouse is over this option.
130      * </p>
131      */
132     private String title = null;
133     /***
134      * <p>
135      * Overridden to add the title attribute and set the value in text in the
136      * surrounding select tag.
137      * </p>
138      *
139      * @return <code>EVAL_PAGE</code> since we always want to evaluate the
140      *         page after this tag.
141      *
142      * @throws JspException
143      *             if there is an error wrting to <code>out.print()</code>
144      */
145     public int doEndTag() throws JspException {
146         // Acquire the select tag we are associated with
147         SelectTag selectTag = (SelectTag) findAncestorWithClass(this,
148                 SelectTag.class);
149         if (selectTag == null) {
150             throw new JspException(
151                     "ERROR in OptionTag: no surrounding select tag found.");
152         }
153         // Generate an HTML <option> element
154         StringBuffer results = new StringBuffer();
155         results.append("<option ");
156         results.append(HTMLFormatter.getAttributeNotNull("class", this
157                 .getStyleClass()));
158         results.append(HTMLFormatter.getBooleanAttribute("disabled", this
159                 .getDisabled()));
160         results.append(HTMLFormatter.getAttributeNotNull("id", this
161                 .getStyleId()));
162         results.append(HTMLFormatter.getBooleanAttribute("selected", selectTag
163                 .isMatched(getValue())));
164         results.append(HTMLFormatter.getAttributeNotNull("style", this
165                 .getStyle()));
166         results.append(HTMLFormatter.getAttributeNotNull("title", title));
167         results.append(HTMLFormatter.getAttributeNotNull("value", this
168                 .getValue()));
169         results.append(">");
170         String text = text();
171         if (text == null) {
172             text = getValue();
173         }
174         // if this is 'the one', set the current value to the select tag
175         if (selectTag.isMatched(getValue())) {
176             selectTag.setValueText(text);
177         }
178         results.append(text);
179         results.append("</option>");
180         // Render this element to our writer
181         TagUtils.getInstance().write(pageContext, results.toString());
182         // Continue evaluating this page
183         return (EVAL_PAGE);
184     }
185     /***
186      * <p>
187      * Tooltip or title to be displayed when the mouse is over this option.
188      * </p>
189      *
190      * @return the current value of title.
191      */
192     public String getTitle() {
193         return title;
194     }
195     /***
196      * <p>
197      * Tooltip or title to be displayed when the mouse is over this option.
198      * </p>
199      *
200      * @param titleParam
201      *            the new value of title.
202      */
203     public final void setTitle(final String titleParam) {
204         this.title = titleParam;
205     }
206 }
207