1. /*
  2. * @(#)SmartGridLayout.java 1.3 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.colorchooser;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.event.*;
  12. import javax.swing.text.*;
  13. import java.io.Serializable;
  14. /**
  15. * A better GridLayout class
  16. *
  17. * @version 1.3 11/29/01
  18. * @author Steve Wilson
  19. */
  20. class SmartGridLayout implements LayoutManager, Serializable {
  21. int rows = 2;
  22. int columns = 2;
  23. int xGap = 2;
  24. int yGap = 2;
  25. int componentCount = 0;
  26. Component[][] layoutGrid;
  27. public SmartGridLayout(int numColumns, int numRows) {
  28. rows = numRows;
  29. columns = numColumns;
  30. layoutGrid = new Component[numColumns][numRows];
  31. }
  32. public void layoutContainer(Container c) {
  33. buildLayoutGrid(c);
  34. int[] rowHeights = new int[rows];
  35. int[] columnWidths = new int[columns];
  36. for (int row = 0; row < rows; row++) {
  37. rowHeights[row] = computeRowHeight(row);
  38. }
  39. for (int column = 0; column < columns; column++) {
  40. columnWidths[column] = computeColumnWidth(column);
  41. }
  42. Insets insets = c.getInsets();
  43. int horizLoc = insets.left;
  44. for (int column = 0; column < columns; column++) {
  45. int vertLoc = insets.top;
  46. for (int row = 0; row < rows; row++) {
  47. Component current = layoutGrid[column][row];
  48. current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
  49. // System.out.println(current.getBounds());
  50. vertLoc += (rowHeights[row] + yGap);
  51. }
  52. horizLoc += (columnWidths[column] + xGap );
  53. }
  54. }
  55. public Dimension minimumLayoutSize(Container c) {
  56. buildLayoutGrid(c);
  57. Insets insets = c.getInsets();
  58. int height = 0;
  59. int width = 0;
  60. for (int row = 0; row < rows; row++) {
  61. height += computeRowHeight(row);
  62. }
  63. for (int column = 0; column < columns; column++) {
  64. width += computeColumnWidth(column);
  65. }
  66. height += (yGap * (rows - 1)) + insets.top + insets.bottom;
  67. width += (xGap * (columns - 1)) + insets.right + insets.left;
  68. return new Dimension(width, height);
  69. }
  70. public Dimension preferredLayoutSize(Container c) {
  71. return minimumLayoutSize(c);
  72. }
  73. public void addLayoutComponent(String s, Component c) {}
  74. public void removeLayoutComponent(Component c) {}
  75. private void buildLayoutGrid(Container c) {
  76. Component[] children = c.getComponents();
  77. for (int componentCount = 0; componentCount < children.length; componentCount++) {
  78. // System.out.println("Children: " +componentCount);
  79. int row = 0;
  80. int column = 0;
  81. if (componentCount != 0) {
  82. column = componentCount % columns;
  83. row = (componentCount - column) / columns;
  84. }
  85. // System.out.println("inserting into: "+ column + " " + row);
  86. layoutGrid[column][row] = children[componentCount];
  87. }
  88. }
  89. private int computeColumnWidth(int columnNum) {
  90. int maxWidth = 1;
  91. for (int row = 0; row < rows; row++) {
  92. int width = layoutGrid[columnNum][row].getPreferredSize().width;
  93. if (width > maxWidth) {
  94. maxWidth = width;
  95. }
  96. }
  97. return maxWidth;
  98. }
  99. private int computeRowHeight(int rowNum) {
  100. int maxHeight = 1;
  101. for (int column = 0; column < columns; column++) {
  102. int height = layoutGrid[column][rowNum].getPreferredSize().height;
  103. if (height > maxHeight) {
  104. maxHeight = height;
  105. }
  106. }
  107. return maxHeight;
  108. }
  109. }