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