1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.Component ; 33 import java.awt.Container ; 34 import java.awt.Dimension ; 35 import java.awt.FlowLayout ; 36 import java.awt.Insets ; 37 38 49 public class DjVerticalFlowLayout extends FlowLayout implements java.io.Serializable 50 { 51 private static final long serialVersionUID = 1L; 52 53 public static final int TOP = 0; 54 public static final int MIDDLE = 1; 55 public static final int BOTTOM = 2; 56 57 int hgap; 59 int vgap; 60 boolean hfill; 61 boolean vfill; 62 63 67 public DjVerticalFlowLayout() 68 { 69 this(TOP, 5, 5, true, false); 70 } 71 72 76 public DjVerticalFlowLayout(boolean hfill, boolean vfill) 77 { 78 this(TOP, 5, 5, hfill, vfill); 79 } 80 81 85 public DjVerticalFlowLayout(int align) 86 { 87 this(align, 5, 5, true, false); 88 } 89 90 95 public DjVerticalFlowLayout(int align, boolean hfill, boolean vfill) 96 { 97 this(align, 5, 5, hfill, vfill); 98 } 99 100 107 public DjVerticalFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill) 108 { 109 setAlignment(align); 110 this.hgap = hgap; 111 this.vgap = vgap; 112 this.hfill = hfill; 113 this.vfill = vfill; 114 } 115 116 122 public int getHgap() 123 { 124 return hgap; 125 } 126 127 130 public void setHgap(int hgap) 131 { 132 super.setHgap(hgap); 133 this.hgap = hgap; 134 } 135 136 139 public int getVgap() 140 { 141 return vgap; 142 } 143 144 147 public void setVgap(int vgap) 148 { 149 super.setVgap(vgap); 150 this.vgap = vgap; 151 } 152 153 158 public Dimension preferredLayoutSize(Container target) 159 { 160 Dimension tarsiz = new Dimension (0, 0); 161 162 for (int i = 0; i < target.getComponentCount(); i++) 163 { 164 Component m = target.getComponent(i); 165 if (m.isVisible()) 166 { 167 Dimension d = m.getPreferredSize(); 168 tarsiz.width = Math.max(tarsiz.width, d.width); 169 if (i > 0) 170 { 171 tarsiz.height += vgap; 172 } 173 tarsiz.height += d.height; 174 } 175 } 176 Insets insets = target.getInsets(); 177 tarsiz.width += insets.left + insets.right + hgap * 2; 178 tarsiz.height += insets.top + insets.bottom + vgap * 2; 179 return tarsiz; 180 } 181 182 186 public Dimension minimumLayoutSize(Container target) 187 { 188 Dimension tarsiz = new Dimension (0, 0); 189 190 for (int i = 0; i < target.getComponentCount(); i++) 191 { 192 Component m = target.getComponent(i); 193 if (m.isVisible()) 194 { 195 Dimension d = m.getMinimumSize(); 196 tarsiz.width = Math.max(tarsiz.width, d.width); 197 if (i > 0) 198 { 199 tarsiz.height += vgap; 200 } 201 tarsiz.height += d.height; 202 } 203 } 204 Insets insets = target.getInsets(); 205 tarsiz.width += insets.left + insets.right + hgap * 2; 206 tarsiz.height += insets.top + insets.bottom + vgap * 2; 207 return tarsiz; 208 } 209 210 public void setVerticalFill(boolean vfill) 211 { 212 this.vfill = vfill; 213 } 214 215 public boolean getVerticalFill() 216 { 217 return vfill; 218 } 219 220 public void setHorizontalFill(boolean hfill) 221 { 222 this.hfill = hfill; 223 } 224 225 public boolean getHorizontalFill() 226 { 227 return hfill; 228 } 229 230 241 private void placethem(Container target, int x, int y, int width, int height, int first, int last) 242 { 243 int align = getAlignment(); 244 if (align == DjVerticalFlowLayout.MIDDLE) y += height / 2; 247 if (align == DjVerticalFlowLayout.BOTTOM) y += height; 248 249 for (int i = first; i < last; i++) 250 { 251 Component m = target.getComponent(i); 252 Dimension md = m.getSize(); 253 if (m.isVisible()) 254 { 255 m.setLocation(x, y); 256 y += vgap + md.height; 257 } 258 } 259 } 260 261 265 public void layoutContainer(Container target) 266 { 267 Insets insets = target.getInsets(); 268 int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap * 2); 269 int maxwidth = target.getSize().width - (insets.left + insets.right + hgap * 2); 270 int numcomp = target.getComponentCount(); 271 int x = insets.left + hgap; 272 int y = 0; 273 int colw = 0, start = 0; 274 275 for (int i = 0; i < numcomp; i++) 276 { 277 Component comp = target.getComponent(i); 278 if (comp.isVisible()) 279 { 280 Dimension compDim = comp.getPreferredSize(); 281 if ((this.vfill) && (i == (numcomp - 1))) 283 { 284 compDim.height = Math.max((maxheight - y), comp.getPreferredSize().height); 285 } 286 287 if (this.hfill) 289 { 290 comp.setSize(maxwidth, compDim.height); 291 compDim.width = maxwidth; 292 } 293 else 294 { 295 comp.setSize(compDim.width, compDim.height); 296 } 297 298 { 308 if (y > 0) y += vgap; 309 y += compDim.height; 310 colw = Math.max(colw, compDim.width); 311 } 312 } 313 } 314 placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp); 315 } 316 } | Popular Tags |