1 19 20 package swingwtx.custom.layout; 21 22 import swingwt.awt.*; 23 24 30 public class TableLayout implements LayoutManager, java.io.Serializable { 31 32 int hgap = 4; 33 int vgap = 4; 34 int rows; 35 int cols; 36 40 boolean leftToRight = true; 41 42 public TableLayout() { 43 this(1, 0, 4, 4); 44 } 45 46 public TableLayout(int rows, int cols) { 47 this(rows, cols, 4, 4, true); 48 } 49 50 public TableLayout(int rows, int cols, boolean leftToRight) { 51 this(rows, cols, 4, 4, leftToRight); 52 } 53 54 public TableLayout(int rows, int cols, int hgap, int vgap) { 55 this(rows, cols, hgap, vgap, true); 56 } 57 58 public TableLayout(int rows, int cols, int hgap, int vgap, boolean leftToRight) { 59 if ((rows == 0) && (cols == 0)) { 60 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 61 } 62 this.rows = rows; 63 this.cols = cols; 64 this.hgap = hgap; 65 this.vgap = vgap; 66 this.leftToRight = leftToRight; 67 } 68 69 public int getRows() { 70 return rows; 71 } 72 73 public void setRows(int rows) { 74 if ((rows == 0) && (this.cols == 0)) { 75 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 76 } 77 this.rows = rows; 78 } 79 80 public int getColumns() { 81 return cols; 82 } 83 84 public void setColumns(int cols) { 85 if ((cols == 0) && (this.rows == 0)) { 86 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 87 } 88 this.cols = cols; 89 } 90 91 public int getHgap() { 92 return hgap; 93 } 94 95 public void setHgap(int hgap) { 96 this.hgap = hgap; 97 } 98 99 public int getVgap() { 100 return vgap; 101 } 102 103 public void setVgap(int vgap) { 104 this.vgap = vgap; 105 } 106 107 public void addLayoutComponent(String name, Component comp) { 108 } 109 110 public void removeLayoutComponent(Component comp) { 111 } 112 113 public Dimension preferredLayoutSize(Container parent) { 114 Insets insets = parent.getInsets(); 115 int ncomponents = parent.getComponentCount(); 116 int nrows = rows; 117 int ncols = cols; 118 119 if (nrows > 0) { 120 ncols = (ncomponents + nrows - 1) / nrows; 121 } else { 122 nrows = (ncomponents + ncols - 1) / ncols; 123 } 124 int w = 0; 125 int h = 0; 126 for (int i = 0; i < ncomponents; i++) { 127 Component comp = parent.getComponent(i); 128 Dimension d = comp.getPreferredSize(); 129 if (w < d.width) { 130 w = d.width; 131 } 132 if (h < d.height) { 133 h = d.height; 134 } 135 } 136 return new Dimension( 137 insets.left + insets.right + ncols * w + (ncols - 1) * hgap, 138 insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap); 139 } 140 141 public Dimension minimumLayoutSize(Container parent) { 142 Insets insets = parent.getInsets(); 143 int ncomponents = parent.getComponentCount(); 144 int nrows = rows; 145 int ncols = cols; 146 147 if (nrows > 0) { 148 ncols = (ncomponents + nrows - 1) / nrows; 149 } else { 150 nrows = (ncomponents + ncols - 1) / ncols; 151 } 152 int w = 0; 153 int h = 0; 154 for (int i = 0; i < ncomponents; i++) { 155 Component comp = parent.getComponent(i); 156 Dimension d = comp.getMinimumSize(); 157 if (w < d.width) { 158 w = d.width; 159 } 160 if (h < d.height) { 161 h = d.height; 162 } 163 } 164 return new Dimension( 165 insets.left + insets.right + ncols * w + (ncols - 1) * hgap, 166 insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap); 167 } 168 169 public void layoutContainer(Container parent) { 170 171 Insets insets = parent.getInsets(); 172 int ncomponents = parent.getComponentCount(); 173 int nrows = rows; 174 int ncols = cols; 175 176 if (ncomponents == 0) { 177 return; 178 } 179 180 if (leftToRight) { 181 int[] maxWidths = new int[(cols)]; 184 185 int cc = 0; for (int i = 0; i < ncomponents; i++) { 187 int cw = parent.getComponent(i).getPreferredSize().width; 188 if (cw > maxWidths[cc]) 189 maxWidths[cc] = cw; 190 cc++; 191 if (cc == cols) cc = 0; 192 } 193 194 int[] colPoints = new int[(cols)]; 197 for (int i = 0; i < cols; i++) { 198 if (i != 0) 199 colPoints[i] = colPoints[i-1] + hgap + maxWidths[i-1]; 200 else 201 colPoints[i] = hgap; 202 } 203 204 cc = 0; 205 int highestInRow = 0; 206 int curRowPos = 0; 207 for (int i = 0; i < parent.getComponentCount(); i++) { 208 parent.getComponent(i).setBounds( 209 colPoints[cc], 210 curRowPos, 211 parent.getComponent(i).getPreferredSize().width, 212 parent.getComponent(i).getPreferredSize().height 213 ); 214 if (parent.getComponent(i).getPreferredSize().height > highestInRow) 215 highestInRow = parent.getComponent(i).getPreferredSize().height; 216 cc++; 217 if (cc == cols) { 218 cc = 0; 219 curRowPos += highestInRow + vgap; 222 highestInRow = 0; 223 } 224 } 225 } 226 else { 227 228 231 int totRows = parent.getHeight() / (parent.getComponent(0).getPreferredSize().height + vgap); 235 236 int[] maxWidths = new int[(cols * 2)]; 239 240 int cr = 0; int cc = 0; for (int i = 0; i < ncomponents; i++) { 243 int ch = parent.getComponent(i).getPreferredSize().width; 244 if (ch > maxWidths[cc]) 245 maxWidths[cc] = ch; 246 cr++; 247 if (cr == totRows) { cr = 0; cc++; } 248 } 249 if (cols < cc) cols = cc; 252 253 int[] colPoints = new int[(cols * 2)]; 256 for (int i = 0; i < cols; i++) { 257 if (i != 0) 258 colPoints[i] = colPoints[i-1] + hgap + maxWidths[i-1]; 259 else 260 colPoints[i] = hgap; 261 } 262 263 cc = 0; 264 int curRowPos = 0; 265 cr = 0; 266 for (int i = 0; i < parent.getComponentCount(); i++) { 267 parent.getComponent(i).setBounds( 268 colPoints[cc], 269 curRowPos, 270 parent.getComponent(i).getPreferredSize().width, 271 parent.getComponent(i).getPreferredSize().height 272 ); 273 cr++; 274 275 curRowPos += parent.getComponent(i).getPreferredSize().height + vgap; 277 278 if (cr == totRows) { 279 cr = 0; 280 curRowPos = 0; 281 cc++; 282 } 283 } 284 } 285 } 286 } 287 | Popular Tags |