1 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 38 public class Column 39 { 40 41 44 private Row row; 45 46 49 private HeaderCell header; 50 51 54 private HtmlAttributeMap htmlAttributes; 55 56 59 private String stringValue; 60 61 64 private Cell cell; 65 66 72 public Column(HeaderCell headerCell, Cell currentCell, Row parentRow) 73 { 74 this.header = headerCell; 75 this.row = parentRow; 76 this.cell = currentCell; 77 78 this.htmlAttributes = headerCell.getHtmlAttributes(); 80 } 81 82 86 public HeaderCell getHeaderCell() 87 { 88 return this.header; 89 } 90 91 98 public Object getValue(boolean decorated) throws ObjectLookupException, DecoratorException 99 { 100 101 Object object = null; 102 103 if (this.cell.getStaticValue() != null) 105 { 106 object = this.cell.getStaticValue(); 107 } 108 else if (this.header.getBeanPropertyName() != null) 109 { 110 111 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 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)) { 141 if (!this.header.getShowNulls()) 142 { 143 object = TagConstants.EMPTY_STRING; 144 } 145 } 146 147 return object; 148 } 149 150 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 174 public void initialize() throws DecoratorException, ObjectLookupException 175 { 176 if (this.stringValue == null) 177 { 178 this.stringValue = createChoppedAndLinkedValue(); 179 } 180 } 181 182 186 public String getCloseTag() 187 { 188 this.stringValue = null; 189 return this.header.getCloseTag(); 190 } 191 192 198 public String createChoppedAndLinkedValue() throws ObjectLookupException, DecoratorException 199 { 200 201 String fullValue = ObjectUtils.toString(getValue(true)); 202 String choppedValue; 203 204 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 if (!ObjectUtils.equals(fullValue, choppedValue)) 222 { 223 this.htmlAttributes = (HtmlAttributeMap) this.htmlAttributes.clone(); 225 this.htmlAttributes.put(TagConstants.ATTRIBUTE_TITLE, HtmlTagUtil.stripHTMLTags(fullValue)); 227 } 228 229 if (this.header.getHref() != null) 230 { 231 Href colHref = getColumnHref(fullValue); 233 Anchor anchor = new Anchor(colHref, choppedValue); 234 choppedValue = anchor.toString(); 235 } 236 237 return choppedValue; 238 } 239 240 246 private Href getColumnHref(String columnContent) throws ObjectLookupException 247 { 248 Href colHref = (Href) this.header.getHref().clone(); 250 251 if (this.header.getParamName() != null) 253 { 254 255 Object paramValue; 256 257 if (this.header.getParamProperty() != null) 258 { 259 paramValue = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getParamProperty()); 261 262 } 263 else 264 { 265 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"))); } 277 catch (UnsupportedEncodingException e) 278 { 279 throw new UnhandledException(e); 280 } 281 } 282 } 283 return colHref; 284 } 285 286 291 public String getChoppedAndLinkedValue() 292 { 293 return this.stringValue; 294 } 295 296 299 public String toString() 300 { 301 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) .append("cell", this.cell) .append("header", this.header) .append("htmlAttributes", this.htmlAttributes) .append("stringValue", this.stringValue) .toString(); 307 } 308 } | Popular Tags |