SpringUtilities.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.dbinstaller.util.swing;
  32. import javax.swing.*;
  33. import javax.swing.SpringLayout;
  34. import java.awt.*;
  35. /**
  36. * A 1.4 file that provides utility methods for creating form- or grid-style layouts with SpringLayout. These utilities are used by several programs, such as SpringBox and SpringCompactGrid.
  37. */
  38. public class SpringUtilities
  39. {
  40. /**
  41. * A debugging utility that prints to stdout the component's minimum, preferred, and maximum sizes.
  42. * @param c
  43. */
  44. public static void printSizes(Component c)
  45. {
  46. System.out.println("minimumSize = " + c.getMinimumSize());
  47. System.out.println("preferredSize = " + c.getPreferredSize());
  48. System.out.println("maximumSize = " + c.getMaximumSize());
  49. }
  50. /**
  51. * 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.
  52. * @param parent
  53. * @param rows number of rows
  54. * @param cols number of columns
  55. * @param initialX x location to start the grid at
  56. * @param initialY y location to start the grid at
  57. * @param xPad x padding between cells
  58. * @param yPad y padding between cells
  59. */
  60. public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
  61. {
  62. SpringLayout layout;
  63. try
  64. {
  65. layout = (SpringLayout) parent.getLayout();
  66. }
  67. catch (ClassCastException exc)
  68. {
  69. System.err.println("The first argument to makeGrid must use SpringLayout.");
  70. return;
  71. }
  72. Spring xPadSpring = Spring.constant(xPad);
  73. Spring yPadSpring = Spring.constant(yPad);
  74. Spring initialXSpring = Spring.constant(initialX);
  75. Spring initialYSpring = Spring.constant(initialY);
  76. int max = rows * cols;
  77. // Calculate Springs that are the max of the width/height so that all
  78. // cells have the same size.
  79. Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
  80. Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
  81. for (int i = 1; i < max; i++)
  82. {
  83. SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
  84. maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
  85. maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
  86. }
  87. // Apply the new width/height Spring. This forces all the
  88. // components to have the same size.
  89. for (int i = 0; i < max; i++)
  90. {
  91. SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
  92. cons.setWidth(maxWidthSpring);
  93. cons.setHeight(maxHeightSpring);
  94. }
  95. // Then adjust the x/y constraints of all the cells so that they
  96. // are aligned in a grid.
  97. SpringLayout.Constraints lastCons = null;
  98. SpringLayout.Constraints lastRowCons = null;
  99. for (int i = 0; i < max; i++)
  100. {
  101. SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
  102. if (i % cols == 0)
  103. { // start of new row
  104. lastRowCons = lastCons;
  105. cons.setX(initialXSpring);
  106. }
  107. else
  108. { // x position depends on previous component
  109. cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
  110. }
  111. if (i / cols == 0)
  112. { // first row
  113. cons.setY(initialYSpring);
  114. }
  115. else
  116. { // y position depends on previous row
  117. cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
  118. }
  119. lastCons = cons;
  120. }
  121. // Set the parent's size.
  122. SpringLayout.Constraints pCons = layout.getConstraints(parent);
  123. pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
  124. pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
  125. }
  126. /* Used by makeCompactGrid. */
  127. private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols)
  128. {
  129. SpringLayout layout = (SpringLayout) parent.getLayout();
  130. Component c = parent.getComponent(row * cols + col);
  131. return layout.getConstraints(c);
  132. }
  133. /**
  134. * 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
  135. * them all.
  136. * @param parent
  137. * @param rows number of rows
  138. * @param cols number of columns
  139. * @param initialX x location to start the grid at
  140. * @param initialY y location to start the grid at
  141. * @param xPad x padding between cells
  142. * @param yPad y padding between cells
  143. */
  144. public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
  145. {
  146. SpringLayout layout;
  147. try
  148. {
  149. layout = (SpringLayout) parent.getLayout();
  150. }
  151. catch (ClassCastException exc)
  152. {
  153. System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
  154. return;
  155. }
  156. // Align all cells in each column and make them the same width.
  157. Spring x = Spring.constant(initialX);
  158. for (int c = 0; c < cols; c++)
  159. {
  160. Spring width = Spring.constant(0);
  161. for (int r = 0; r < rows; r++)
  162. {
  163. width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
  164. }
  165. for (int r = 0; r < rows; r++)
  166. {
  167. SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
  168. constraints.setX(x);
  169. constraints.setWidth(width);
  170. }
  171. x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
  172. }
  173. // Align all cells in each row and make them the same height.
  174. Spring y = Spring.constant(initialY);
  175. for (int r = 0; r < rows; r++)
  176. {
  177. Spring height = Spring.constant(0);
  178. for (int c = 0; c < cols; c++)
  179. {
  180. height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
  181. }
  182. for (int c = 0; c < cols; c++)
  183. {
  184. SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
  185. constraints.setY(y);
  186. constraints.setHeight(height);
  187. }
  188. y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
  189. }
  190. // Set the parent's size.
  191. SpringLayout.Constraints pCons = layout.getConstraints(parent);
  192. pCons.setConstraint(SpringLayout.SOUTH, y);
  193. pCons.setConstraint(SpringLayout.EAST, x);
  194. }
  195. }