1 22 package com.izforge.izpack.installer; 23 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.Insets ; 27 import java.awt.LayoutManager2 ; 28 29 import javax.swing.JComponent ; 30 31 import com.izforge.izpack.gui.IzPanelConstraints; 32 import com.izforge.izpack.gui.IzPanelLayout; 33 import com.izforge.izpack.gui.LayoutConstants; 34 import com.izforge.izpack.installer.IzPanel.Filler; 35 36 48 public class LayoutHelper implements LayoutConstants 49 { 50 51 JComponent parent; 52 53 54 protected boolean layoutStarted = false; 55 56 57 protected Object defaultConstraints; 58 59 60 protected int gridxCounter = -1; 61 62 63 protected int gridyCounter = -1; 64 65 66 protected LayoutManager2 izPanelLayout; 67 68 71 protected static int ANCHOR = -1; 72 73 protected static int X_STRETCH_TYPE = -1; 74 75 protected static int Y_STRETCH_TYPE = -1; 76 77 protected static double FULL_LINE_STRETCH_DEFAULT = -1.0; 78 79 protected static double FULL_COLUMN_STRETCH_DEFAULT = -1.0; 80 81 protected static Double INITIAL_STRETCH_DEFAULT = new Double (1.0); 82 83 protected static Double DOUBLE_ZERO = new Double (0.0); 84 85 89 public final static String [] X_GAP_NAME_LOOK_UP = { "INTERNAL_USED", "labelXGap", "textXGab", 90 "controlXGap", "paragraphXGap", "labelToTextXGap", "labelToControlXGap", 91 "textToLabelXGap", "controlToLabelXGap", "controlToTextXGap", "textToControlXGap", 92 "firstXGap", "INTERNAL_USED", "INTERNAL_USED", "filler1XGap", "filler2XGap", 93 "filler3XGap", "filler4XGap", "filler5XGap"}; 94 95 99 public final static String [] Y_GAP_NAME_LOOK_UP = { "INTERNAL_USED", "labelYGap", "textYGab", 100 "controlYGap", "paragraphYGap", "labelToTextYGap", "labelToControlYGap", 101 "textToLabelYGap", "controlToLabelYGap", "controlToTextYGap", "textToControlYGap", 102 "firstYGap", "INTERNAL_USED", "INTERNAL_USED", "filler1YGap", "filler2YGap", 103 "filler3YGap", "filler4YGap", "filler5YGap"}; 104 105 106 public final static String ALL_X_GAP = "allXGap"; 107 108 109 public final static String ALL_Y_GAP = "allYGap"; 110 111 116 public LayoutHelper(JComponent parent) 117 { 118 this(); 119 this.parent = parent; 120 izPanelLayout = new GridBagLayout (); 121 parent.setLayout(izPanelLayout); 122 gridyCounter++; 123 } 124 125 128 protected LayoutHelper() 129 { 130 super(); 131 } 132 133 138 private boolean isGridBag() 139 { 140 return (izPanelLayout instanceof GridBagLayout ); 141 } 142 143 148 private boolean isIzPanel() 149 { 150 return (izPanelLayout instanceof IzPanelLayout); 151 } 152 153 155 163 public void startLayout(LayoutManager2 layout) 164 { 165 if (layoutStarted) return; 166 izPanelLayout = layout; 167 if (isGridBag()) 168 { 169 startGridBagLayout(); 170 return; 171 } 172 if (isIzPanel()) startIzPanelLayout(); 174 } 175 176 179 private void startIzPanelLayout() 180 { 181 IzPanelLayout.setAnchor(getAnchor()); 182 IzPanelLayout.setXStretchType(getXStretchType()); 183 IzPanelLayout.setYStretchType(getYStretchType()); 184 IzPanelLayout.setFullLineStretch(getFullLineStretch()); 185 IzPanelLayout.setFullColumnStretch(getFullColumnStretch()); 186 getXGap(LABEL_GAP); getYGap(LABEL_GAP); parent.setLayout(izPanelLayout); 189 } 191 192 198 public void completeLayout() 199 { 200 if (isGridBag()) 201 { 202 completeGridBagLayout(); 203 return; 204 } 205 } 207 208 213 public Object getDefaultConstraints() 214 { 215 startLayout(izPanelLayout); 216 return defaultConstraints; 217 } 218 219 224 public void setDefaultConstraints(Object constraints) 225 { 226 227 startLayout(izPanelLayout); 228 if ((isGridBag() && !(constraints instanceof GridBagConstraints )) 229 || (isIzPanel() && !(constraints instanceof IzPanelConstraints))) 230 throw new IllegalArgumentException ( 231 "Layout and constraints have to be from the same type."); 232 defaultConstraints = constraints; 233 } 234 235 238 public void resetGridCounter() 239 { 240 gridxCounter = -1; 241 gridyCounter = -1; 242 } 243 244 253 public Object getNewConstraints(int gridx, int gridy) 254 { 255 if (isGridBag()) 256 { 257 GridBagConstraints retval = (GridBagConstraints ) ((GridBagConstraints ) getDefaultConstraints()) 258 .clone(); 259 retval.gridx = gridx; 260 retval.gridy = gridy; 261 return (retval); 262 } 263 if (isIzPanel()) 264 { 265 IzPanelConstraints retval = (IzPanelConstraints) ((IzPanelConstraints) getDefaultConstraints()) 266 .clone(); 267 retval.setXPos(gridx); 268 retval.setYPos(gridy); 269 return (retval); 270 } 271 return (null); 272 } 273 274 285 public Object getNewConstraints(int gridx, int gridy, int gridwidth, int gridheight) 286 { 287 Object retval = getNewConstraints(gridx, gridy); 288 if (isGridBag()) 289 { 290 GridBagConstraints gbc = (GridBagConstraints ) retval; 291 gbc.gridwidth = gridwidth; 292 gbc.gridheight = gridheight; 293 } 294 if (isIzPanel()) 295 { 296 IzPanelConstraints gbc = (IzPanelConstraints) retval; 297 gbc.setXWeight(gridwidth); 298 gbc.setYWeight(gridheight); 299 } 300 return (retval); 301 } 302 303 309 public Object getNextXConstraints() 310 { 311 gridxCounter++; 312 return (getNewConstraints(gridxCounter, gridyCounter)); 313 } 314 315 321 public Object getNextYConstraints() 322 { 323 gridyCounter++; 324 gridxCounter = 0; 325 return (getNewConstraints(0, gridyCounter)); 326 } 327 328 336 public Object getNextYConstraints(int gridwidth, int gridheight) 337 { 338 gridyCounter++; 339 gridxCounter = 0; 340 return (getNewConstraints(0, gridyCounter, gridwidth, gridheight)); 341 } 342 343 345 352 private void startGridBagLayout() 353 { 354 if (layoutStarted) return; 355 layoutStarted = true; 356 if (izPanelLayout == null || !(izPanelLayout instanceof GridBagLayout )) 357 izPanelLayout = new GridBagLayout (); 358 GridBagConstraints dgbc = new GridBagConstraints (); 359 dgbc.insets = new Insets (0, 0, getYGap(LABEL_GAP), 0); 360 dgbc.anchor = GridBagConstraints.WEST; 361 defaultConstraints = dgbc; 362 parent.setLayout(izPanelLayout); 363 switch (getAnchor()) 364 { 365 case SOUTH: 366 case SOUTH_WEST: 367 Filler dummy = new Filler(); 369 GridBagConstraints gbConstraint = (GridBagConstraints ) getNextYConstraints(); 370 gbConstraint.weighty = 1.0; 371 gbConstraint.fill = GridBagConstraints.BOTH; 372 gbConstraint.anchor = GridBagConstraints.WEST; 373 parent.add(dummy, gbConstraint); 374 break; 375 default: 376 break; 377 } 378 } 380 381 387 private void completeGridBagLayout() 388 { 389 switch (getAnchor()) 390 { 391 case NORTH: 392 case NORTH_WEST: 393 Filler dummy = new Filler(); 395 GridBagConstraints gbConstraint = (GridBagConstraints ) getNextYConstraints(); 396 gbConstraint.weighty = 1.0; 397 gbConstraint.fill = GridBagConstraints.BOTH; 398 gbConstraint.anchor = GridBagConstraints.WEST; 399 parent.add(dummy, gbConstraint); 400 break; 401 default: 402 break; 403 } 404 } 405 406 414 public static int getAnchor() 415 { 416 if (ANCHOR >= 0) return (ANCHOR); 417 AutomatedInstallData idata = AutomatedInstallData.getInstance(); 418 String todo; 419 if (idata instanceof InstallData 420 && ((InstallData) idata).guiPrefs.modifier.containsKey("layoutAnchor")) 421 todo = (String ) ((InstallData) idata).guiPrefs.modifier.get("layoutAnchor"); 422 else 423 todo = idata.getVariable("IzPanel.LayoutType"); 424 if (todo == null) ANCHOR = CENTER; 426 else if ("EAST".equalsIgnoreCase(todo)) 427 ANCHOR = EAST; 428 else if ("WEST".equalsIgnoreCase(todo)) 429 ANCHOR = WEST; 430 else if ("TOP".equalsIgnoreCase(todo) || "NORTH".equalsIgnoreCase(todo)) 431 ANCHOR = NORTH; 432 else if ("BOTTOM".equalsIgnoreCase(todo) || "SOUTH".equalsIgnoreCase(todo)) 433 ANCHOR = SOUTH; 434 else if ("SOUTHWEST".equalsIgnoreCase(todo) || "SOUTH_WEST".equalsIgnoreCase(todo)) 435 ANCHOR = SOUTH_WEST; 436 else if ("SOUTHEAST".equalsIgnoreCase(todo) || "SOUTH_EAST".equalsIgnoreCase(todo)) 437 ANCHOR = SOUTH_EAST; 438 else if ("NORTHWEST".equalsIgnoreCase(todo) || "NORTH_WEST".equalsIgnoreCase(todo)) 439 ANCHOR = NORTH_WEST; 440 else if ("NORTHEAST".equalsIgnoreCase(todo) || "NORTH_EAST".equalsIgnoreCase(todo)) 441 ANCHOR = NORTH_EAST; 442 else if ("CENTER".equalsIgnoreCase(todo)) ANCHOR = CENTER; 443 return (ANCHOR); 444 } 445 446 456 public static int getXGap(int gapId) 457 { 458 gapId = IzPanelLayout.verifyGapId(gapId); 459 if (IzPanelLayout.getDefaultXGap(GAP_LOAD_MARKER) >= 0) 460 return (IzPanelLayout.getDefaultXGap(gapId)); 461 AutomatedInstallData idata = AutomatedInstallData.getInstance(); 462 if (!(idata instanceof InstallData)) return (IzPanelLayout.getDefaultXGap(gapId)); 463 String var = null; 464 InstallData id = (InstallData) idata; 465 int commonDefault = -1; 466 if (id.guiPrefs.modifier.containsKey(ALL_X_GAP)) 467 { 468 try 469 { 470 commonDefault = Integer.parseInt((String ) id.guiPrefs.modifier.get(ALL_X_GAP)); 471 } 472 catch (NumberFormatException nfe) 473 { 474 } 477 478 } 479 for (int i = 0; i < X_GAP_NAME_LOOK_UP.length; ++i) 480 { 481 int currentDefault = 0; 482 if (commonDefault >= 0) 483 { 484 currentDefault = commonDefault; 485 } 486 else 487 { 488 var = (String ) id.guiPrefs.modifier.get(X_GAP_NAME_LOOK_UP[i]); 489 if (var != null) 490 { 491 try 492 { 493 currentDefault = Integer.parseInt(var); 494 } 495 catch (NumberFormatException nfe) 496 { 497 } 500 } 501 } 502 IzPanelLayout.setDefaultXGap(currentDefault, i); 503 } 504 IzPanelLayout.setDefaultXGap(0, GAP_LOAD_MARKER); return (IzPanelLayout.getDefaultXGap(gapId)); 507 } 508 509 519 public static int getYGap(int gapId) 520 { 521 gapId = IzPanelLayout.verifyGapId(gapId); 522 if (IzPanelLayout.getDefaultYGap(GAP_LOAD_MARKER) >= 0) 523 return (IzPanelLayout.getDefaultYGap(gapId)); 524 AutomatedInstallData idata = AutomatedInstallData.getInstance(); 525 if (!(idata instanceof InstallData)) return (IzPanelLayout.getDefaultYGap(gapId)); 526 String var = null; 527 InstallData id = (InstallData) idata; 528 int commonDefault = -1; 529 if (id.guiPrefs.modifier.containsKey(ALL_Y_GAP)) 530 { 531 try 532 { 533 commonDefault = Integer.parseInt((String ) id.guiPrefs.modifier.get(ALL_Y_GAP)); 534 } 535 catch (NumberFormatException nfe) 536 { 537 } 540 541 } 542 for (int i = 0; i < Y_GAP_NAME_LOOK_UP.length; ++i) 543 { 544 int currentDefault = 0; 545 if (commonDefault >= 0) 546 { 547 currentDefault = commonDefault; 548 } 549 else 550 { 551 var = (String ) id.guiPrefs.modifier.get(Y_GAP_NAME_LOOK_UP[i]); 552 if (var != null) 553 { 554 try 555 { 556 currentDefault = Integer.parseInt(var); 557 } 558 catch (NumberFormatException nfe) 559 { 560 } 563 } 564 } 565 IzPanelLayout.setDefaultYGap(currentDefault, i); 566 } 567 IzPanelLayout.setDefaultYGap(0, GAP_LOAD_MARKER); return (IzPanelLayout.getDefaultYGap(gapId)); 570 } 571 572 584 public static int getXStretchType() 585 { 586 if (X_STRETCH_TYPE > -1) return (X_STRETCH_TYPE); 587 X_STRETCH_TYPE = ABSOLUTE_STRETCH; 588 String var = ((String ) getModifierValue(null, "RELATIVE_STRETCH", null, 589 "layoutXStretchType")); 590 if (var != null) 591 { 592 if ("RELATIVE_STRETCH".equalsIgnoreCase(var) || "RELATIVE".equalsIgnoreCase(var)) 593 X_STRETCH_TYPE = RELATIVE_STRETCH; 594 else if ("ABSOLUTE_STRETCH".equalsIgnoreCase(var) || "ABSOLUTE".equalsIgnoreCase(var)) 595 X_STRETCH_TYPE = ABSOLUTE_STRETCH; 596 else if ("NO_STRETCH".equalsIgnoreCase(var) || "NO".equalsIgnoreCase(var)) 597 X_STRETCH_TYPE = NO_STRETCH; 598 } 599 return (X_STRETCH_TYPE); 600 } 601 602 614 public static int getYStretchType() 615 { 616 if (Y_STRETCH_TYPE > -1) return (Y_STRETCH_TYPE); 617 Y_STRETCH_TYPE = ABSOLUTE_STRETCH; 618 String var = ((String ) getModifierValue(null, "RELATIVE_STRETCH", null, 619 "layoutYStretchType")); 620 if (var != null) 621 { 622 if ("RELATIVE_STRETCH".equalsIgnoreCase(var) || "RELATIVE".equalsIgnoreCase(var)) 623 Y_STRETCH_TYPE = RELATIVE_STRETCH; 624 else if ("ABSOLUTE_STRETCH".equalsIgnoreCase(var) || "ABSOLUTE".equalsIgnoreCase(var)) 625 Y_STRETCH_TYPE = ABSOLUTE_STRETCH; 626 else if ("NO_STRETCH".equalsIgnoreCase(var) || "NO".equalsIgnoreCase(var)) 627 Y_STRETCH_TYPE = NO_STRETCH; 628 } 629 return (Y_STRETCH_TYPE); 630 } 631 632 640 641 public static double getFullLineStretch() 642 { 643 FULL_LINE_STRETCH_DEFAULT = ((Double ) getModifierValue( 644 new Double (FULL_LINE_STRETCH_DEFAULT), INITIAL_STRETCH_DEFAULT, DOUBLE_ZERO, 645 "layoutFullLineStretch")).doubleValue(); 646 return (FULL_LINE_STRETCH_DEFAULT); 647 } 648 649 657 658 public static double getFullColumnStretch() 659 { 660 FULL_COLUMN_STRETCH_DEFAULT = ((Double ) getModifierValue(new Double ( 661 FULL_COLUMN_STRETCH_DEFAULT), INITIAL_STRETCH_DEFAULT, DOUBLE_ZERO, 662 "layoutFullColumnStretch")).doubleValue(); 663 return (FULL_COLUMN_STRETCH_DEFAULT); 664 } 665 666 private static Object getModifierValue(Object currentVal, Object defaultVal, Object readLimit, 667 String key) 668 { 669 if (defaultVal instanceof Integer ) 670 if (((Integer ) currentVal).intValue() >= ((Integer ) readLimit).intValue()) 671 return (currentVal); 672 if (defaultVal instanceof Double ) 673 { 674 if (((Double ) currentVal).doubleValue() >= ((Double ) readLimit).doubleValue()) 675 return (currentVal); 676 } 677 Object retval = defaultVal; 678 AutomatedInstallData idata = AutomatedInstallData.getInstance(); 679 if (!(idata instanceof InstallData)) return (retval); 680 String var = null; 681 if (((InstallData) idata).guiPrefs.modifier.containsKey(key)) 682 { 683 var = (String ) ((InstallData) idata).guiPrefs.modifier.get(key); 684 if (var != null) 685 { 686 try 687 { 688 if (defaultVal instanceof Integer ) return (new Integer (Integer.parseInt(var))); 689 if (defaultVal instanceof Double ) { return (new Double (Double.parseDouble(var))); } 690 return (var); 691 } 692 catch (NumberFormatException nfe) 693 { 694 } 697 } 698 } 699 return (retval); 700 } 701 702 708 public LayoutManager2 getLayout() 709 { 710 return izPanelLayout; 711 } 712 713 718 public void setLayout(LayoutManager2 izPanelLayout) 719 { 720 this.izPanelLayout = izPanelLayout; 721 } 722 723 } 724 | Popular Tags |