1 30 31 package com.jgoodies.forms.layout; 32 33 import java.awt.Component ; 34 import java.awt.Container ; 35 import java.util.List ; 36 37 65 66 public final class ConstantSize implements Size { 67 68 70 public static final Unit PIXEL = new Unit("Pixel", "px", true); 71 public static final Unit POINT = new Unit("Point", "pt", true); 72 public static final Unit DIALOG_UNITS_X = new Unit("Dialog units X", "dluX", true); 73 public static final Unit DLUX = DIALOG_UNITS_X; 74 public static final Unit DIALOG_UNITS_Y = new Unit("Dialog units Y", "dluY", true); 75 public static final Unit DLUY = DIALOG_UNITS_Y; 76 public static final Unit MILLIMETER = new Unit("Millimeter", "mm", false); 77 public static final Unit MM = MILLIMETER; 78 public static final Unit CENTIMETER = new Unit("Centimeter", "cm", false); 79 public static final Unit CM = CENTIMETER; 80 public static final Unit INCH = new Unit("Inch", "in", false); 81 public static final Unit IN = INCH; 82 83 84 86 private final double value; 87 private final Unit unit; 88 89 90 92 99 ConstantSize(int value, Unit unit) { 100 this.value = value; 101 this.unit = unit; 102 } 103 104 111 ConstantSize(double value, Unit unit) { 112 this.value = value; 113 this.unit = unit; 114 } 115 116 125 static ConstantSize valueOf(String encodedValueAndUnit, boolean horizontal) { 126 String split[] = ConstantSize.splitValueAndUnit(encodedValueAndUnit); 127 String encodedValue = split[0]; 128 String encodedUnit = split[1]; 129 Unit unit = Unit.valueOf(encodedUnit, horizontal); 130 double value = Double.parseDouble(encodedValue); 131 if (unit.requiresIntegers) { 132 if (value != (int) value) 133 throw new IllegalArgumentException (unit.toString() 134 + " value " + encodedValue + " must be an integer."); 135 } 136 return new ConstantSize(value, unit); 137 } 138 139 146 static ConstantSize dluX(int value) { 147 return new ConstantSize(value, DLUX); 148 } 149 150 157 static ConstantSize dluY(int value) { 158 return new ConstantSize(value, DLUY); 159 } 160 161 162 164 170 public int getPixelSize(Component component) { 171 if (unit == PIXEL) 172 return intValue(); 173 else if (unit == POINT) 174 return Sizes.pointAsPixel(intValue(), component); 175 else if (unit == INCH) 176 return Sizes.inchAsPixel(value, component); 177 else if (unit == MILLIMETER) 178 return Sizes.millimeterAsPixel(value, component); 179 else if (unit == CENTIMETER) 180 return Sizes.centimeterAsPixel(value, component); 181 else if (unit == DIALOG_UNITS_X) 182 return Sizes.dialogUnitXAsPixel(intValue(), component); 183 else if (unit == DIALOG_UNITS_Y) 184 return Sizes.dialogUnitYAsPixel(intValue(), component); 185 else 186 throw new IllegalStateException ("Invalid unit " + unit); 187 } 188 189 190 192 205 public int maximumSize(Container container, 206 List components, 207 FormLayout.Measure minMeasure, 208 FormLayout.Measure prefMeasure, 209 FormLayout.Measure defaultMeasure) { 210 return getPixelSize(container); 211 } 212 213 215 225 public boolean equals(Object o) { 226 if (!(o instanceof ConstantSize)) 227 return false; 228 ConstantSize size = (ConstantSize) o; 229 return this.value == size.value 230 && this.unit == size.unit; 231 } 232 233 242 public int hashCode() { 243 return new Double (value).hashCode() + 37 * unit.hashCode(); 244 } 245 246 251 public String toString() { 252 return (value == intValue()) 253 ? Integer.toString(intValue()) + unit.abbreviation() 254 : Double.toString(value) + unit.abbreviation(); 255 } 256 257 258 260 private int intValue() { 261 return (int) Math.round(value); 262 } 263 264 271 static String [] splitValueAndUnit(String encodedValueAndUnit) { 272 String [] result = new String [2]; 273 int len = encodedValueAndUnit.length(); 274 int firstLetterIndex = len; 275 while (firstLetterIndex > 0 276 && Character.isLetter(encodedValueAndUnit.charAt(firstLetterIndex-1))) { 277 firstLetterIndex--; 278 } 279 result[0] = encodedValueAndUnit.substring(0, firstLetterIndex); 280 result[1] = encodedValueAndUnit.substring(firstLetterIndex); 281 return result; 282 } 283 284 286 290 public static final class Unit { 291 292 private final String name; 293 private final String abbreviation; 294 final boolean requiresIntegers; 295 296 private Unit(String name, String abbreviation, boolean requiresIntegers) { 297 this.name = name; 298 this.abbreviation = abbreviation; 299 this.requiresIntegers = requiresIntegers; 300 } 301 302 310 static Unit valueOf(String str, boolean horizontal) { 311 String lowerCase = str.toLowerCase(); 312 if (lowerCase.equals("px") || lowerCase.length() == 0) 313 return PIXEL; 314 else if (lowerCase.equals("dlu")) 315 return horizontal ? DIALOG_UNITS_X : DIALOG_UNITS_Y; 316 else if (lowerCase.equals("pt")) 317 return POINT; 318 else if (lowerCase.equals("in")) 319 return INCH; 320 else if (lowerCase.equals("mm")) 321 return MILLIMETER; 322 else if (lowerCase.equals("cm")) 323 return CENTIMETER; 324 else 325 throw new IllegalArgumentException ( 326 "Invalid unit name '" + str + "'. Must be one of: " + 327 "px, dlu, pt, mm, cm, in"); 328 } 329 330 public String toString() { 331 return name; 332 } 333 334 public String abbreviation() { 335 return abbreviation; 336 } 337 338 } 339 340 341 } | Popular Tags |