1
2
3
4
5
6
7
8
9
10
11
12 package org.displaytag.model;
13
14 import java.io.UnsupportedEncodingException;
15 import java.net.URLEncoder;
16
17 import org.apache.commons.lang.ObjectUtils;
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.lang.UnhandledException;
20 import org.apache.commons.lang.builder.ToStringBuilder;
21 import org.apache.commons.lang.builder.ToStringStyle;
22 import org.displaytag.decorator.DisplaytagColumnDecorator;
23 import org.displaytag.exception.DecoratorException;
24 import org.displaytag.exception.ObjectLookupException;
25 import org.displaytag.util.Anchor;
26 import org.displaytag.util.Href;
27 import org.displaytag.util.HtmlAttributeMap;
28 import org.displaytag.util.HtmlTagUtil;
29 import org.displaytag.util.LookupUtil;
30 import org.displaytag.util.TagConstants;
31
32
33
34
35
36
37
38 public class Column
39 {
40
41
42
43
44 private Row row;
45
46
47
48
49 private HeaderCell header;
50
51
52
53
54 private HtmlAttributeMap htmlAttributes;
55
56
57
58
59 private String stringValue;
60
61
62
63
64 private Cell cell;
65
66
67
68
69
70
71
72 public Column(HeaderCell headerCell, Cell currentCell, Row parentRow)
73 {
74 this.header = headerCell;
75 this.row = parentRow;
76 this.cell = currentCell;
77
78
79 this.htmlAttributes = headerCell.getHtmlAttributes();
80 }
81
82
83
84
85
86 public HeaderCell getHeaderCell()
87 {
88 return this.header;
89 }
90
91
92
93
94
95
96
97
98 public Object getValue(boolean decorated) throws ObjectLookupException, DecoratorException
99 {
100
101 Object object = null;
102
103
104 if (this.cell.getStaticValue() != null)
105 {
106 object = this.cell.getStaticValue();
107 }
108 else if (this.header.getBeanPropertyName() != null)
109 {
110
111
112
113 if (decorated
114 && this.row.getParentTable().getTableDecorator() != null
115 && this.row.getParentTable().getTableDecorator().hasGetterFor(this.header.getBeanPropertyName()))
116 {
117
118 object = LookupUtil.getBeanProperty(this.row.getParentTable().getTableDecorator(), this.header
119 .getBeanPropertyName());
120 }
121 else
122 {
123
124 object = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getBeanPropertyName());
125 }
126 }
127
128 DisplaytagColumnDecorator[] decorators = this.header.getColumnDecorators();
129 if (decorated)
130 {
131 for (int j = 0; j < decorators.length; j++)
132 {
133 object = decorators[j].decorate(object, row.getParentTable().getPageContext(), row
134 .getParentTable()
135 .getMedia());
136 }
137 }
138
139 if (object == null || "null".equals(object))
140 {
141 if (!this.header.getShowNulls())
142 {
143 object = TagConstants.EMPTY_STRING;
144 }
145 }
146
147 return object;
148 }
149
150
151
152
153
154 public String getOpenTag()
155 {
156 HtmlAttributeMap rowAttributes = cell.getPerRowAttributes();
157
158 HtmlAttributeMap atts = htmlAttributes;
159 if (rowAttributes != null)
160 {
161 atts = (HtmlAttributeMap) atts.clone();
162 atts.putAll(rowAttributes);
163 }
164 return HtmlTagUtil.createOpenTagString(TagConstants.TAGNAME_COLUMN, atts);
165 }
166
167
168
169
170
171
172
173
174 public void initialize() throws DecoratorException, ObjectLookupException
175 {
176 if (this.stringValue == null)
177 {
178 this.stringValue = createChoppedAndLinkedValue();
179 }
180 }
181
182
183
184
185
186 public String getCloseTag()
187 {
188 this.stringValue = null;
189 return this.header.getCloseTag();
190 }
191
192
193
194
195
196
197
198 public String createChoppedAndLinkedValue() throws ObjectLookupException, DecoratorException
199 {
200
201 String fullValue = ObjectUtils.toString(getValue(true));
202 String choppedValue;
203
204
205 if (this.header.getMaxLength() > 0)
206 {
207 choppedValue = HtmlTagUtil.abbreviateHtmlString(fullValue, this.header.getMaxLength(), false);
208 }
209 else if (this.header.getMaxWords() > 0)
210 {
211 choppedValue = HtmlTagUtil.abbreviateHtmlString(fullValue, this.header.getMaxWords(), true);
212 }
213 else
214 {
215 choppedValue = fullValue;
216 }
217
218
219
220
221 if (!ObjectUtils.equals(fullValue, choppedValue))
222 {
223
224 this.htmlAttributes = (HtmlAttributeMap) this.htmlAttributes.clone();
225
226 this.htmlAttributes.put(TagConstants.ATTRIBUTE_TITLE, HtmlTagUtil.stripHTMLTags(fullValue));
227 }
228
229 if (this.header.getHref() != null)
230 {
231
232 Href colHref = getColumnHref(fullValue);
233 Anchor anchor = new Anchor(colHref, choppedValue);
234 choppedValue = anchor.toString();
235 }
236
237 return choppedValue;
238 }
239
240
241
242
243
244
245
246 private Href getColumnHref(String columnContent) throws ObjectLookupException
247 {
248
249 Href colHref = (Href) this.header.getHref().clone();
250
251
252 if (this.header.getParamName() != null)
253 {
254
255 Object paramValue;
256
257 if (this.header.getParamProperty() != null)
258 {
259
260 paramValue = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getParamProperty());
261
262 }
263 else
264 {
265
266 paramValue = columnContent;
267 }
268
269 if (paramValue != null)
270 {
271 try
272 {
273 colHref.addParameter(this.header.getParamName(), URLEncoder.encode(
274 paramValue.toString(),
275 StringUtils.defaultString(this.row.getParentTable().getEncoding(), "UTF8")));
276 }
277 catch (UnsupportedEncodingException e)
278 {
279 throw new UnhandledException(e);
280 }
281 }
282 }
283 return colHref;
284 }
285
286
287
288
289
290
291 public String getChoppedAndLinkedValue()
292 {
293 return this.stringValue;
294 }
295
296
297
298
299 public String toString()
300 {
301 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
302 .append("cell", this.cell)
303 .append("header", this.header)
304 .append("htmlAttributes", this.htmlAttributes)
305 .append("stringValue", this.stringValue)
306 .toString();
307 }
308 }