1 36 37 package swingwt.awt; 38 39 45 public class TableLayout implements LayoutManager, java.io.Serializable { 46 47 protected int hgap = 4; 48 protected int vgap = 4; 49 protected int rows; 50 protected int cols; 51 55 protected boolean leftToRight = true; 56 57 public TableLayout() { 58 this(1, 0, 4, 4); 59 } 60 61 public TableLayout(int rows, int cols) { 62 this(rows, cols, 4, 4, true); 63 } 64 65 public TableLayout(int rows, int cols, boolean leftToRight) { 66 this(rows, cols, 4, 4, leftToRight); 67 } 68 69 public TableLayout(int rows, int cols, int hgap, int vgap) { 70 this(rows, cols, hgap, vgap, true); 71 } 72 73 public TableLayout(int rows, int cols, int hgap, int vgap, boolean leftToRight) { 74 if ((rows == 0) && (cols == 0)) { 75 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 76 } 77 this.rows = rows; 78 this.cols = cols; 79 this.hgap = hgap; 80 this.vgap = vgap; 81 this.leftToRight = leftToRight; 82 } 83 84 public int getRows() { 85 return rows; 86 } 87 88 public void setRows(int rows) { 89 if ((rows == 0) && (this.cols == 0)) { 90 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 91 } 92 this.rows = rows; 93 } 94 95 public int getColumns() { 96 return cols; 97 } 98 99 public void setColumns(int cols) { 100 if ((cols == 0) && (this.rows == 0)) { 101 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 102 } 103 this.cols = cols; 104 } 105 106 public int getHgap() { 107 return hgap; 108 } 109 110 public void setHgap(int hgap) { 111 this.hgap = hgap; 112 } 113 114 public int getVgap() { 115 return vgap; 116 } 117 118 public void setVgap(int vgap) { 119 this.vgap = vgap; 120 } 121 122 public void addLayoutComponent(String name, Component comp) { 123 } 124 125 public void removeLayoutComponent(Component comp) { 126 } 127 128 public Dimension preferredLayoutSize(Container parent) { 129 Insets insets = parent.getInsets(); 130 int ncomponents = parent.getComponentCount(); 131 int nrows = rows; 132 int ncols = cols; 133 134 if (nrows > 0) { 135 ncols = (ncomponents + nrows - 1) / nrows; 136 } else { 137 nrows = (ncomponents + ncols - 1) / ncols; 138 } 139 int w = 0; 140 int h = 0; 141 for (int i = 0; i < ncomponents; i++) { 142 Component comp = parent.getComponent(i); 143 Dimension d = comp.getPreferredSize(); 144 if (w < d.width) { 145 w = d.width; 146 } 147 if (h < d.height) { 148 h = d.height; 149 } 150 } 151 return new Dimension( 152 insets.left + insets.right + ncols * w + (ncols - 1) * hgap, 153 insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap); 154 } 155 156 public Dimension minimumLayoutSize(Container parent) { 157 Insets insets = parent.getInsets(); 158 int ncomponents = parent.getComponentCount(); 159 int nrows = rows; 160 int ncols = cols; 161 162 if (nrows > 0) { 163 ncols = (ncomponents + nrows - 1) / nrows; 164 } else { 165 nrows = (ncomponents + ncols - 1) / ncols; 166 } 167 int w = 0; 168 int h = 0; 169 for (int i = 0; i < ncomponents; i++) { 170 Component comp = parent.getComponent(i); 171 Dimension d = comp.getMinimumSize(); 172 if (w < d.width) { 173 w = d.width; 174 } 175 if (h < d.height) { 176 h = d.height; 177 } 178 } 179 return new Dimension( 180 insets.left + insets.right + ncols * w + (ncols - 1) * hgap, 181 insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap); 182 } 183 184 public void layoutContainer(Container parent) { 185 186 Insets insets = parent.getInsets(); 187 int ncomponents = parent.getComponentCount(); 188 int nrows = rows; 189 int ncols = cols; 190 191 if (ncomponents == 0) { 192 return; 193 } 194 195 if (leftToRight) { 196 int[] maxWidths = new int[(cols)]; 199 200 int cc = 0; for (int i = 0; i < ncomponents; i++) { 202 int cw = parent.getComponent(i).getPreferredSize().width; 203 if (cw > maxWidths[cc]) 204 maxWidths[cc] = cw; 205 cc++; 206 if (cc == cols) cc = 0; 207 } 208 209 int[] colPoints = new int[(cols)]; 212 for (int i = 0; i < cols; i++) { 213 if (i != 0) 214 colPoints[i] = colPoints[i-1] + hgap + maxWidths[i-1]; 215 else 216 colPoints[i] = hgap; 217 } 218 219 cc = 0; 220 int highestInRow = 0; 221 int curRowPos = 0; 222 for (int i = 0; i < parent.getComponentCount(); i++) { 223 parent.getComponent(i).setBounds( 224 colPoints[cc], 225 curRowPos, 226 parent.getComponent(i).getPreferredSize().width, 227 parent.getComponent(i).getPreferredSize().height 228 ); 229 if (parent.getComponent(i).getPreferredSize().height > highestInRow) 230 highestInRow = parent.getComponent(i).getPreferredSize().height; 231 cc++; 232 if (cc == cols) { 233 cc = 0; 234 curRowPos += highestInRow + vgap; 237 highestInRow = 0; 238 } 239 } 240 } 241 else { 242 243 246 int totRows = parent.getHeight() / (parent.getComponent(0).getPreferredSize().height + vgap); 250 251 int[] maxWidths = new int[(cols * 20)]; 254 255 int cr = 0; int cc = 0; for (int i = 0; i < ncomponents; i++) { 258 int ch = parent.getComponent(i).getPreferredSize().width; 259 if (ch > maxWidths[cc]) 260 maxWidths[cc] = ch; 261 cr++; 262 if (cr == totRows) { cr = 0; cc++; } 263 } 264 if (cols < cc) cols = cc; 267 268 int[] colPoints = new int[(cols * 2)]; 271 for (int i = 0; i < cols; i++) { 272 if (i != 0) 273 colPoints[i] = colPoints[i-1] + hgap + maxWidths[i-1]; 274 else 275 colPoints[i] = hgap; 276 } 277 278 cc = 0; 279 int curRowPos = 0; 280 cr = 0; 281 for (int i = 0; i < parent.getComponentCount(); i++) { 282 parent.getComponent(i).setBounds( 283 colPoints[cc], 284 curRowPos, 285 parent.getComponent(i).getPreferredSize().width, 286 parent.getComponent(i).getPreferredSize().height 287 ); 288 cr++; 289 290 curRowPos += parent.getComponent(i).getPreferredSize().height + vgap; 292 293 if (cr == totRows) { 294 cr = 0; 295 curRowPos = 0; 296 cc++; 297 } 298 } 299 } 300 } 301 } 302 | Popular Tags |