1 13 package com.tonbeller.wcf.table; 14 15 import org.w3c.dom.Element ; 16 17 import com.tonbeller.wcf.controller.RequestContext; 18 import com.tonbeller.wcf.format.FormatException; 19 import com.tonbeller.wcf.format.FormatHandler; 20 import com.tonbeller.wcf.format.Formatter; 21 import com.tonbeller.wcf.utils.DomUtils; 22 23 27 public class DefaultCellRenderer implements CellRenderer { 28 Alignment alignment = Alignment.GUESS; 29 String format; 30 String type; 31 private static final String NBSP = "\u00a0"; 32 33 public static class Alignment { 34 35 39 public static final Alignment GUESS = new Alignment(""); 40 41 44 public static final Alignment LEFT = new Alignment("left"); 45 46 49 public static final Alignment RIGHT = new Alignment("right"); 50 51 private String attributeValue; 52 53 private Alignment(String attributeValue) { 54 this.attributeValue = attributeValue; 55 } 56 57 public String getAttributeValue() { 58 return attributeValue; 59 } 60 } 61 62 public DefaultCellRenderer() { 63 } 64 65 public void render(RequestContext context, Element td, Object value) throws FormatException { 66 Element elem = td; 67 boolean needSpace = true; 68 69 if (value instanceof DefaultCell) { 70 DefaultCell hc = (DefaultCell)value; 71 value = hc.getValue(); 72 if (hc.getURL() != null) { 73 elem = DomUtils.appendElement(elem, "a"); 74 elem.setAttribute("href", hc.getURL()); 75 if (hc.getTarget() != null) 76 elem.setAttribute("target", hc.getTarget()); 77 if (hc.getOnClick() != null) 78 elem.setAttribute("onClick", hc.getOnClick()); 79 } 80 81 if (hc.getImage() != null) { 82 Element img = DomUtils.appendElement(elem, "img"); 83 img.setAttribute("border", "0"); 84 img.setAttribute("src", hc.getImage()); 85 DomUtils.appendText(elem, " "); 86 needSpace = false; 87 } 88 } 89 90 if (value != null) { 91 Formatter fmt = context.getFormatter(); 92 FormatHandler fh = null; 93 if (type != null) 94 fh = fmt.getHandler(type); 95 if (fh == null) 96 fh = fmt.guessHandler(value); 97 String s; 98 if (fh != null) 99 s = fh.format(value, format); 100 else if (value != null) 101 s = value.toString(); 102 else 103 s = null; 104 if (s == null || s.length() == 0 || s.trim().length()==0) 105 s = NBSP; 106 DomUtils.appendText(elem, s); 107 if(alignment == Alignment.GUESS) { 108 if (value instanceof Number ) 109 td.setAttribute("align", "right"); 110 else 111 td.setAttribute("align", "left"); 112 } 113 else 114 td.setAttribute("align", alignment.getAttributeValue()); 115 116 } 117 118 else if (needSpace) 119 DomUtils.setText(elem, NBSP); 120 } 121 122 126 public void setFormat(String newFormat) { 127 format = newFormat; 128 } 129 133 public String getFormat() { 134 return format; 135 } 136 140 public void setType(String newType) { 141 type = newType; 142 } 143 147 public String getType() { 148 return type; 149 } 150 151 154 public void setAlignment(Alignment alignment) { 155 this.alignment = alignment; 156 } 157 } | Popular Tags |