KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: BlockElementTag.java,v 1.16 2007/01/07 06:14:08 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.Stack JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27
28 /**
29  * Custom tag to generate all HTML code necessary to display block element (DIV
30  * or SPAN), for which we can set id and style and the ID is remembered so that
31  * child containers can access it and use it to generate compound IDs.
32  *
33  * @version $Id: BlockElementTag.java,v 1.16 2007/01/07 06:14:08 bastafidli Exp $
34  * @author Miro Halas
35  * @code.reviewer Miro Halas
36  * @code.reviewed 1.12 2006/02/17 06:54:09 bastafidli
37  */

38 public class BlockElementTag extends PageElementCacheTag
39 {
40    // Constants ////////////////////////////////////////////////////////////////
41

42    /**
43     * Name of the attribute representing stack storing ID of the current element.
44     */

45    public static final String JavaDoc CURRENT_ELEMENT_ID = "currentelementid";
46    
47    /**
48     * DIV block element.
49     */

50    public static final String JavaDoc DIV_BLOCK_ELEMENT = "div";
51    
52    /**
53     * SPAN block element.
54     */

55    public static final String JavaDoc SPAN_BLOCK_ELEMENT = "span";
56    
57    // Attributes ///////////////////////////////////////////////////////////////
58

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

62    private static final long serialVersionUID = -5162089287834734148L;
63
64    /**
65     * Class for the the whole element.
66     */

67    protected String JavaDoc m_strCssclass;
68    
69    /**
70     * Additional CSS style for the the whole element.
71     */

72    protected String JavaDoc m_strStyle;
73    
74    /**
75     * Type of the block element.
76     */

77    protected String JavaDoc m_strType;
78    
79    /**
80     * Suffix to append to id when generating HTML.
81     */

82    protected String JavaDoc m_strIdSuffix;
83
84    /**
85     * Flag signaling if the id of this element is related to the id of the
86     * parent element. If true then the current id is concatenated with the parent
87     * id when any code is being generated. If false then only the current id as
88     * setup for this element is being used. Null means false to speed up the code.
89     */

90    protected String JavaDoc m_strRelatedIds;
91    
92    /**
93     * The original value of m_strId if it was changed due to m_bRelatedIds.
94     */

95    private String JavaDoc m_strOriginalId;
96    
97    /**
98     * Flag signaling if ID was stored in the stack or not.
99     */

100    private boolean m_bIdStored;
101    
102    // Constructors /////////////////////////////////////////////////////////////
103

104    /**
105     * Constructor for custom tag.
106     *
107     * @param strCssclass - initial class of the element
108     * @param strType - type of tag - DIV or SPAN
109     */

110    public BlockElementTag(
111       String JavaDoc strCssclass,
112       String JavaDoc strType
113    )
114    {
115       super();
116       
117       // Do not use null, since if id is not set, we don't want to get NPE
118
m_strCssclass = strCssclass;
119       m_strStyle = "";
120       m_strType = strType;
121       m_strIdSuffix = "";
122       m_bIdStored = false;
123       // By default we do not want related ids since this is new feature and
124
// most code is not aware of it so we do not want to break code which
125
// expect ids to have some specific value
126
m_strRelatedIds = null;
127    }
128    
129    // Business logic ///////////////////////////////////////////////////////////
130

131    /**
132     * {@inheritDoc}
133     */

134    public int doStartTag(
135    ) throws JspException JavaDoc
136    {
137       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
138
139       adjustId();
140       
141       if ((m_strType == null) || (m_strType.length() == 0))
142       {
143          m_strType = DIV_BLOCK_ELEMENT;
144       }
145       
146       // Generate the start of the element
147
sbHtml.append("<");
148       sbHtml.append(m_strType);
149       sbHtml.append(" ");
150       if ((m_strId != null) && (m_strId.length() > 0))
151       {
152          sbHtml.append(" id=\"");
153          sbHtml.append(m_strId);
154          if ((m_strIdSuffix != null) && (m_strIdSuffix.length() > 0))
155          {
156             sbHtml.append(m_strIdSuffix);
157          }
158          sbHtml.append("\"");
159       }
160       if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
161       {
162          sbHtml.append(" class=\"");
163          sbHtml.append(m_strCssclass);
164          sbHtml.append("\"");
165       }
166       if ((m_strStyle != null) && (m_strStyle.length() > 0))
167       {
168          sbHtml.append(" style=\"");
169          sbHtml.append(m_strStyle);
170          sbHtml.append("\"");
171       }
172       sbHtml.append(">");
173       
174       pushCurrentId();
175       TagUtils.write(pageContext, sbHtml.toString());
176       
177       return (EVAL_BODY_INCLUDE);
178    }
179
180    /**
181     * {@inheritDoc}
182     */

183    public int doEndTag(
184    ) throws JspException JavaDoc
185    {
186       // Finish the element
187
StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
188       
189       sbHtml.append("</");
190       sbHtml.append(m_strType);
191       sbHtml.append(">");
192       
193       popCurrentId();
194       TagUtils.write(pageContext, sbHtml.toString());
195       
196       restoreId();
197       
198       return (EVAL_PAGE);
199    }
200    
201    /**
202     * @return String - Class for the the whole element
203     */

204    public String JavaDoc getCssclass(
205    )
206    {
207       return m_strCssclass;
208    }
209
210    /**
211     * @return String - type of the block element
212     */

213    public String JavaDoc getType(
214    )
215    {
216       return m_strType;
217    }
218
219    /**
220     * @return String - Additional CSS style for the the whole element.
221     */

222    public String JavaDoc getStyle()
223    {
224       return m_strStyle;
225    }
226    
227    /**
228     * @param strCssclass - Class for the the whole element
229     */

230    public void setCssclass(
231       String JavaDoc strCssclass
232    )
233    {
234       m_strCssclass = strCssclass;
235    }
236
237    /**
238     * @param strType - type of the block element
239     */

240    public void setType(
241       String JavaDoc strType
242    )
243    {
244       m_strType = strType;
245    }
246    
247    /**
248     * @param style - Additional CSS style for the the whole element.
249     */

250    public void setStyle(
251       String JavaDoc style
252    )
253    {
254       m_strStyle = style;
255    }
256
257    /**
258     * @return String - If id of this and parent tag should be related then this
259     * attribute should say true or 1.
260     */

261    public String JavaDoc getRelatedids(
262    )
263    {
264       return m_strRelatedIds;
265    }
266
267    /**
268     * @param strRelatedIds - If id of this and parent tag should be related then
269     * this attribute should say true or 1.
270     */

271    public void setRelatedids(
272       String JavaDoc strRelatedIds
273    )
274    {
275       m_strRelatedIds = strRelatedIds;
276    }
277    
278    /**
279     * @param bRelatedIds - If id of this and parent tag should be related then
280     * this attribute should say true or 1.
281     */

282    public void setRelatedids(
283       boolean bRelatedIds
284    )
285    {
286       m_strRelatedIds = Boolean.toString(bRelatedIds);
287    }
288
289    /**
290     * @return boolean - true if the id of this and parent tag are related.
291     */

292    public boolean isRelatedIdsTag(
293    )
294    {
295       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strRelatedIds))
296              || ("1".equals(m_strRelatedIds)));
297    }
298
299    // Helper methods ///////////////////////////////////////////////////////////
300

301    /**
302     * Store to the current id to be used by nested elements. This method
303     * is usually called from the doStartTag method.
304     */

305    protected void pushCurrentId(
306    )
307    {
308       Object JavaDoc objTemp;
309       Stack JavaDoc ids;
310
311       if ((m_strId != null) && (m_strId.length() > 0) && (!m_bIdStored))
312       {
313          // Store the ID only if it makes sense to store it
314
objTemp = pageContext.getAttribute(CURRENT_ELEMENT_ID);
315          if (objTemp == null)
316          {
317             ids = new Stack JavaDoc();
318             pageContext.setAttribute(CURRENT_ELEMENT_ID, ids);
319          }
320          else
321          {
322             ids = (Stack JavaDoc)objTemp;
323          }
324          ids.push(m_strId);
325          m_bIdStored = true;
326       }
327    }
328    
329    /**
330     * Restore to the current id to be used by nested elements. This method
331     * is usually called from the doEndTag method.
332     *
333     * @throws JspException - id was stored but cannot be found anymore
334     */

335    protected void popCurrentId(
336    ) throws JspException JavaDoc
337    {
338       Object JavaDoc objTemp;
339       Stack JavaDoc ids;
340
341       if (m_bIdStored)
342       {
343          // Store the ID only if it makes sense to store it
344
objTemp = pageContext.getAttribute(CURRENT_ELEMENT_ID);
345          if (objTemp == null)
346          {
347             throw new JspException JavaDoc("Cannot find element " + CURRENT_ELEMENT_ID
348                                    + " in page context even though it should be there.");
349          }
350          else
351          {
352             ids = (Stack JavaDoc)objTemp;
353          }
354          ids.pop();
355          m_bIdStored = false;
356       }
357    }
358
359    /**
360     * Restore to the current id to be used by nested elements. This method
361     * is usually called from the doEndTag method.
362     *
363     * @return String - current id pushed by parent element or empty string if
364     * none, never null
365     */

366    protected String JavaDoc getCurrentId(
367    )
368    {
369       Object JavaDoc objTemp;
370       Stack JavaDoc ids;
371       String JavaDoc strCurrentId = "";
372
373       // Store the ID only if it makes sense to store it
374
objTemp = pageContext.getAttribute(CURRENT_ELEMENT_ID);
375       if (objTemp != null)
376       {
377          ids = (Stack JavaDoc)objTemp;
378          if (!ids.empty())
379          {
380             strCurrentId = (String JavaDoc)ids.peek();
381          }
382       }
383       
384       return strCurrentId;
385    }
386    
387    /**
388     * Set up flag for stored ID
389     * @param bIdStored - flag signaling if id is stored or not
390     */

391    protected void setStoredId(
392       boolean bIdStored
393    )
394    {
395       m_bIdStored = bIdStored;
396    }
397    
398    /**
399     * Checks if the ids of this and parent tags are related and if they are,
400     * change the id of this tag to be concatenation with the parent id. If they
401     * are not, this method will not do anything. The restoreId method will
402     * reverse this change.
403     */

404    protected void adjustId(
405    )
406    {
407       if ((m_strRelatedIds != null) && (isRelatedIdsTag()))
408       {
409          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(getCurrentId());
410          
411          buffer.append(m_strId);
412          
413          m_strOriginalId = m_strId;
414          m_strId = buffer.toString();
415       }
416    }
417    
418    /**
419     * Restore the changed (if any) made by adjustId method.
420     */

421    protected void restoreId(
422    )
423    {
424       if (m_strOriginalId != null)
425       {
426          m_strId = m_strOriginalId;
427       }
428    }
429 }
430
Popular Tags