KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ScrollableTableJSTag.java,v 1.6 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 javax.servlet.jsp.JspException JavaDoc;
25
26 import org.opensubsystems.core.www.PageElementCacheTag;
27 import org.opensubsystems.core.www.TagUtils;
28
29 /**
30  * Custom tag to generate JavaScript required to interact with the scrollable
31  * table.
32  *
33  * Currently supported functions are
34  * onload - id - id of the scrollable table for which the JavaScript is generated
35  * - param1 - how to determine size of the table. Accepted values are:
36  * "fixed" - the table will have fixed height
37  * "fill" - the table will fill the whole content of the client
38  * area of the browser window
39  * - param2 - if param1 is "fixed", then this parameter will contain fixed
40  * width value in pixels of the table
41  * if param1 is "fill", then this parameter is ignored
42  * - param3 - if param1 is "fixed", then this parameter will contain number
43  * of rows that the table is going to contain (not only visible
44  * rows but total number of rows)
45  * if param1 is fill, then this parameter is ignored
46  * onresize - the same parameters as onload
47  *
48  * @version $Id: ScrollableTableJSTag.java,v 1.6 2007/01/07 06:14:16 bastafidli Exp $
49  * @author Miro Halas
50  * @code.reviewer Miro Halas
51  * @code.reviewed 1.3 2006/02/18 05:29:32 bastafidli
52  */

53 public class ScrollableTableJSTag extends PageElementCacheTag
54 {
55    // Attributes ///////////////////////////////////////////////////////////////
56

57    /**
58     * Generated serial version id for this class.
59     */

60    private static final long serialVersionUID = -2507154189971104193L;
61
62    /**
63     * Name of the function to generate the javascript for. Currently supported
64     * functions are onload, onresize.
65     */

66    protected String JavaDoc m_strFunction;
67
68    /**
69     * Parameter 1 to pass to the function if it allows it.
70     */

71    protected String JavaDoc m_strParam1;
72
73    /**
74     * Parameter 2 to pass to the function if it allows it.
75     */

76    protected int m_iParam2;
77
78    /**
79     * Parameter 3 to pass to the function if it allows it.
80     */

81    protected int m_iParam3;
82
83    // Constructors /////////////////////////////////////////////////////////////
84

85    /**
86     * Constructor for custom tag.
87     */

88    public ScrollableTableJSTag()
89    {
90       super();
91       m_strParam1 = null;
92       m_iParam2 = 0;
93       m_iParam3 = 0;
94    }
95    
96    // Business logic ///////////////////////////////////////////////////////////
97

98    /**
99     * {@inheritDoc}
100     */

101    public int doStartTag(
102    ) throws JspException JavaDoc
103    {
104       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
105
106       if (("onload".equalsIgnoreCase(m_strFunction))
107          || ("onresize".equalsIgnoreCase(m_strFunction)))
108       {
109          if ((m_strParam1 == null) || (m_strParam1.length() == 0))
110          {
111             // By default we construct fixed height table
112
m_strParam1 = "fixed";
113          }
114          if ((m_iParam2 <= 0))
115          {
116             // And the
117
m_iParam2 = 200;
118          }
119
120          /*
121          For onload we need to initialize the size of the table
122           
123          iRowsNumber[getTableIndex('scrollabletable')] = 0;
124          <logic:notEmpty name="rolelist">
125             iRowsNumber[getTableIndex('scrollabletable')] = <%= rolelist.size() %>;
126          </logic:notEmpty>
127
128          For fill the script looks like this for both resize and onload
129  
130          var iContentAreaHeight;
131          var bExact = false;
132
133          iContentAreaHeight = resizeScrollableTableContentArea('scrollabletable');
134
135          For fixed the script looks like this for both resize and reload
136          
137          var iContentAreaHeight = 200;
138          var bExact = true;
139
140          This is common for both fixed and fill
141          var arColumnsSizes;
142          
143          resizeScrollableTable('scrollabletable', iContentAreaHeight, bExact);
144          // First find out how wide are the columns
145          arColumnsSizes = retrieveColumnHeadingSizes('scrollabletablebodycolumns');
146          // Now try to set the headers to match the columns
147          setColumnHeadingSizes('scrollabletableheader', arColumnsSizes);
148          */

149
150          // Include the name of the table in the names of variables so that in
151
// case there are multiple tables we do not conflict
152
if ("fill".equalsIgnoreCase(m_strParam1))
153          {
154             sbHtml.append("var iContentAreaHeight");
155             sbHtml.append(m_strId);
156             sbHtml.append(";\n");
157             sbHtml.append("var bExact");
158             sbHtml.append(m_strId);
159             sbHtml.append(" = false;\n");
160             sbHtml.append("iContentAreaHeight");
161             sbHtml.append(m_strId);
162             sbHtml.append(" = resizeScrollableTableContentArea('");
163             sbHtml.append(m_strId);
164             sbHtml.append("');\n");
165          }
166          else if ("fixed".equalsIgnoreCase(m_strParam1))
167          {
168             if ("onload".equalsIgnoreCase(m_strFunction))
169             {
170                if (m_iParam3 <= 0)
171                {
172                   sbHtml.append("<!-- Unknown number of rows for table with fixed" +
173                                 " size, use param3 to specify number of rows -->\n");
174                   m_iParam3 = 0;
175                }
176                // This variable is defined in scrolltable.js
177
sbHtml.append("iRowsNumber[getTableIndex('");
178                sbHtml.append(m_strId);
179                sbHtml.append("')] = ");
180                sbHtml.append(m_iParam3);
181                sbHtml.append(";\n");
182             }
183             
184             sbHtml.append("var iContentAreaHeight");
185             sbHtml.append(m_strId);
186             sbHtml.append(" = ");
187             sbHtml.append(m_iParam2);
188             sbHtml.append(";\n");
189             sbHtml.append("var bExact");
190             sbHtml.append(m_strId);
191             sbHtml.append(" = true;\n");
192          }
193          else
194          {
195             throw new JspException JavaDoc("For method " + m_strFunction
196                          + " param2 has to be either fixed or fill.");
197          }
198          sbHtml.append("var arColumnsSizes");
199          sbHtml.append(m_strId);
200          sbHtml.append(";\n");
201
202          sbHtml.append("resizeScrollableTable('");
203          sbHtml.append(m_strId);
204          sbHtml.append("', iContentAreaHeight");
205          sbHtml.append(m_strId);
206          sbHtml.append(", bExact");
207          sbHtml.append(m_strId);
208          sbHtml.append(");\n");
209          
210          sbHtml.append("arColumnsSizes");
211          sbHtml.append(m_strId);
212          sbHtml.append(" = retrieveColumnHeadingSizes('");
213          sbHtml.append(m_strId);
214          sbHtml.append("bodycolumns');\n");
215          
216          sbHtml.append("setColumnHeadingSizes('");
217          sbHtml.append(m_strId);
218          sbHtml.append("header', arColumnsSizes");
219          sbHtml.append(m_strId);
220          sbHtml.append(");");
221       }
222       else
223       {
224          sbHtml.append("<!-- Scrollable table doesn't support method ");
225          sbHtml.append(m_strFunction);
226          sbHtml.append(" with parameters ");
227          if (m_strId != null)
228          {
229             sbHtml.append(" param1=");
230             sbHtml.append(m_strParam1);
231             sbHtml.append(" ");
232          }
233          else
234          {
235             sbHtml.append(" param1=null ");
236          }
237          sbHtml.append(" param2=");
238          sbHtml.append(m_iParam2);
239          sbHtml.append(" ");
240          sbHtml.append(" -->");
241       }
242       
243       TagUtils.write(pageContext, sbHtml.toString());
244                
245       return (SKIP_BODY);
246    }
247
248    /**
249     * {@inheritDoc}
250     */

251    public int doEndTag(
252    ) throws JspException JavaDoc
253    {
254       return (EVAL_PAGE);
255    }
256
257    /**
258     * @return String - Name of the function to generate the javascript for.
259     * Currently supported functions are onload, onresize.
260     */

261    public String JavaDoc getFunction(
262    )
263    {
264       return m_strFunction;
265    }
266
267    /**
268     * @return String - Value of parameter 1 if the function requires it.
269     */

270    public String JavaDoc getParam1(
271    )
272    {
273       return m_strParam1;
274    }
275
276    /**
277     * @return int - Value of parameter 2 if the function requires it.
278     */

279    public int getParam2(
280    )
281    {
282       return m_iParam2;
283    }
284
285    /**
286     * @return int - Value of parameter 3 if the function requires it.
287     */

288    public int getParam3(
289    )
290    {
291       return m_iParam3;
292    }
293
294    /**
295     * @param strFunction - Name of the function to generate the javascript for.
296     * Currently supported functions are onload, onresize,
297     * ondialoghelp.
298     */

299    public void setFunction(
300       String JavaDoc strFunction
301    )
302    {
303       m_strFunction = strFunction;
304    }
305
306    /**
307     * @param strParam1 - Value of parameter 1 if the function requires it.
308     */

309    public void setParam1(
310       String JavaDoc strParam1
311    )
312    {
313       m_strParam1 = strParam1;
314    }
315
316    /**
317     * @param iParam2 - Value of parameter 2 if the function requires it.
318     */

319    public void setParam2(
320       int iParam2
321    )
322    {
323       m_iParam2 = iParam2;
324    }
325
326    /**
327     * @param iParam3 - Value of parameter 2 if the function requires it.
328     */

329    public void setParam3(
330       int iParam3
331    )
332    {
333       m_iParam3 = iParam3;
334    }
335 }
336
Popular Tags