KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: HelpLabelTag.java,v 1.16 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.util.Properties JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
28
29 import org.opensubsystems.core.util.Config;
30 import org.opensubsystems.core.www.BlockElementTag;
31 import org.opensubsystems.core.www.TagUtils;
32
33 /**
34  * Custom tag to generate all HTML code necessary to display control label which
35  * serves as a trigger to display context help for some control associated with
36  * the label. The association between the control and its label is made using id,
37  * since both the label, the control and the context help should be constructed
38  * using the same id or placed within a dialog row or a column of dialog row with
39  * the same id.
40  *
41  * @version $Id: HelpLabelTag.java,v 1.16 2007/01/07 06:14:28 bastafidli Exp $
42  * @author Miro Halas
43  * @code.reviewer Miro Halas
44  * @code.reviewed 1.11 2006/02/18 05:29:32 bastafidli
45  */

46 public class HelpLabelTag extends BlockElementTag
47 {
48     // Configuration settings //////////////////////////////////////////////////
49

50    /**
51     * Configuration setting controlling if user interface allow user to focus
52     * (for example by tabbing through controls on the screen) on labels that
53     * can be used to display context help.
54     */

55    public static final String JavaDoc WEBUI_HELPLABEL_FOCUS = "oss.webui.helplabel.focus";
56
57    // Constants ////////////////////////////////////////////////////////////////
58

59    /**
60     * Default value of flag controlling if user interface allow user to focus
61     * (for example by tabbing through controls on the screen) on labels that
62     * can be used to display context help.
63     */

64    public static final boolean WEBUI_HELPLABEL_FOCUS_DEFAULT = false;
65
66    // Attributes ///////////////////////////////////////////////////////////////
67

68    /**
69     * Generated serial version id for this class.
70     */

71    private static final long serialVersionUID = -1158455519942369498L;
72
73    /**
74     * More text added behind the label
75     */

76    protected String JavaDoc m_strPostfix;
77
78    /**
79     * Accesskey that will be used for mnemonics
80     */

81    protected String JavaDoc m_strAccessKey;
82
83    /**
84     * AllowFocus flag that will be used for allowing to get focus for Label text
85     * of particular control. When there is switched between controls using tab key,
86     * the 'Label text' will be skipped. Default is false ('Label text' will be skipped).
87     * This attribute should say true or 1. Not required.
88     */

89    protected String JavaDoc m_strAllowFocus;
90
91    // Constructors /////////////////////////////////////////////////////////////
92

93    /**
94     * Constructor for custom tag.
95     */

96    public HelpLabelTag()
97    {
98       super("clsOtherStrechLabel", BlockElementTag.DIV_BLOCK_ELEMENT);
99
100       m_strStyle = "";
101       m_strPostfix = "";
102       m_strAccessKey = "";
103
104       // Load setting which specify if we allow focusing Help label tag belonging
105
// to particular control. This definition has to be in constructor
106
// and not in static definition because of tests.
107
Properties JavaDoc prpSettings;
108       
109       prpSettings = Config.getInstance().getPropertiesSafely();
110       m_strAllowFocus = Config.getBooleanPropertyAsString(
111                                   prpSettings,
112                                   WEBUI_HELPLABEL_FOCUS,
113                                   WEBUI_HELPLABEL_FOCUS_DEFAULT,
114                                   "Flag allowing for control labels to have focus");
115    }
116
117    // Business logic ///////////////////////////////////////////////////////////
118

119    /**
120     * {@inheritDoc}
121     */

122    public int doStartTag(
123    ) throws JspException JavaDoc
124    {
125       return (EVAL_BODY_BUFFERED);
126    }
127
128    /**
129     * {@inheritDoc}
130     */

131    public int doEndTag(
132    ) throws JspException JavaDoc
133    {
134       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
135       StringBuffer JavaDoc sbHtmlTemp = new StringBuffer JavaDoc();
136       
137       /*
138       <div id="sessionloginlabel" class="clsOtherStrechLabel">
139          <label id="sessionloginhelp" class="clsHelpLabel" accesskey="X"
140                 for="sessionlogin" title="Some title">
141             <a HREF="#sessionloginhelp" onclick="return false;" rel="Help"
142                rev="Help">System maintenance:</a>
143          </label>
144       </div>
145       */

146       sbHtml.append("<");
147       sbHtml.append(m_strType);
148       sbHtml.append(" id=\"");
149       sbHtml.append(getCurrentId());
150       sbHtml.append(m_strId);
151       sbHtml.append("label\"");
152       if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
153       {
154          sbHtml.append(" class=\"");
155          sbHtml.append(m_strCssclass);
156          sbHtml.append("\"");
157       }
158       if ((m_strStyle != null) && (m_strStyle.length() > 0))
159       {
160          sbHtml.append(" style=\"");
161          sbHtml.append(m_strStyle);
162          sbHtml.append("\"");
163       }
164       sbHtml.append("><label id=\"");
165       sbHtml.append(getCurrentId());
166       sbHtml.append(m_strId);
167       sbHtml.append("help\" class=\"clsHelpLabel\"");
168       if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
169       {
170          sbHtml.append(" accesskey=\"");
171          sbHtml.append(m_strAccessKey);
172          sbHtml.append("\" for=\"");
173          sbHtml.append(getCurrentId());
174          sbHtml.append("\"");
175       }
176       sbHtml.append(" title=\"Click here or press ALT+F1 when focused in the " +
177                     "related control to see context help\"><a HREF=\"");
178       sbHtml.append(getCurrentId());
179       sbHtml.append(m_strId);
180       sbHtml.append("help\" onclick=\"return false;\" rel=\"Help\" rev=\"Help\"");
181       if (!isFocusAllowed())
182       {
183          // If focus is not allowed for <a href> tag, add tabindex="-1" for particular tag.
184
// Value -1 of tabindex will allow to skip control while TAB key using.
185
// TODO: Bug: Mozilla. tabindex="-1" is supported from Mozilla 1.8
186
// http://www.mozilla.org/access/keyboard/tabindex.html
187
sbHtml.append(" tabindex=\"-1\"");
188       }
189       sbHtml.append(">");
190
191       
192       // Insert the buffered body
193
BodyContent JavaDoc content = getBodyContent();
194       int iCharIndex = -1;
195       String JavaDoc strContent = content.getString();
196       if (content != null)
197       {
198          // Look for first occurence of accesskey. First try looking for
199
// case sensitive and if not found then look for case insensitive.
200
// After char was found, surround it by underline tag <u></u>
201
if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
202          {
203             iCharIndex = strContent.indexOf(m_strAccessKey);
204             if (iCharIndex == -1)
205             {
206                iCharIndex = strContent.toLowerCase().indexOf(m_strAccessKey.toLowerCase());
207             }
208          }
209          if (iCharIndex == -1)
210          {
211             if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
212             {
213                // There was not found accesskey char within the label content.
214
// Create comment about it and add it into the html code.
215
sbHtmlTemp.append("\n<!-- Accesskey '");
216                sbHtmlTemp.append(m_strAccessKey);
217                sbHtmlTemp.append("' was not found within the label content for ");
218                sbHtmlTemp.append(getCurrentId());
219                sbHtmlTemp.append(m_strId);
220                sbHtmlTemp.append(" -->\n");
221             }
222          }
223          else
224          {
225             // There was found accesskey char within the label content.
226
// Replace its position with the same but underlined char.
227
sbHtmlTemp.append(strContent.substring(0, iCharIndex));
228             sbHtmlTemp.append("<u>");
229             sbHtmlTemp.append(strContent.charAt(iCharIndex));
230             sbHtmlTemp.append("</u>");
231             sbHtmlTemp.append(strContent.substring(iCharIndex + 1, strContent.length()));
232             strContent = sbHtmlTemp.toString();
233             sbHtmlTemp.delete(0, sbHtmlTemp.length());
234          }
235          sbHtml.append(strContent);
236       }
237       else
238       {
239          sbHtml.append("Missing context label for ");
240          sbHtml.append(getCurrentId());
241          sbHtml.append(m_strId);
242       }
243       
244       sbHtml.append("</a>");
245       // Add comment if there was not found accesskey char within the label content
246
sbHtml.append(sbHtmlTemp);
247       sbHtml.append("</label>");
248       if (m_strPostfix != null && m_strPostfix.length() > 0)
249       {
250          sbHtml.append("<span>");
251          sbHtml.append(m_strPostfix);
252          sbHtml.append("</span>");
253       }
254       sbHtml.append("</");
255       sbHtml.append(m_strType);
256       sbHtml.append('>');
257
258       TagUtils.write(pageContext, sbHtml.toString());
259
260       return (EVAL_PAGE);
261    }
262
263    /**
264     * @return - Extra text after label tag
265     */

266    public String JavaDoc getPostfix(
267    )
268    {
269       return m_strPostfix;
270    }
271
272    /**
273     * @param strPostfix - Extra text added after label tag
274     */

275    public void setPostfix(
276       String JavaDoc strPostfix
277    )
278    {
279       m_strPostfix = strPostfix;
280    }
281
282    /**
283     * @return String - access key used for mnemonics
284     */

285    public String JavaDoc getAccesskey(
286    )
287    {
288       return m_strAccessKey;
289    }
290
291    /**
292     * @param strAccessKey - access key that will be used in label for mnemonics
293     */

294    public void setAccesskey(
295       String JavaDoc strAccessKey
296    )
297    {
298       m_strAccessKey = strAccessKey;
299    }
300
301    /**
302     * @return String - flag that will be used for allowing to get focus for
303     * Label text
304     */

305    public String JavaDoc getAllowFocus(
306    )
307    {
308       return m_strAllowFocus;
309    }
310
311    /**
312     * @param strAllowFocus - If AllowFocus will be true there will be possible to
313     * focus to particular Label text. This attribute should
314     * say true or 1.
315     */

316    public void setAllowfocus(
317       String JavaDoc strAllowFocus
318    )
319    {
320       m_strAllowFocus = strAllowFocus;
321    }
322    
323    /**
324     * @param bAllowFocus - If AllowFocus will be true there will be possible to
325     * focus to particular Label text.
326     */

327    public void setAllowfocus(
328       boolean bAllowFocus
329    )
330    {
331       m_strAllowFocus = Boolean.toString(bAllowFocus);
332    }
333
334    /**
335     * @return boolean - true if focus for Help label text is allowed
336     */

337    public boolean isFocusAllowed(
338    )
339    {
340       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strAllowFocus))
341              || ("1".equals(m_strAllowFocus)));
342    }
343 }
344
Popular Tags