2
0

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