1 29 30 package nextapp.echo2.webcontainer.propertyrender; 31 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import nextapp.echo2.app.Extent; 37 import nextapp.echo2.webrender.output.CssStyle; 38 39 43 public class ExtentRender { 44 45 private static final Map UNIT_NAMES_TO_VALUES; 46 static { 47 Map m = new HashMap (); 48 m.put("cm", new Integer (Extent.CM)); 49 m.put("em", new Integer (Extent.EM)); 50 m.put("ex", new Integer (Extent.EX)); 51 m.put("in", new Integer (Extent.IN)); 52 m.put("mm", new Integer (Extent.MM)); 53 m.put("pc", new Integer (Extent.PC)); 54 m.put("%", new Integer (Extent.PERCENT)); 55 m.put("pt", new Integer (Extent.PT)); 56 m.put("px", new Integer (Extent.PX)); 57 UNIT_NAMES_TO_VALUES = Collections.unmodifiableMap(m); 58 } 59 60 68 public static boolean isZeroLength(Extent extent) { 69 return extent == null || extent.getValue() == 0; 70 } 71 72 80 public static final String renderCssAttributePixelValue(Extent extent) { 81 return renderCssAttributePixelValue(extent, null); 82 } 83 84 94 public static final String renderCssAttributePixelValue(Extent extent, String invalidValue) { 95 if (extent != null && extent.getUnits() == Extent.PX) { 96 return extent.getValue() + "px"; 97 } else { 98 return invalidValue; 99 } 100 } 101 102 109 public static final String renderCssAttributeValue(Extent extent) { 110 return extent.getValue() + renderUnits(extent.getUnits()); 111 } 112 113 122 public static final String renderCssAttributeValueHalf(Extent extent) { 123 if (extent.getValue() % 2 == 0) { 124 return (extent.getValue() / 2) + renderUnits(extent.getUnits()); 125 } else { 126 return (extent.getValue() / 2) + ".5" + renderUnits(extent.getUnits()); 127 } 128 } 129 130 138 public static final void renderToStyle(CssStyle cssStyle, String cssAttribute, Extent extent) { 139 if (extent == null) { 140 return; 141 } 142 cssStyle.setAttribute(cssAttribute, renderCssAttributeValue(extent)); 143 } 144 145 152 public static final String renderUnits(int units) { 153 switch (units) { 154 case Extent.CM: return "cm"; 155 case Extent.EM: return "em"; 156 case Extent.EX: return "ex"; 157 case Extent.IN: return "in"; 158 case Extent.MM: return "mm"; 159 case Extent.PC: return "pc"; 160 case Extent.PERCENT: return "%"; 161 case Extent.PT: return "pt"; 162 case Extent.PX: return "px"; 163 default: 164 throw new IllegalArgumentException ("Invalid extent."); 165 } 166 } 167 168 private static int getUnitPosition(String s) { 169 int length = s.length(); 170 for (int i = 0; i < length; ++i) { 171 char ch = s.charAt(i); 172 if (ch != '-' && (ch < '0' || ch > '9')) { 173 return i; 174 } 175 } 176 return -1; 177 } 178 179 187 public static final Extent toExtent(String extentString) { 188 try { 189 if (extentString == null) { 190 return null; 191 } 192 int unitPosition = getUnitPosition(extentString); 193 if (unitPosition == -1) { 194 return null; 195 } 196 String unitString = extentString.substring(unitPosition); 197 Integer unitInteger = (Integer ) UNIT_NAMES_TO_VALUES.get(unitString); 198 if (unitInteger == null) { 199 return null; 200 } 201 Extent extent = new Extent(Integer.parseInt(extentString.substring(0, unitPosition)), unitInteger.intValue()); 202 return extent; 203 } catch (NumberFormatException ex) { 204 return null; 205 } 206 } 207 208 217 public static final int toPixels(Extent extent, int defaultPixels) { 218 if (extent != null && extent.getUnits() == Extent.PX) { 219 return extent.getValue(); 220 } else { 221 return defaultPixels; 222 } 223 } 224 225 226 private ExtentRender() { } 227 } 228 | Popular Tags |