SpringUtilities.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Oracle or the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. package com.l2jserver.tools.dbinstaller.util.swing;
  32. import java.awt.Component;
  33. import java.awt.Container;
  34. import javax.swing.Spring;
  35. import javax.swing.SpringLayout;
  36. /**
  37. * A 1.4 file that provides utility methods for creating form- or grid-style layouts with SpringLayout.<br>
  38. * These utilities are used by several programs, such as SpringBox and SpringCompactGrid.
  39. */
  40. public class SpringUtilities
  41. {
  42. /**
  43. * A debugging utility that prints to stdout the component's minimum, preferred, and maximum sizes.
  44. * @param c
  45. */
  46. public static void printSizes(Component c)
  47. {
  48. System.out.println("minimumSize = " + c.getMinimumSize());
  49. System.out.println("preferredSize = " + c.getPreferredSize());
  50. System.out.println("maximumSize = " + c.getMaximumSize());
  51. }
  52. /**
  53. * Aligns the first <code>rows</code> * <code>cols</code> components of <code>parent</code> in a grid. Each component is as big as the maximum preferred width and height of the components. The parent is made just big enough to fit them all.
  54. * @param parent
  55. * @param rows number of rows
  56. * @param cols number of columns
  57. * @param initialX x location to start the grid at
  58. * @param initialY y location to start the grid at
  59. * @param xPad x padding between cells
  60. * @param yPad y padding between cells
  61. */
  62. public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
  63. {
  64. SpringLayout layout;
  65. try
  66. {
  67. layout = (SpringLayout) parent.getLayout();
  68. }
  69. catch (ClassCastException exc)
  70. {
  71. System.err.println("The first argument to makeGrid must use SpringLayout.");
  72. return;
  73. }
  74. Spring xPadSpring = Spring.constant(xPad);
  75. Spring yPadSpring = Spring.constant(yPad);
  76. Spring initialXSpring = Spring.constant(initialX);
  77. Spring initialYSpring = Spring.constant(initialY);
  78. int max = rows * cols;
  79. // Calculate Springs that are the max of the width/height so that all
  80. // cells have the same size.
  81. Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
  82. Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
  83. for (int i = 1; i < max; i++)
  84. {
  85. SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
  86. maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
  87. maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
  88. }
  89. // Apply the new width/height Spring. This forces all the
  90. // components to have the same size.
  91. for (int i = 0; i < max; i++)
  92. {
  93. SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
  94. cons.setWidth(maxWidthSpring);
  95. cons.setHeight(maxHeightSpring);
  96. }
  97. // Then adjust the x/y constraints of all the cells so that they
  98. // are aligned in a grid.
  99. SpringLayout.Constraints lastCons = null;
  100. SpringLayout.Constraints lastRowCons = null;
  101. for (int i = 0; i < max; i++)
  102. {
  103. SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
  104. if ((i % cols) == 0)
  105. { // start of new row
  106. lastRowCons = lastCons;
  107. cons.setX(initialXSpring);
  108. }
  109. else
  110. {
  111. // x position depends on previous component
  112. if (lastCons != null)
  113. {
  114. cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
  115. }
  116. }
  117. if ((i / cols) == 0)
  118. {
  119. // first row
  120. cons.setY(initialYSpring);
  121. }
  122. else
  123. {
  124. // y position depends on previous row
  125. if (lastRowCons != null)
  126. {
  127. cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
  128. }
  129. }
  130. lastCons = cons;
  131. }
  132. // Set the parent's size.
  133. SpringLayout.Constraints pCons = layout.getConstraints(parent);
  134. if (lastCons != null)
  135. {
  136. pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
  137. pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
  138. }
  139. }
  140. /* Used by makeCompactGrid. */
  141. private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols)
  142. {
  143. SpringLayout layout = (SpringLayout) parent.getLayout();
  144. Component c = parent.getComponent((row * cols) + col);
  145. return layout.getConstraints(c);
  146. }
  147. /**
  148. * Aligns the first <code>rows</code> * <code>cols</code> components of <code>parent</code> in a grid. Each component in a column is as wide as the maximum preferred width of the components in that column; height is similarly determined for each row. The parent is made just big enough to fit
  149. * them all.
  150. * @param parent
  151. * @param rows number of rows
  152. * @param cols number of columns
  153. * @param initialX x location to start the grid at
  154. * @param initialY y location to start the grid at
  155. * @param xPad x padding between cells
  156. * @param yPad y padding between cells
  157. */
  158. public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
  159. {
  160. SpringLayout layout;
  161. try
  162. {
  163. layout = (SpringLayout) parent.getLayout();
  164. }
  165. catch (ClassCastException exc)
  166. {
  167. System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
  168. return;
  169. }
  170. // Align all cells in each column and make them the same width.
  171. Spring x = Spring.constant(initialX);
  172. for (int c = 0; c < cols; c++)
  173. {
  174. Spring width = Spring.constant(0);
  175. for (int r = 0; r < rows; r++)
  176. {
  177. width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
  178. }
  179. for (int r = 0; r < rows; r++)
  180. {
  181. SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
  182. constraints.setX(x);
  183. constraints.setWidth(width);
  184. }
  185. x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
  186. }
  187. // Align all cells in each row and make them the same height.
  188. Spring y = Spring.constant(initialY);
  189. for (int r = 0; r < rows; r++)
  190. {
  191. Spring height = Spring.constant(0);
  192. for (int c = 0; c < cols; c++)
  193. {
  194. height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
  195. }
  196. for (int c = 0; c < cols; c++)
  197. {
  198. SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
  199. constraints.setY(y);
  200. constraints.setHeight(height);
  201. }
  202. y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
  203. }
  204. // Set the parent's size.
  205. SpringLayout.Constraints pCons = layout.getConstraints(parent);
  206. pCons.setConstraint(SpringLayout.SOUTH, y);
  207. pCons.setConstraint(SpringLayout.EAST, x);
  208. }
  209. }