You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIUtilities.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui;
  23. import java.awt.Component;
  24. import java.awt.Container;
  25. import java.awt.Dimension;
  26. import javax.swing.Spring;
  27. import javax.swing.SpringLayout;
  28. /**
  29. * UI constants.
  30. */
  31. public final class UIUtilities {
  32. /** Size of a large border. */
  33. public static final int LARGE_BORDER = 10;
  34. /** Size of a small border. */
  35. public static final int SMALL_BORDER = 5;
  36. /** Standard button size. */
  37. public static final Dimension BUTTON_SIZE = new Dimension(100, 25);
  38. /** Not intended to be instatiated. */
  39. private UIUtilities() {
  40. }
  41. /**
  42. * Aligns the components in a container horizontally and adds springs
  43. * vertically.
  44. *
  45. * @param parent parent container
  46. * @param rows number of rows
  47. * @param columns number of columns
  48. * @param initialXPadding initial x padding
  49. * @param initialYPadding initial y padding
  50. * @param xPadding x padding
  51. * @param yPadding y padding
  52. */
  53. public static void layoutGrid(final Container parent, final int rows,
  54. final int columns, final int initialXPadding,
  55. final int initialYPadding, final int xPadding, final int yPadding) {
  56. final SpringLayout layout = (SpringLayout) parent.getLayout();
  57. Spring x = Spring.constant(initialXPadding);
  58. Spring y = Spring.constant(initialYPadding);
  59. SpringLayout.Constraints constraints;
  60. for (int c = 0; c < columns; c++) {
  61. Spring width = Spring.constant(0);
  62. for (int r = 0; r < rows; r++) {
  63. width = Spring.max(width,
  64. getConstraintsForCell(r, c, parent, columns).
  65. getWidth());
  66. }
  67. for (int r = 0; r < rows; r++) {
  68. constraints = getConstraintsForCell(r, c, parent, columns);
  69. constraints.setX(x);
  70. constraints.setWidth(width);
  71. }
  72. x = Spring.sum(x, Spring.sum(width, Spring.constant(xPadding)));
  73. }
  74. for (int r = 0; r < rows; r++) {
  75. int height = 0;
  76. for (int c = 0; c < columns; c++) {
  77. height +=
  78. getConstraintsForCell(r, c, parent, columns).
  79. getHeight().getValue();
  80. }
  81. for (int c = 0; c < columns; c++) {
  82. constraints = getConstraintsForCell(r, c, parent, columns);
  83. constraints.setY(y);
  84. constraints.setHeight(Spring.constant(height));
  85. }
  86. y = Spring.sum(y, Spring.sum(Spring.constant(height),
  87. Spring.constant(yPadding)));
  88. }
  89. final SpringLayout.Constraints pCons = layout.getConstraints(parent);
  90. pCons.setConstraint(SpringLayout.SOUTH, y);
  91. pCons.setConstraint(SpringLayout.EAST, x);
  92. }
  93. /**
  94. * Returns the constraints for a specific cell.
  95. *
  96. * @param row Row of cell
  97. * @param column Column of cell
  98. * @param parent parent container
  99. * @param columns number of columns
  100. *
  101. * @return Constraits for a specific cell
  102. */
  103. private static SpringLayout.Constraints getConstraintsForCell(final int row,
  104. final int column, final Container parent, final int columns) {
  105. final SpringLayout layout = (SpringLayout) parent.getLayout();
  106. final Component constraints = parent.getComponent(row * columns + column);
  107. return layout.getConstraints(constraints);
  108. }
  109. }