1 30 31 package com.jgoodies.forms.layout; 32 33 import java.awt.Container ; 34 import java.util.List ; 35 import java.util.StringTokenizer ; 36 37 38 47 abstract class FormSpec { 48 49 50 52 55 static final DefaultAlignment LEFT_ALIGN = new DefaultAlignment("left"); 56 57 60 static final DefaultAlignment RIGHT_ALIGN = new DefaultAlignment("right"); 61 62 65 static final DefaultAlignment TOP_ALIGN = new DefaultAlignment("top"); 66 67 70 static final DefaultAlignment BOTTOM_ALIGN = new DefaultAlignment("bottom"); 71 72 75 static final DefaultAlignment CENTER_ALIGN = new DefaultAlignment("center"); 76 77 80 static final DefaultAlignment FILL_ALIGN = new DefaultAlignment("fill"); 81 82 83 85 88 public static final double NO_GROW = 0.0d; 89 90 93 public static final double DEFAULT_GROW = 1.0d; 94 95 96 98 102 private DefaultAlignment defaultAlignment; 103 104 107 private Size size; 108 109 112 private double resizeWeight; 113 114 115 117 128 protected FormSpec(DefaultAlignment defaultAlignment, 129 Size size, 130 double resizeWeight) { 131 this.defaultAlignment = defaultAlignment; 132 this.size = size; 133 this.resizeWeight = resizeWeight; 134 if (resizeWeight < 0) 135 throw new IllegalArgumentException ("The resize weight must be non-negative."); 136 } 137 138 145 protected FormSpec(DefaultAlignment defaultAlignment, String encodedDescription) { 146 this(defaultAlignment, Sizes.DEFAULT, NO_GROW); 147 parseAndInitValues(encodedDescription.toLowerCase()); 148 } 149 150 151 153 156 public final DefaultAlignment getDefaultAlignment() { 157 return defaultAlignment; 158 } 159 160 165 public void setDefaultAlignment(DefaultAlignment newDefaultAlignment) { 166 defaultAlignment = newDefaultAlignment; 167 } 168 169 174 public final Size getSize() { 175 return size; 176 } 177 178 183 public void setSize(Size size) { 184 this.size = size; 185 } 186 187 188 193 public final double getResizeWeight() { 194 return resizeWeight; 195 } 196 197 202 public void setResizeWeight(double weight) { 203 resizeWeight = weight; 204 } 205 206 207 214 final boolean canGrow() { 215 return getResizeWeight() != NO_GROW; 216 } 217 218 219 221 228 private void parseAndInitValues(String encodedDescription) { 229 StringTokenizer tokenizer = new StringTokenizer (encodedDescription, ":"); 230 if (!tokenizer.hasMoreTokens()) { 231 throw new IllegalArgumentException ( 232 "The form spec must not be empty."); 233 } 234 String token = tokenizer.nextToken(); 235 236 DefaultAlignment alignment = DefaultAlignment.valueOf(token, isHorizontal()); 238 if (alignment != null) { 239 setDefaultAlignment(alignment); 240 if (!tokenizer.hasMoreTokens()) { 241 throw new IllegalArgumentException ( 242 "The form spec must provide a size."); 243 } 244 token = tokenizer.nextToken(); 245 } 246 247 parseAndInitSize(token); 248 249 if (tokenizer.hasMoreTokens()) { 250 setResizeWeight(decodeResize(tokenizer.nextToken())); 251 } 252 } 253 254 255 260 private void parseAndInitSize(String token) { 261 if (token.startsWith("max(") && token.endsWith(")")) { 262 setSize(parseAndInitBoundedSize(token, false)); 263 return; 264 } 265 if (token.startsWith("min(") && token.endsWith(")")) { 266 setSize(parseAndInitBoundedSize(token, true)); 267 return; 268 } 269 setSize(decodeAtomicSize(token)); 270 } 271 272 273 282 private Size parseAndInitBoundedSize(String token, boolean setMax) { 283 int semicolonIndex = token.indexOf(';'); 284 String sizeToken1 = token.substring(4, semicolonIndex); 285 String sizeToken2 = token.substring(semicolonIndex+1, token.length()-1); 286 287 Size size1 = decodeAtomicSize(sizeToken1); 288 Size size2 = decodeAtomicSize(sizeToken2); 289 290 if (size1 instanceof ConstantSize) { 292 if (size2 instanceof Sizes.ComponentSize) { 293 return new BoundedSize(size2, setMax ? null : size1, 294 setMax ? size1 : null); 295 } else 296 throw new IllegalArgumentException ( 297 "Bounded sizes must not be both constants."); 298 } else { 299 if (size2 instanceof ConstantSize) { 300 return new BoundedSize(size1, setMax ? null : size2, 301 setMax ? size2 : null); 302 } else 303 throw new IllegalArgumentException ( 304 "Bounded sizes must not be both logical."); 305 } 306 } 307 308 309 315 private Size decodeAtomicSize(String token) { 316 Sizes.ComponentSize componentSize = Sizes.ComponentSize.valueOf(token); 317 if (componentSize != null) 318 return componentSize; 319 else 320 return ConstantSize.valueOf(token, isHorizontal()); 321 } 322 323 324 332 private double decodeResize(String token) { 333 if (token.equals("g") || token.equals("grow")) { 334 return DEFAULT_GROW; 335 } 336 if (token.equals("n") || token.equals("nogrow") || token.equals("none")) { 337 return NO_GROW; 338 } 339 if ((token.startsWith("grow(") || token.startsWith("g(")) 341 && token.endsWith(")")) { 342 int leftParen = token.indexOf('('); 343 int rightParen = token.indexOf(')'); 344 String substring = token.substring(leftParen + 1, rightParen); 345 return Double.parseDouble(substring); 346 } else { 347 throw new IllegalArgumentException ( 348 "The resize argument '" + token + "' is invalid. " + 349 " Must be one of: grow, g, none, n, grow(<double>), g(<double>)"); 350 } 351 } 352 353 354 356 364 public final String toString() { 365 StringBuffer buffer = new StringBuffer (); 366 buffer.append(defaultAlignment); 367 368 buffer.append(":"); 369 buffer.append(size.toString()); 370 buffer.append(':'); 371 if (resizeWeight == NO_GROW) { 372 buffer.append("noGrow"); 373 } else if (resizeWeight == DEFAULT_GROW) { 374 buffer.append("grow"); 375 } else { 376 buffer.append("grow("); 377 buffer.append(resizeWeight); 378 buffer.append(')'); 379 } 380 return buffer.toString(); 381 } 382 383 391 public final String toShortString() { 392 StringBuffer buffer = new StringBuffer (); 393 buffer.append(defaultAlignment.abbreviation()); 394 395 buffer.append(":"); 396 buffer.append(size.toString()); 397 buffer.append(':'); 398 if (resizeWeight == NO_GROW) { 399 buffer.append("n"); 400 } else if (resizeWeight == DEFAULT_GROW) { 401 buffer.append("g"); 402 } else { 403 buffer.append("g("); 404 buffer.append(resizeWeight); 405 buffer.append(')'); 406 } 407 return buffer.toString(); 408 } 409 410 411 413 419 abstract boolean isHorizontal(); 420 421 422 424 430 final int maximumSize(Container container, 431 List components, 432 FormLayout.Measure minMeasure, 433 FormLayout.Measure prefMeasure, 434 FormLayout.Measure defaultMeasure) { 435 return size.maximumSize(container, 436 components, 437 minMeasure, 438 prefMeasure, 439 defaultMeasure); 440 } 441 442 443 446 public static final class DefaultAlignment { 447 448 private final String name; 449 450 private DefaultAlignment(String name) { 451 this.name = name; 452 } 453 454 461 static DefaultAlignment valueOf(String str, boolean isHorizontal) { 462 if (str.equals("f") || str.equals("fill")) 463 return FILL_ALIGN; 464 else if (str.equals("c") || str.equals("center")) 465 return CENTER_ALIGN; 466 else if (isHorizontal) { 467 if (str.equals("r") || str.equals("right")) 468 return RIGHT_ALIGN; 469 else if (str.equals("l") || str.equals("left")) 470 return LEFT_ALIGN; 471 else 472 return null; 473 } else { 474 if (str.equals("t") || str.equals("top")) 475 return TOP_ALIGN; 476 else if (str.equals("b") || str.equals("bottom")) 477 return BOTTOM_ALIGN; 478 else 479 return null; 480 } 481 } 482 483 public String toString() { return name; } 484 485 public char abbreviation() { return name.charAt(0); } 486 487 } 488 489 490 } 491 492 | Popular Tags |