1. /*
  2. * @(#)SmartGridLayout.java 1.8 03/12/19
  3. *
  4. * Copyright 2004 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.8 12/19/03
  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. if (c.getComponentOrientation().isLeftToRight()) {
  44. int horizLoc = insets.left;
  45. for (int column = 0; column < columns; column++) {
  46. int vertLoc = insets.top;
  47. for (int row = 0; row < rows; row++) {
  48. Component current = layoutGrid[column][row];
  49. current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
  50. // System.out.println(current.getBounds());
  51. vertLoc += (rowHeights[row] + yGap);
  52. }
  53. horizLoc += (columnWidths[column] + xGap );
  54. }
  55. } else {
  56. int horizLoc = c.getWidth() - insets.right;
  57. for (int column = 0; column < columns; column++) {
  58. int vertLoc = insets.top;
  59. horizLoc -= columnWidths[column];
  60. for (int row = 0; row < rows; row++) {
  61. Component current = layoutGrid[column][row];
  62. current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
  63. // System.out.println(current.getBounds());
  64. vertLoc += (rowHeights[row] + yGap);
  65. }
  66. horizLoc -= xGap;
  67. }
  68. }
  69. }
  70. public Dimension minimumLayoutSize(Container c) {
  71. buildLayoutGrid(c);
  72. Insets insets = c.getInsets();
  73. int height = 0;
  74. int width = 0;
  75. for (int row = 0; row < rows; row++) {
  76. height += computeRowHeight(row);
  77. }
  78. for (int column = 0; column < columns; column++) {
  79. width += computeColumnWidth(column);
  80. }
  81. height += (yGap * (rows - 1)) + insets.top + insets.bottom;
  82. width += (xGap * (columns - 1)) + insets.right + insets.left;
  83. return new Dimension(width, height);
  84. }
  85. public Dimension preferredLayoutSize(Container c) {
  86. return minimumLayoutSize(c);
  87. }
  88. public void addLayoutComponent(String s, Component c) {}
  89. public void removeLayoutComponent(Component c) {}
  90. private void buildLayoutGrid(Container c) {
  91. Component[] children = c.getComponents();
  92. for (int componentCount = 0; componentCount < children.length; componentCount++) {
  93. // System.out.println("Children: " +componentCount);
  94. int row = 0;
  95. int column = 0;
  96. if (componentCount != 0) {
  97. column = componentCount % columns;
  98. row = (componentCount - column) / columns;
  99. }
  100. // System.out.println("inserting into: "+ column + " " + row);
  101. layoutGrid[column][row] = children[componentCount];
  102. }
  103. }
  104. private int computeColumnWidth(int columnNum) {
  105. int maxWidth = 1;
  106. for (int row = 0; row < rows; row++) {
  107. int width = layoutGrid[columnNum][row].getPreferredSize().width;
  108. if (width > maxWidth) {
  109. maxWidth = width;
  110. }
  111. }
  112. return maxWidth;
  113. }
  114. private int computeRowHeight(int rowNum) {
  115. int maxHeight = 1;
  116. for (int column = 0; column < columns; column++) {
  117. int height = layoutGrid[column][rowNum].getPreferredSize().height;
  118. if (height > maxHeight) {
  119. maxHeight = height;
  120. }
  121. }
  122. return maxHeight;
  123. }
  124. }