KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > scrollabletable > www > ScrollableTableTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ScrollableTableTag.java,v 1.19 2007/01/07 06:14:16 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.scrollabletable.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 scrollable table.
35  * This is the main tag which represents the table itself. The contents of the
36  * table will be created from the content of the body of this tag.
37  *
38  * @version $Id: ScrollableTableTag.java,v 1.19 2007/01/07 06:14:16 bastafidli Exp $
39  * @author Miro Halas
40  * @code.reviewer Miro Halas
41  * @code.reviewed 1.15 2006/06/16 10:27:30 jlegeny
42  */

43 public class ScrollableTableTag extends BlockElementTag
44 {
45    // Configuration settings ///////////////////////////////////////////////////
46

47    /**
48     * Configuration setting for a flag controlling if scrollable tables has a
49     * checkbox in each line.
50     */

51    public static final String JavaDoc WEBUI_LIST_CHECK = "oss.webui.list.check";
52
53    /**
54     * Configuration setting for a flag controlling if rows of a scrollable table
55     * can be sorted by clicking on a column heading.
56     */

57    public static final String JavaDoc WEBUI_LIST_SORTABLE = "oss.webui.list.sortable";
58
59    // Constants ////////////////////////////////////////////////////////////////
60

61    /**
62     * Default value of flag that allows to show checkboxes for each line in the
63     * table.
64     */

65    public static final boolean WEBUI_LIST_CHECK_DEFAULT = true;
66
67    /**
68     * Default value of flag that allows to table columns to be sortable.
69     */

70    public static final boolean WEBUI_LIST_SORTABLE_DEFAULT = true;
71
72    /**
73     * Id of the active tabbed dialog which will be used by all other related
74     * tags.
75     */

76    public static final String JavaDoc ACTIVE_SCROLLABLE_TABLE_ID = "activescrollabletableid";
77
78    /**
79     * The control which should allow to show checkboxes for each line
80     * in the table.
81     */

82    public static final String JavaDoc SCROLLABLE_TABLE_CHECK = "scrollabletablecheck";
83
84    /**
85     * Name of property for table columns that can be sortable
86     */

87    public static final String JavaDoc SCROLLABLE_TABLE_SORTABLE = "scrollabletablesortable";
88
89    /**
90     * Name of property for assigned table rows limit value
91     */

92    public static final String JavaDoc SCROLLABLE_TABLE_ROW_LIMIT = "scrollabletablerowlimit";
93
94    /**
95     * Name of the table that has no specified ID
96     */

97    public static final String JavaDoc SCROLLABLE_TABLE_PREFIX = "unknowntableid";
98
99    // Attributes ///////////////////////////////////////////////////////////////
100

101    /**
102     * Generated serial version id for this class.
103     */

104    private static final long serialVersionUID = 3264660649363019353L;
105
106    /**
107     * Flag signaling if each row of the table should contain a checkbox in its
108     * first column. These checkboxes will be used for selecting particular item
109     * (row) in the table. This attribute should say true or 1 if checkbox should
110     * be present. This attribute works only if the content of the scrollable table
111     * is generated using custom tags derived from ListTag class.
112     */

113    protected String JavaDoc m_strCheck;
114
115    /**
116     * Flag signaling if the list can be sorted by user clicking on a column header.
117     * This attribute should say true or 1 if table content can be sorted.
118     */

119    protected String JavaDoc m_strSortable;
120
121    /**
122     * How many rows should the table contain at most. This value is used by
123     * various JavaScripts dynamically modifying the content of the table.
124     * 0 means no limit.
125     */

126    protected String JavaDoc m_strLimit;
127
128    // Constructors /////////////////////////////////////////////////////////////
129

130    /**
131     * Constructor for custom tag.
132     */

133    public ScrollableTableTag()
134    {
135       // This table is most often used embedded in other page so use the
136
// css style for this type of tables as default
137
super("clsEmbeddedListDynamic", BlockElementTag.DIV_BLOCK_ELEMENT);
138
139       // Load setting which specify if we can use checkboxes for each line.
140
// This definition has to be in constructor
141
// and not in static definition because of tests.
142
Properties JavaDoc prpSettings;
143       
144       prpSettings = Config.getInstance().getPropertiesSafely();
145       m_strCheck = Config.getBooleanPropertyAsString(
146                              prpSettings,
147                              WEBUI_LIST_CHECK,
148                              WEBUI_LIST_CHECK_DEFAULT,
149                              "Flag allowing for scrollable tables to have checkbox"
150                              + " in each line");
151       
152       m_strSortable = Config.getBooleanPropertyAsString(
153                              prpSettings,
154                              WEBUI_LIST_SORTABLE,
155                              WEBUI_LIST_SORTABLE_DEFAULT,
156                              "Flag allowing for scrollable tables to be sortable"
157                              + " using column headings");
158       
159       m_strLimit = "";
160    }
161
162    // Business logic ///////////////////////////////////////////////////////////
163

164    /**
165     * {@inheritDoc}
166     */

167    public int doStartTag(
168    ) throws JspException JavaDoc
169    {
170       super.doStartTag();
171       
172       // Save the id to the page context so that other related tags don't have to
173
// specify it
174
cache(ACTIVE_SCROLLABLE_TABLE_ID, m_strId);
175       // Cache value for showing checkboxes for each line
176
cache(SCROLLABLE_TABLE_CHECK, m_strCheck);
177       // Cache value for allowing sortable table columns
178
cache(SCROLLABLE_TABLE_SORTABLE, m_strSortable);
179       // Cache value that stores limit for number of assigned rows
180
cache(SCROLLABLE_TABLE_ROW_LIMIT, m_strLimit);
181
182       return (EVAL_BODY_BUFFERED);
183    }
184
185    /**
186     * {@inheritDoc}
187     */

188    public int doEndTag(
189    ) throws JspException JavaDoc
190    {
191       // Get the buffered body and replace all occurences of 'unknowntableid'
192
// generated within the ListTag by actual table name. The real table wasn't
193
// known in the ListTag so we have just generated placeholders that will be
194
// rewrited now.
195
BodyContent JavaDoc content = getBodyContent();
196       StringBuffer JavaDoc sbContent = new StringBuffer JavaDoc();
197       sbContent.append(content.getString());
198       String JavaDoc strContent;
199       
200       if ((m_strLimit != null) && (m_strLimit.length() > 0))
201       {
202          // if limit is specified generate particular input hidden parameter
203
// storing limit value
204
sbContent.append("<input type=\"hidden\" id=\"");
205          sbContent.append(m_strId);
206          sbContent.append("limit\" value=\"");
207          sbContent.append(m_strLimit);
208          sbContent.append("\">");
209       }
210       strContent = sbContent.toString();
211       
212       if (content != null)
213       {
214          strContent = strContent.replaceAll(SCROLLABLE_TABLE_PREFIX , m_strId);
215       }
216       // Remove the attributes from cache since we are done with this scrollable table
217

218       // Remove cached value for table id
219
getCachedContent(ACTIVE_SCROLLABLE_TABLE_ID, true);
220       // Remove cached value for allowing checkboxes for each line in the table
221
getCachedContent(ScrollableTableTag.SCROLLABLE_TABLE_CHECK, true);
222       // Remove cached value for allowing sortable table columns
223
getCachedContent(ScrollableTableTag.SCROLLABLE_TABLE_SORTABLE, true);
224       
225       TagUtils.write(pageContext, strContent);
226
227       super.doEndTag();
228
229       return (EVAL_PAGE);
230    }
231
232    /**
233     * @return String - If checkbox will be present in each row then this attribute
234     * should say true or 1. This attribute works only if the
235     * content of the scrollable table is generated using custom
236     * tags derived from ListTag class.
237     */

238    public String JavaDoc getCheck(
239    )
240    {
241       return m_strCheck;
242    }
243
244    /**
245     * @param strCheck - If checkbox will be present in each row then this attribute
246     * should say true or 1. This attribute works only if the
247     * content of the scrollable table is generated using custom
248     * tags derived from ListTag class.
249     */

250    public void setCheck(
251       String JavaDoc strCheck
252    )
253    {
254       m_strCheck = strCheck;
255    }
256    
257    /**
258     * @param bCheck - If checkbox will be present in each row then this attribute
259     * should say true or 1. This attribute works only if the
260     * content of the scrollable table is generated using custom
261     * tags derived from ListTag class.
262     */

263    public void setCheck(
264       boolean bCheck
265    )
266    {
267       m_strCheck = Boolean.toString(bCheck);
268    }
269
270    /**
271     * @return boolean - true if checkbox will be present in each row of the table
272     * This attribute works only if the content of the scrollable
273     * table is generated using custom tags derived from ListTag
274     * class.
275     */

276    public boolean isCheckActive(
277    )
278    {
279       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strCheck))
280              || ("1".equals(m_strCheck)));
281    }
282    
283    /**
284     * @return String - If table columns have to be sortable say true or 1.
285     */

286    public String JavaDoc getSortable(
287    )
288    {
289       return m_strSortable;
290    }
291
292    /**
293     * @param strSortable - If table columns have to be sortable say true or 1.
294     */

295    public void setSortable(
296       String JavaDoc strSortable
297    )
298    {
299       m_strSortable = strSortable;
300    }
301    
302    /**
303     * @param bSortable - If table columns have to be sortable say true or 1.
304     */

305    public void setSortable(
306       boolean bSortable
307    )
308    {
309       m_strSortable = Boolean.toString(bSortable);
310    }
311
312    /**
313     * @return boolean - true if table columns will be sortable
314     */

315    public boolean isSortableActive(
316    )
317    {
318       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strSortable))
319              || ("1".equals(m_strSortable)));
320    }
321    
322    /**
323     * @return String - get limit of assigned items.
324     */

325    public String JavaDoc getLimit(
326    )
327    {
328       return m_strLimit;
329    }
330
331    /**
332     * @param strLimit - limit of assigned items.
333     */

334    public void setLimit(
335       String JavaDoc strLimit
336    )
337    {
338       m_strLimit = strLimit;
339    }
340 }
341
Popular Tags