1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * > http://www.gnu.org/copyleft/lesser.html >
18 * http://www.opensource.org/licenses/lgpl-license.php
19 */
20 package net.mlw.vlh.web.tag.support;
21
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.jsp.JspException;
28
29 import net.mlw.vlh.ValueListInfo;
30 import net.mlw.vlh.web.ValueListConfigBean;
31 import net.mlw.vlh.web.tag.TableInfo;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /***
37 * @author Matthew L. Wilson, Andrej Zachar
38 * @version $Revision: 1.20 $ $Date: 2005/11/23 14:37:15 $
39 */
40 public class HtmlDisplayProvider extends AbstractHTMLDisplayProvider
41 {
42 /***
43 * Logger for this class
44 */
45 private static final Log LOGGER = LogFactory.getLog(HtmlDisplayProvider.class);
46
47 private String imageHome = "images";
48
49 private boolean preAppendContextPath = false;
50
51 private boolean usePadding = true;
52
53 private boolean useNoWrap = true;
54
55 /***
56 * @see #setImageHome(String)
57 * @return base image home.
58 */
59 public String getImageHome()
60 {
61 return imageHome;
62 }
63
64 /***
65 * @see #setPreAppendContextPath(boolean)
66 * @return true, false
67 */
68 public boolean isPreAppendContextPath()
69 {
70 return preAppendContextPath;
71 }
72
73 /***
74 *
75 * @return
76 */
77 public boolean isUseNoWrap()
78 {
79 return useNoWrap;
80 }
81
82 /***
83 *
84 * @return
85 */
86 public boolean isUsePadding()
87 {
88 return usePadding;
89 }
90
91 /***
92 * Pre append to <code>imageHome</code> property application context dir.
93 * Default is set to false due to backward compatibility.
94 * <h4>Example
95 * <h4>Considere that valuelist is the name of application context.
96 * <ul>
97 * <li>true</li>
98 * <b>valuelist/ </b>images
99 * <li>false</li>
100 * images
101 * </ul>
102 *
103 * @param preAppendContextPath
104 */
105 public void setPreAppendContextPath(boolean preAppendAppDir)
106 {
107 this.preAppendContextPath = preAppendAppDir;
108 }
109
110 /***
111 * Set the default path for images used in paging, focusing and so on.
112 * Default is set to <code>images</code>.
113 *
114 * @see #setPreAppendContextPath(boolean)
115 * @param dir
116 * <h4>Example
117 * <h4>
118 * <ul>
119 * <li>/myProject/images/microsoftLook</li>
120 * generate html for image like this < alt="Sort"
121 * src="/myProject/images/microsoftLook/..." >
122 * <li>images/microsoftLook</li>
123 * generate html for image like this < alt="Sort"
124 * src="images/microsoftLook/..." >
125 * </ul>
126 */
127 public void setImageHome(String dir)
128 {
129 imageHome = dir;
130 }
131
132 /***
133 * Get the HTML that comes before the column text.
134 *
135 * @return The HTML that comes before the column text.
136 */
137 public String getHeaderCellPreProcess(ColumnInfo columnInfo, ValueListInfo info)
138 {
139 StringBuffer sb = new StringBuffer();
140 sb.append("\n <th");
141 if (useNoWrap)
142 {
143 sb.append(" nowrap=\"true\"");
144 }
145 if (columnInfo != null)
146 {
147 if (columnInfo.getToolTip() != null)
148 {
149 sb.append(" title=\"").append(columnInfo.getToolTip()).append("\"");
150 }
151 if (columnInfo.getAttributes() != null)
152 {
153 sb.append(" ").append(columnInfo.getAttributes());
154 }
155 }
156 sb.append(">");
157 if (usePadding)
158 {
159 sb.append(" ");
160 }
161 return sb.toString();
162 }
163
164 /***
165 * Formats the text to be displayed as the header by wraping it in a link if
166 * sorting is enabled. Alt (hint) is localized, please define in your
167 * property file for messages the property "sorting"
168 *
169 * @param columnInfo The ColumnInfo.
170 * @param tableInfo The TableInfo.
171 * @param info The ValueListInfo.
172 * @return The formated HTML.
173 */
174 public String getHeaderLabel(ColumnInfo columnInfo, TableInfo tableInfo, ValueListInfo info, Map includeParameters)
175 {
176
177 ValueListConfigBean config = tableInfo.getConfig();
178 Map parameters = new HashMap(includeParameters);
179
180 if (columnInfo.getDefaultSort() != null)
181 {
182
183 String column = info.getSortingColumn();
184 Integer direction = info.getSortingDirection();
185
186 parameters.put(ValueListInfo.PAGING_NUMBER_PER + tableInfo.getId(), String.valueOf(info.getPagingNumberPer()));
187 parameters.put(ValueListInfo.PAGING_PAGE + tableInfo.getId(), "1");
188 parameters.put(ValueListInfo.SORT_COLUMN + tableInfo.getId(), columnInfo.getAdapterPropertyName());
189 parameters
190 .put(
191 ValueListInfo.SORT_DIRECTION + tableInfo.getId(),
192 ((columnInfo.getAdapterPropertyName().equals(column)) ? (ValueListInfo.ASCENDING.equals(direction) ? ValueListInfo.DESCENDING
193 : ValueListInfo.ASCENDING)
194 : columnInfo.getDefaultSort()));
195
196 if (info.isFocusEnabled())
197 {
198 parameters.put(ValueListInfo.DO_FOCUS + tableInfo.getId(), info.isDoFocusAgain() ? "true" : "false");
199 if (info.getFocusProperty() != null)
200 {
201 parameters.put(ValueListInfo.FOCUS_PROPERTY + tableInfo.getId(), info.getFocusProperty());
202 }
203 if (info.getFocusValue() != null)
204 {
205 parameters.put(ValueListInfo.FOCUS_VALUE + tableInfo.getId(), info.getFocusValue());
206 }
207 }
208
209 StringBuffer sb = new StringBuffer();
210
211 renderHeaderLabelLink(sb, columnInfo, tableInfo, info, parameters);
212
213 if (columnInfo.getAdapterPropertyName().equals(column))
214 {
215 if (usePadding)
216 {
217 sb.append(" ");
218 }
219 sb.append("<img src=\"").append(getImageHome((HttpServletRequest) tableInfo.getPageContext().getRequest())).append("/sort(");
220 sb.append((ValueListInfo.ASCENDING.equals(direction) ? ValueListInfo.DESCENDING : ValueListInfo.ASCENDING));
221 sb.append(").png\" border=\"0\"/>");
222 }
223 else if (columnInfo.getDefaultSort() != null)
224 {
225 Locale locale = config.getLocaleResolver().resolveLocale((HttpServletRequest) (tableInfo.getPageContext().getRequest()));
226 String altSort;
227 try
228 {
229 altSort = config.getDisplayHelper().help(tableInfo.getPageContext(),
230 config.getMessageSource().getMessage("sorting", null, "Sort", locale));
231 }
232 catch (JspException e)
233 {
234 LOGGER.error("getHeaderLabel() - Error getting property 'sorting' : " + e.getMessage()
235 + " Locale locale = " + locale
236 + ", String column = " + column + " using defalt hint for sorting images.");
237
238 altSort = "Sort";
239 }
240
241 sb.append(((usePadding) ? " " : "")).append("<img alt=\"").append(altSort).append("\" src=\"").append(
242 getImageHome((HttpServletRequest) tableInfo.getPageContext().getRequest())).append("/sort(null).png\" border=\"0\"/>");
243 }
244
245 return sb.toString();
246
247 }
248 else
249 {
250 return columnInfo.getTitle();
251 }
252 }
253
254 /***
255 * Renders a link as a header of the column if the sorting is enabled. Subclasses can overide or extend the method to provide different behaviour.
256 *
257 * @param sb StringBuffer to render into
258 * @param columnInfo The ColumnInfo.
259 * @param tableInfo The TableInfo.
260 * @param info The ValueListInfo.
261 * @param parameters Map of parameters
262 */
263 protected void renderHeaderLabelLink(StringBuffer sb, ColumnInfo columnInfo, TableInfo tableInfo, ValueListInfo info, Map parameters)
264 {
265
266
267
268
269 sb.append("<a href=\"").append(tableInfo.getUrl());
270
271 sb.append(tableInfo.getConfig().getLinkEncoder().encode(tableInfo.getPageContext(), parameters));
272
273 sb.append("\">").append(columnInfo.getTitle()).append("</a>");
274 }
275
276 /***
277 * Return imageHome, if is needed, return it with prefix "appDir"
278 *
279 * @param pageContext
280 * @param HttpServletRequest -if null, skip appending appDir-contextPath
281 * @return String that represent images home dir.
282 */
283 public String getImageHome(HttpServletRequest request)
284 {
285 if (preAppendContextPath && request != null)
286 {
287 if (LOGGER.isDebugEnabled())
288 {
289 LOGGER.debug("Preappending context path='" + request.getContextPath() + "' to image Home '" + imageHome + "'.");
290 }
291 return request.getContextPath() + "/" + imageHome;
292 }
293 return imageHome;
294 }
295
296 /***
297 * Get the HTML that comes after the column text.
298 *
299 * @return The HTML that comes after the column text.
300 */
301 public String getHeaderCellPostProcess()
302 {
303 return ((usePadding) ? " " : "") + "</th>";
304 }
305
306 public String getCellPreProcess(Attributes attributes)
307 {
308 return (attributes == null) ? "\n<td>" : "\n<td " + attributes.getCellAttributesAsString() + ">" + ((usePadding) ? " " : "");
309 }
310
311 public String getCellPostProcess()
312 {
313 return ((usePadding) ? " " : "") + "</td>";
314 }
315
316 /***
317 * @see java.lang.Object#toString()
318 */
319 public String toString()
320 {
321 return "Images home(without context path): " + getImageHome(null) + " - " + super.toString();
322 }
323
324 /***
325 * @param usePadding The usePadding to set.
326 */
327 public void setUsePadding(boolean usePadding)
328 {
329 this.usePadding = usePadding;
330 }
331
332 /***
333 * @param useNoWrap The useNoWrap to set.
334 */
335 public void setUseNoWrap(boolean useNoWrap)
336 {
337 this.useNoWrap = useNoWrap;
338 }
339
340 }