KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > table > tags > ColumnHeaderTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.table.tags;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28
29 import org.apache.struts.taglib.TagUtils;
30 import org.apache.struts.taglib.html.LinkTag;
31 import org.apache.struts.taglib.logic.IterateTag;
32
33 import com.sslexplorer.core.CoreUtil;
34 import com.sslexplorer.core.tags.FormTag;
35 import com.sslexplorer.table.Pager;
36
37 public class ColumnHeaderTag extends LinkTag {
38
39     /**
40      * Name of the bean that contains the {@link com.sslexplorer.table.Pager}
41      * object
42      */

43     protected String JavaDoc pagerName = null;
44
45     protected String JavaDoc attributesName;
46     protected String JavaDoc attributesProperties;
47
48     public String JavaDoc getPagerName() {
49         return (this.pagerName);
50     }
51
52     public void setPagerName(String JavaDoc pagerName) {
53         this.pagerName = pagerName;
54     }
55
56     /**
57      * Name of the property to be accessed on the specified bean that contains
58      * the {@link com.sslexplorer.table.Pager}.
59      */

60     protected String JavaDoc pagerProperty = null;
61
62     public String JavaDoc getPagerProperty() {
63         return (this.pagerProperty);
64     }
65
66     public void setPagerProperty(String JavaDoc pagerProperty) {
67         this.pagerProperty = pagerProperty;
68     }
69
70     /**
71      * The column index.
72      */

73     protected int columnIndex = 0;
74
75     public String JavaDoc getColumnIndex() {
76         return String.valueOf(this.columnIndex);
77     }
78
79     public void setColumnIndex(String JavaDoc columnIndex) {
80         this.columnIndex = Integer.parseInt(columnIndex);
81     }
82
83     // --------------------------------------------------------- Public Methods
84

85     // /**
86
// * Process the start tag.
87
// *
88
// * @exception JspException if a JSP exception has occurred
89
// */
90
// public int doStartTag() throws JspException {
91
//
92
// // Look up the requested bean (if necessary)
93
// if (ignore) {
94
// if (TagUtils.getInstance().lookup(pageContext, name, scope) == null) {
95
// return (SKIP_BODY); // Nothing to output
96
// }
97
// }
98
//
99
// // Look up the requested property value
100
// Object value = TagUtils.getInstance().lookup(pageContext, name, property,
101
// scope);
102
//
103
// if (value == null) {
104
// return (SKIP_BODY); // Nothing to output
105
// }
106
//
107
// // Convert value to the String with some formatting
108
// String output = "<a HREF=\"javascript: setActionTarget('resort');
109
// document.forms[0].submit();" + formatValue(value) + "</a>";
110
//
111
// // Print this property value to our output writer, suitably filtered
112
// if (filter) {
113
// TagUtils.getInstance().write(pageContext,
114
// TagUtils.getInstance().filter(output));
115
// } else {
116
// TagUtils.getInstance().write(pageContext, output);
117
// }
118
//
119
// // Continue processing this page
120
// return (SKIP_BODY);
121
//
122
// }
123

124     public int doEndTag() throws JspException JavaDoc {
125
126         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
127         Pager pager = (Pager) TagUtils.getInstance().lookup(pageContext, pagerName, pagerProperty, scope);
128         if (text != null) {
129             results.append(text);
130         }
131         if (pager != null) {
132             String JavaDoc name = pager.getModel().getColumnName(columnIndex);
133             if (name.equals(pager.getSortName())) {
134                 results.append("&nbsp;<img border=\"0\" SRC=\"");
135                 if (pager.getSortReverse()) {
136                     results.append(CoreUtil.getThemePath(pageContext.getSession()) + "/images/actions/descending.gif");
137                 } else {
138                     results.append(CoreUtil.getThemePath(pageContext.getSession()) + "/images/actions/ascending.gif");
139                 }
140                 results.append("\"/>");
141             }
142         }
143         results.append("</a>");
144
145         TagUtils.getInstance().write(pageContext, results.toString());
146         return (EVAL_PAGE);
147
148     }
149
150     /**
151      * Return the complete URL to which this hyperlink will direct the user.
152      * Support for indexed property since Struts 1.1
153      *
154      * @exception JspException if an exception is thrown calculating the value
155      */

156     protected String JavaDoc calculateURL() throws JspException JavaDoc {
157
158         // Identify the parameters we will add to the completed URL
159
Map JavaDoc params = TagUtils.getInstance().computeParameters(pageContext, paramId, paramName, paramProperty, paramScope, name,
160             property, scope, transaction);
161
162         // if "indexed=true", add "index=x" parameter to query string
163
// * @since Struts 1.1
164
if (indexed) {
165
166             // look for outer iterate tag
167
IterateTag iterateTag = (IterateTag) findAncestorWithClass(this, IterateTag.class);
168             if (iterateTag == null) {
169                 // This tag should only be nested in an iterate tag
170
// If it's not, throw exception
171
JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("indexed.noEnclosingIterate"));
172                 TagUtils.getInstance().saveException(pageContext, e);
173                 throw e;
174             }
175
176             // calculate index, and add as a parameter
177
if (params == null) {
178                 params = new HashMap JavaDoc(); // create new HashMap if no other params
179
}
180             if (indexId != null) {
181                 params.put(indexId, Integer.toString(iterateTag.getIndex()));
182             } else {
183                 params.put("index", Integer.toString(iterateTag.getIndex()));
184             }
185
186         }
187
188         // get the pager object
189
Pager pager = (Pager) TagUtils.getInstance().lookup(pageContext, pagerName, pagerProperty, scope);
190         if (pager != null) {
191             if (params == null) {
192                 params = new HashMap JavaDoc(); // create new HashMap if no other params
193
}
194             String JavaDoc name = pager.getModel().getId() + "." + pager.getModel().getColumnName(columnIndex);
195
196             // NOTE - These are now stored in the session anyway - BPS
197
params.put("pageSize", String.valueOf(pager.getPageSize()));
198             params.put("startRow", String.valueOf(pager.getStartRow()));
199             params.put("sortName", name);
200             params.put("sortReverse", name.equals(pager.getModel().getId() + "." + pager.getSortName()) ? String.valueOf(!pager.getSortReverse()) : "false");
201         }
202         
203         //
204

205         FormTag formTag = (FormTag)CoreUtil.getParentTagOfClass(FormTag.class, this);
206         if(formTag != null) {
207             if (params == null) {
208                 params = new HashMap JavaDoc();
209             }
210             params.put("subForm", formTag.getSubFormName());
211         }
212         
213         if(attributesName != null) {
214             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(attributesProperties, ",");
215             while (t.hasMoreTokens()) {
216                 String JavaDoc attr = t.nextToken();
217                 try {
218                     params.put(attr, TagUtils.getInstance().lookup(pageContext, attributesName, attr, scope).toString());
219                 } catch (Exception JavaDoc e) {
220                 }
221             }
222         }
223         
224         if(forward == null && page == null && action == null && href == null &&
225                         formTag != null) {
226             page = formTag.getAction();
227         }
228         
229         String JavaDoc url = null;
230         try {
231             url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href, page, action, module, params,
232                 anchor, false, useLocalEncoding);
233         } catch (MalformedURLException JavaDoc e) {
234             TagUtils.getInstance().saveException(pageContext, e);
235             throw new JspException JavaDoc(messages.getMessage("rewrite.url", e.toString()));
236         }
237         return (url);
238
239     }
240
241     /**
242      * Release all allocated resources.
243      */

244     public void release() {
245         super.release();
246         pagerName = null;
247         pagerProperty = null;
248         columnIndex = 0;
249         attributesName = null;
250         attributesProperties = null;
251
252     }
253     
254     public void setAttributesName(String JavaDoc attributesName) {
255         this.attributesName = attributesName;
256     }
257
258     public void setAttributesProperties(String JavaDoc attributesProperties) {
259         this.attributesProperties = attributesProperties;
260     }
261
262 }
263
Popular Tags