1 30 31 package com.jgoodies.forms.layout; 32 33 import java.awt.Component ; 34 import java.awt.Container ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 import com.jgoodies.forms.util.DefaultUnitConverter; 39 import com.jgoodies.forms.util.UnitConverter; 40 41 42 55 public final class Sizes { 56 57 58 60 public static final ConstantSize ZERO = pixel(0); 61 62 public static final ConstantSize DLUX1 = dluX( 1); 63 public static final ConstantSize DLUX2 = dluX( 2); 64 public static final ConstantSize DLUX3 = dluX( 3); 65 public static final ConstantSize DLUX4 = dluX( 4); 66 public static final ConstantSize DLUX7 = dluX( 7); 67 public static final ConstantSize DLUX9 = dluX( 9); 68 public static final ConstantSize DLUX11 = dluX(11); 69 public static final ConstantSize DLUX14 = dluX(14); 70 71 public static final ConstantSize DLUY1 = dluY( 1); 72 public static final ConstantSize DLUY2 = dluY( 2); 73 public static final ConstantSize DLUY3 = dluY( 3); 74 public static final ConstantSize DLUY4 = dluY( 4); 75 public static final ConstantSize DLUY6 = dluY( 6); 76 public static final ConstantSize DLUY7 = dluY( 7); 77 public static final ConstantSize DLUY9 = dluY( 9); 78 public static final ConstantSize DLUY11 = dluY(11); 79 public static final ConstantSize DLUY14 = dluY(14); 80 81 82 84 87 public static final ComponentSize MINIMUM = new ComponentSize("minimum"); 88 89 92 public static final ComponentSize PREFERRED = new ComponentSize("preferred"); 93 94 99 public static final ComponentSize DEFAULT = new ComponentSize("default"); 100 101 102 104 107 private static UnitConverter unitConverter; 108 109 110 112 private Sizes() { 113 } 115 116 117 119 127 public static ConstantSize constant(String encodedValueAndUnit, 128 boolean horizontal) { 129 return ConstantSize.valueOf(encodedValueAndUnit, horizontal); 130 } 131 132 139 public static ConstantSize dluX(int value) { 140 return ConstantSize.dluX(value); 141 } 142 143 150 public static ConstantSize dluY(int value) { 151 return ConstantSize.dluY(value); 152 } 153 154 160 public static ConstantSize pixel(int value) { 161 return new ConstantSize(value, ConstantSize.PIXEL); 162 } 163 164 174 public static Size bounded(Size basis, Size lowerBound, Size upperBound) { 175 return new BoundedSize(basis, lowerBound, upperBound); 176 } 177 178 179 181 188 public static int inchAsPixel(double in, Component component) { 189 return getUnitConverter().inchAsPixel(in, component); 190 } 191 192 200 public static int millimeterAsPixel(double mm, Component component) { 201 return getUnitConverter().millimeterAsPixel(mm, component); 202 } 203 204 212 public static int centimeterAsPixel(double cm, Component component) { 213 return getUnitConverter().centimeterAsPixel(cm, component); 214 } 215 216 224 public static int pointAsPixel(int pt, Component component) { 225 return getUnitConverter().pointAsPixel(pt, component); 226 } 227 228 236 public static int dialogUnitXAsPixel(int dluX, Component component) { 237 return getUnitConverter().dialogUnitXAsPixel(dluX, component); 238 } 239 240 248 public static int dialogUnitYAsPixel(int dluY, Component component) { 249 return getUnitConverter().dialogUnitYAsPixel(dluY, component); 250 } 251 252 253 255 259 public static UnitConverter getUnitConverter() { 260 if (unitConverter == null) { 261 unitConverter = DefaultUnitConverter.getInstance(); 262 } 263 return unitConverter; 264 } 265 266 272 public static void setUnitConverter(UnitConverter newUnitConverter) { 273 unitConverter = newUnitConverter; 274 } 275 276 277 279 282 static final class ComponentSize implements Size { 283 284 private final String name; 285 286 private ComponentSize(String name) { 287 this.name = name; 288 } 289 290 296 static ComponentSize valueOf(String str) { 297 if (str.equals("m") || str.equals("min")) 298 return MINIMUM; 299 if (str.equals("p") || str.equals("pref")) 300 return PREFERRED; 301 if (str.equals("d") || str.equals("default")) 302 return DEFAULT; 303 else 304 return null; 305 } 306 307 315 public int maximumSize( 316 Container container, 317 List components, 318 FormLayout.Measure minMeasure, 319 FormLayout.Measure prefMeasure, 320 FormLayout.Measure defaultMeasure) { 321 322 FormLayout.Measure measure = this == MINIMUM 323 ? minMeasure 324 : (this == PREFERRED ? prefMeasure : defaultMeasure); 325 int maximum = 0; 326 for (Iterator i = components.iterator(); i.hasNext();) { 327 Component c = (Component ) i.next(); 328 maximum = Math.max(maximum, measure.sizeOf(c)); 329 } 330 return maximum; 331 } 332 333 public String toString() { return name.substring(0, 1); } 334 335 } 336 337 338 } | Popular Tags |