1 18 19 package org.apache.jorphan.gui.layout; 20 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import java.awt.Dimension ; 24 import java.awt.Insets ; 25 import java.awt.LayoutManager ; 26 import java.io.Serializable ; 27 28 42 public class VerticalLayout implements LayoutManager , Serializable 43 { 44 48 public final static int CENTER = 0; 49 50 53 public final static int RIGHT = 1; 54 55 58 public final static int LEFT = 2; 59 60 64 public final static int BOTH = 3; 65 66 70 public final static int TOP = 1; 71 72 76 public final static int BOTTOM = 2; 77 78 79 private int vgap; 80 81 82 private int alignment; 83 84 88 private int anchor; 89 90 95 public VerticalLayout() 96 { 97 this(5, CENTER, TOP); 98 } 99 100 107 public VerticalLayout(int vgap) 108 { 109 this(vgap, CENTER, TOP); 110 } 111 112 121 public VerticalLayout(int vgap, int alignment) 122 { 123 this(vgap, alignment, TOP); 124 } 125 126 139 public VerticalLayout(int vgap, int alignment, int anchor) 140 { 141 this.vgap = vgap; 142 this.alignment = alignment; 143 this.anchor = anchor; 144 } 145 146 149 public void layoutContainer(Container parent) 150 { 151 Insets insets = parent.getInsets(); 152 synchronized (parent.getTreeLock()) 154 { 155 int n = parent.getComponentCount(); 156 Dimension pd = parent.getSize(); 157 int y = 0; 158 for (int i = 0; i < n; i++) 160 { 161 Component c = parent.getComponent(i); 162 Dimension d = c.getPreferredSize(); 163 y += d.height + vgap; 164 } 165 y -= vgap; if (anchor == TOP) 168 { 169 y = insets.top; 170 } 171 else if (anchor == CENTER) 172 { 173 y = (pd.height - y) / 2; 174 } 175 else 176 { 177 y = pd.height - y - insets.bottom; 178 } 179 for (int i = 0; i < n; i++) 181 { 182 Component c = parent.getComponent(i); 183 Dimension d = c.getPreferredSize(); 184 int x = insets.left; 185 int wid = d.width; 186 if (alignment == CENTER) 187 { 188 x = (pd.width - d.width) / 2; 189 } 190 else if (alignment == RIGHT) 191 { 192 x = pd.width - d.width - insets.right; 193 } 194 else if (alignment == BOTH) 195 { 196 wid = pd.width - insets.left - insets.right; 197 } 198 c.setBounds(x, y, wid, d.height); 199 y += d.height + vgap; 200 } 201 } 202 } 203 204 public Dimension minimumLayoutSize(Container parent) 205 { 206 return layoutSize(parent, true); 207 } 208 209 public Dimension preferredLayoutSize(Container parent) 210 { 211 return layoutSize(parent, false); 212 } 213 214 217 public void addLayoutComponent(String name, Component comp) 218 { 219 } 220 221 224 public void removeLayoutComponent(Component comp) 225 { 226 } 227 228 public String toString() 229 { 230 return getClass().getName() 231 + "[vgap=" 232 + vgap 233 + " align=" 234 + alignment 235 + " anchor=" 236 + anchor 237 + "]"; 238 } 239 240 private Dimension layoutSize(Container parent, boolean minimum) 241 { 242 Dimension dim = new Dimension (0, 0); 243 Dimension d; 244 synchronized (parent.getTreeLock()) 245 { 246 int n = parent.getComponentCount(); 247 for (int i = 0; i < n; i++) 248 { 249 Component c = parent.getComponent(i); 250 if (c.isVisible()) 251 { 252 d = minimum ? c.getMinimumSize() : c.getPreferredSize(); 253 dim.width = Math.max(dim.width, d.width); 254 dim.height += d.height; 255 if (i > 0) 256 { 257 dim.height += vgap; 258 } 259 } 260 } 261 } 262 Insets insets = parent.getInsets(); 263 dim.width += insets.left + insets.right; 264 dim.height += insets.top + insets.bottom + vgap + vgap; 265 return dim; 266 } 267 } 268 | Popular Tags |