KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > web > tag > DefaultColumnTag


1 /**
2  *
3  * This library is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published
5  * by the Free Software Foundation; either version 2.1 of the License, or
6  * (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; with out even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  *
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;
21
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27
28 import net.mlw.vlh.ValueListInfo;
29 import net.mlw.vlh.web.ValueListConfigBean;
30 import net.mlw.vlh.web.tag.support.ColumnInfo;
31 import net.mlw.vlh.web.tag.support.DisplayProvider;
32 import net.mlw.vlh.web.util.JspUtils;
33
34 import org.apache.commons.beanutils.PropertyUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * This tag creates a column in a row, emphase wanted data
40  * and format column with specific locale. If is it not specified,
41  * use spring LocaleResolver to obtain default locale.
42  *
43  * @todo Document this tag.
44  * @author Matthew L. Wilson, Andrej Zachar
45  * @version $Revision: 1.23 $ $Date: 2006/01/06 10:53:54 $
46  */

47 //This class was refactored with using BaseColumnTag. Some methods and fields were pull up.
48
public class DefaultColumnTag extends BaseColumnTag
49 {
50
51    private static final long serialVersionUID = -1160414311192942253L;
52
53    /**
54     * Logger for this class
55     */

56    private static final Log LOGGER = LogFactory.getLog(DefaultColumnTag.class);
57
58    /** Supports column Grouping. **/
59    private String JavaDoc groupKey;
60
61    private String JavaDoc sum;
62
63    /** Holder of default sort direction, null is not sortable. */
64    private Integer JavaDoc defaultSort;
65
66    /** The javabean property name of this column used to get data from list returned by adapter. */
67    private String JavaDoc property;
68
69    /** The property name to use to retrieve do for this columns in an adapter. */
70    private String JavaDoc adapterProperty = null;
71
72    private String JavaDoc format;
73
74    private String JavaDoc defaultValue;
75
76    /** The pattern to be emphasis displayed in the ordinary column cell. */
77    private String JavaDoc emphasisPattern = null;
78
79    /** Locale used in column */
80    private Locale JavaDoc locale = null;
81
82    private List JavaDoc nestedColumnInfoList = null;
83
84    /**
85     * @return Returns the adapterProperty. if it is null, return ordinary property.
86     */

87    public String JavaDoc getAdapterProperty()
88    {
89       return adapterProperty == null ? property : adapterProperty;
90    }
91
92    /**
93     * @param sqlProperty The sql property name to use to retrieve do for this columns in adapter.
94     */

95    public void setAdapterProperty(String JavaDoc sqlProperty)
96    {
97       this.adapterProperty = sqlProperty;
98    }
99
100    /**
101     * Initialization is called at the beginning of <code>doStart</code>.
102     * Subclasses have to call either <code>super.init()</code> or <code>super.doStart()</code>.
103     * @throws JspException
104     */

105    protected void init() throws JspException JavaDoc
106    {
107       if (bodyContent != null)
108       {
109          bodyContent.clearBody();
110       }
111    }
112
113    /**
114     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
115     */

116    public int doStartTag() throws JspException JavaDoc
117    {
118       init();
119       return super.doStartTag();
120    }
121
122    /**
123     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
124     */

125    public int doEndTag() throws JspException JavaDoc
126    {
127
128       ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class);
129
130       DefaultRowTag rowTag = (DefaultRowTag) JspUtils.getParent(this, DefaultRowTag.class);
131
132       ValueListConfigBean config = rootTag.getConfig();
133
134       appendClassCellAttribute(rowTag.getRowStyleClass());
135
136       if (locale == null)
137       {
138          locale = config.getLocaleResolver().resolveLocale((HttpServletRequest JavaDoc) pageContext.getRequest());
139       }
140
141       if (rowTag.getCurrentRowNumber() == 0) // if this is the 1st row == the one with cell headers
142
{
143
144          String JavaDoc titleKey = getTitleKey();
145          String JavaDoc label = (titleKey == null) ? getTitle() : config.getMessageSource().getMessage(titleKey, null, titleKey, locale);
146
147          ColumnInfo columnInfo = new ColumnInfo(config.getDisplayHelper().help(pageContext, label), getAdapterProperty(), defaultSort,
148                getAttributes());
149
150          // Process toolTip if any
151
// toolTip or toolTipKey is set => get the string and put it into the ColumnInfo
152
String JavaDoc toolTipKey = getToolTipKey();
153          columnInfo.setToolTip((toolTipKey == null) ? getToolTip() : config.getMessageSource().getMessage(toolTipKey, null, toolTipKey,
154                locale));
155
156          columnInfo.setNestedList(this.nestedColumnInfoList);
157
158          rowTag.addColumnInfo(columnInfo);
159       }
160
161       DisplayProvider displayProvider = rowTag.getDisplayProvider();
162
163       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(displayProvider.getCellPreProcess(getCellAttributes()));
164
165       boolean hasBodyContent = false;
166       
167       if (displayProvider.doesIncludeBodyContent() && bodyContent != null && bodyContent.getString() != null
168             && bodyContent.getString().trim().length() > 0)
169       {
170          sb.append(bodyContent.getString().trim());
171          bodyContent.clearBody();
172          hasBodyContent = true;
173       }
174       
175       {
176          if (property != null && rowTag.getBeanName() != null)
177          {
178             try
179             {
180                Object JavaDoc bean = pageContext.getAttribute(rowTag.getBeanName());
181                if (bean != null)
182                {
183                   Object JavaDoc value = null;
184                   try
185                   {
186                      value = PropertyUtils.getProperty(bean, property);
187                   }
188                   catch (Exception JavaDoc e)
189                   {
190                      //Do nothing, if you want to handle this exception, then
191
// use a try catch in the body content.
192
LOGGER.error("<vlh:column> Error getting property='" + property + "' from the iterated JavaBean name='"
193                            + rowTag.getBeanName() + "'\n The row's JavaBean was >>>" + bean
194                            + "<<<\n Check the syntax or the spelling of the column's property!");
195                   }
196
197                   if (value != null)
198                   {
199                      if (sum != null && value instanceof Number JavaDoc)
200                      {
201                         double doubleValue = ((Number JavaDoc) value).doubleValue();
202                         Double JavaDoc sumValue = (Double JavaDoc) pageContext.getAttribute(sum);
203                         if (sumValue == null)
204                         {
205                            sumValue = new Double JavaDoc(doubleValue);
206                         }
207                         else
208                         {
209                            sumValue = new Double JavaDoc(sumValue.doubleValue() + doubleValue);
210                         }
211                         pageContext.setAttribute(sum, sumValue);
212                      }
213                      
214                      if( ! hasBodyContent)
215                      {
216                      String JavaDoc formattedValue = JspUtils.format(value, format, locale);
217
218                      if (groupKey == null
219                            || (config.getCellInterceptor() == null || !config.getCellInterceptor().isHidden(pageContext, groupKey,
220                                  property, formattedValue)))
221                      {
222                         sb.append(displayProvider.emphase(formattedValue, getEmphasisPattern(), getColumnStyleClass()));
223                      }
224                      }
225
226                   }
227                   else if (!hasBodyContent)
228                   {
229                      if (LOGGER.isDebugEnabled())
230                      {
231                         LOGGER.debug("The property '" + property + "' of the iterated JavaBean '" + bean + "' is null!");
232                      }
233
234                      Object JavaDoc nullValue = (defaultValue == null) ? config.getNullToken() : defaultValue;
235
236                      if (groupKey == null
237                            || (config.getCellInterceptor() == null || !config.getCellInterceptor().isHidden(pageContext, groupKey,
238                                  property, nullValue)))
239                      {
240                         sb.append(nullValue);
241                      }
242                   }
243                }
244             }
245             catch (Exception JavaDoc e)
246             {
247                final String JavaDoc message = "DefaultColumnTag.doEndTag() - <vlh:column> error getting property: " + property + " from bean.";
248                LOGGER.error(message, e);
249                throw new JspException JavaDoc(message, e);
250             }
251          }
252       }
253
254       sb.append(displayProvider.getCellPostProcess());
255       JspUtils.write(pageContext, sb.toString());
256
257       release();
258
259       return EVAL_PAGE;
260    }
261
262    public String JavaDoc getColumnStyleClass() throws JspException JavaDoc
263    {
264
265       ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class);
266
267       ValueListConfigBean config = rootTag.getConfig();
268
269       if (config == null)
270       {
271          throw new JspException JavaDoc("No config found on root tag");
272       }
273
274       return config.getStylePrefix();
275    }
276
277    /**
278     * Sets the defaultSort property.
279     *
280     * @param value
281     * Valid values are "asc" and "desc".
282     */

283    public void setSortable(String JavaDoc value)
284    {
285       if ("asc".equals(value))
286       {
287          defaultSort = ValueListInfo.ASCENDING;
288       }
289       else if ("desc".equals(value))
290       {
291          defaultSort = ValueListInfo.DESCENDING;
292       }
293    }
294
295    /**
296     * @return Returns the property.
297     */

298    public String JavaDoc getProperty()
299    {
300       return this.property;
301    }
302
303    /**
304     * Sets the javabean property name of this column
305     * used to get data from list returned by adapter.
306     * @param property
307     * The javabean property name of this column.
308     */

309    public void setProperty(String JavaDoc property)
310    {
311       this.property = property;
312    }
313
314    /**
315     * @return Returns the format.
316     */

317    public String JavaDoc getFormat()
318    {
319       return this.format;
320    }
321
322    /**
323     * @param format
324     * The format to set.
325     */

326    public void setFormat(String JavaDoc format)
327    {
328       this.format = format;
329    }
330
331    /**
332     * @return Returns the sum.
333     */

334    public String JavaDoc getSum()
335    {
336       return this.sum;
337    }
338
339    /**
340     * @param sum
341     * The sum to set.
342     */

343    public void setSum(String JavaDoc sum)
344    {
345       this.sum = sum;
346    }
347
348    /**
349     * @return Returns the defaultValue.
350     */

351    public String JavaDoc getDefault()
352    {
353       return this.defaultValue;
354    }
355
356    /**
357     * @param defaultValue The defaultValue to set.
358     */

359    public void setDefault(String JavaDoc defaultValue)
360    {
361       this.defaultValue = defaultValue;
362    }
363
364    /**
365     * @return Returns the emphasisPattern.
366     */

367    public String JavaDoc getEmphasisPattern()
368    {
369       return this.emphasisPattern;
370    }
371
372    /**
373     * It is used to emphasis part of text part of content of the displayed
374     * table cell. if <i>emphasisPattern</i> is empty("") sets it to null.
375     * @param emphasisPattern The emphasisPattern to set.
376     */

377    public void setEmphasisPattern(String JavaDoc emphasisPattern)
378    {
379       this.emphasisPattern = (emphasisPattern == null || emphasisPattern.length() == 0) ? null : emphasisPattern;
380    }
381
382    /**
383     * @param locale The locale to set for column.
384     */

385    public void setLocale(Locale JavaDoc locale)
386    {
387       this.locale = locale;
388    }
389
390    /**
391     * @return Returns the groupKey.
392     */

393    public String JavaDoc getGroupKey()
394    {
395       return this.groupKey;
396    }
397
398    /**
399     * @param groupKey The groupKey to set.
400     */

401    public void setGroupKey(String JavaDoc groupKey)
402    {
403       this.groupKey = groupKey;
404    }
405
406    /**
407     * @return Returns the nestedColumnInfoList.
408     */

409    public List JavaDoc getNestedColumnInfoList()
410    {
411       return nestedColumnInfoList;
412    }
413
414    /**
415     * @param nestedColumnInfoList The nestedColumnInfoList to set.
416     */

417    public void setNestedColumnInfoList(List JavaDoc nestedColumnInfoList)
418    {
419       this.nestedColumnInfoList = nestedColumnInfoList;
420    }
421
422    private void reset()
423    {
424       this.adapterProperty = null;
425       this.defaultSort = null;
426       this.defaultValue = null;
427       this.emphasisPattern = null;
428       this.format = null;
429       this.groupKey = null;
430       this.locale = null;
431       this.property = null;
432       this.sum = null;
433       this.nestedColumnInfoList = null;
434    }
435
436    /**
437     * Called on a Tag handler to release state.
438     * The page compiler guarantees that JSP page implementation
439     * objects will invoke this method on all tag handlers,
440     * but there may be multiple invocations on doStartTag and doEndTag in between.
441     *
442     * @see javax.servlet.jsp.tagext.Tag#release()
443     */

444    public void release()
445    {
446       super.release();
447       reset();
448    }
449 }
Popular Tags