1 7 8 package javax.swing.colorchooser; 9 10 import javax.swing.*; 11 import java.awt.*; 12 import java.awt.event.*; 13 import javax.swing.event.*; 14 import javax.swing.text.*; 15 import java.io.Serializable ; 16 17 18 24 class SmartGridLayout implements LayoutManager, Serializable { 25 26 int rows = 2; 27 int columns = 2; 28 int xGap = 2; 29 int yGap = 2; 30 int componentCount = 0; 31 Component[][] layoutGrid; 32 33 34 public SmartGridLayout(int numColumns, int numRows) { 35 rows = numRows; 36 columns = numColumns; 37 layoutGrid = new Component[numColumns][numRows]; 38 39 } 40 41 42 public void layoutContainer(Container c) { 43 44 buildLayoutGrid(c); 45 46 int[] rowHeights = new int[rows]; 47 int[] columnWidths = new int[columns]; 48 49 for (int row = 0; row < rows; row++) { 50 rowHeights[row] = computeRowHeight(row); 51 } 52 53 for (int column = 0; column < columns; column++) { 54 columnWidths[column] = computeColumnWidth(column); 55 } 56 57 58 Insets insets = c.getInsets(); 59 60 if (c.getComponentOrientation().isLeftToRight()) { 61 int horizLoc = insets.left; 62 for (int column = 0; column < columns; column++) { 63 int vertLoc = insets.top; 64 65 for (int row = 0; row < rows; row++) { 66 Component current = layoutGrid[column][row]; 67 68 current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]); 69 vertLoc += (rowHeights[row] + yGap); 71 } 72 horizLoc += (columnWidths[column] + xGap ); 73 } 74 } else { 75 int horizLoc = c.getWidth() - insets.right; 76 for (int column = 0; column < columns; column++) { 77 int vertLoc = insets.top; 78 horizLoc -= columnWidths[column]; 79 80 for (int row = 0; row < rows; row++) { 81 Component current = layoutGrid[column][row]; 82 83 current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]); 84 vertLoc += (rowHeights[row] + yGap); 86 } 87 horizLoc -= xGap; 88 } 89 } 90 91 92 93 } 94 95 public Dimension minimumLayoutSize(Container c) { 96 97 buildLayoutGrid(c); 98 Insets insets = c.getInsets(); 99 100 101 102 int height = 0; 103 int width = 0; 104 105 for (int row = 0; row < rows; row++) { 106 height += computeRowHeight(row); 107 } 108 109 for (int column = 0; column < columns; column++) { 110 width += computeColumnWidth(column); 111 } 112 113 height += (yGap * (rows - 1)) + insets.top + insets.bottom; 114 width += (xGap * (columns - 1)) + insets.right + insets.left; 115 116 return new Dimension(width, height); 117 118 119 } 120 121 public Dimension preferredLayoutSize(Container c) { 122 return minimumLayoutSize(c); 123 } 124 125 126 public void addLayoutComponent(String s, Component c) {} 127 128 public void removeLayoutComponent(Component c) {} 129 130 131 private void buildLayoutGrid(Container c) { 132 133 Component[] children = c.getComponents(); 134 135 for (int componentCount = 0; componentCount < children.length; componentCount++) { 136 int row = 0; 138 int column = 0; 139 140 if (componentCount != 0) { 141 column = componentCount % columns; 142 row = (componentCount - column) / columns; 143 } 144 145 147 layoutGrid[column][row] = children[componentCount]; 148 } 149 } 150 151 private int computeColumnWidth(int columnNum) { 152 int maxWidth = 1; 153 for (int row = 0; row < rows; row++) { 154 int width = layoutGrid[columnNum][row].getPreferredSize().width; 155 if (width > maxWidth) { 156 maxWidth = width; 157 } 158 } 159 return maxWidth; 160 } 161 162 private int computeRowHeight(int rowNum) { 163 int maxHeight = 1; 164 for (int column = 0; column < columns; column++) { 165 int height = layoutGrid[column][rowNum].getPreferredSize().height; 166 if (height > maxHeight) { 167 maxHeight = height; 168 } 169 } 170 return maxHeight; 171 } 172 173 } 174 | Popular Tags |