1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 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 /** 29 * A vertical layout manager similar to java.awt.FlowLayout. Like FlowLayout 30 * components do not expand to fill available space except when the horizontal 31 * alignment is <code>BOTH</code> in which case components are stretched 32 * horizontally. Unlike FlowLayout, components will not wrap to form another 33 * column if there isn't enough space vertically. VerticalLayout can optionally 34 * anchor components to the top or bottom of the display area or center them 35 * between the top and bottom. Revision date 04 April 1999 36 * 37 * @version $Revision: 674365 $ 38 */ 39 public class VerticalLayout implements LayoutManager, Serializable { 40 /** 41 * The horizontal alignment constant that designates centering. Also used to 42 * designate center anchoring. 43 */ 44 public final static int CENTER = 0; 45 46 /** 47 * The horizontal alignment constant that designates right justification. 48 */ 49 public final static int RIGHT = 1; 50 51 /** 52 * The horizontal alignment constant that designates left justification. 53 */ 54 public final static int LEFT = 2; 55 56 /** 57 * The horizontal alignment constant that designates stretching the 58 * component horizontally. 59 */ 60 public final static int BOTH = 3; 61 62 /** 63 * The anchoring constant that designates anchoring to the top of the 64 * display area. 65 */ 66 public final static int TOP = 1; 67 68 /** 69 * The anchoring constant that designates anchoring to the bottom of the 70 * display area. 71 */ 72 public final static int BOTTOM = 2; 73 74 /** The vertical vgap between components...defaults to 5. */ 75 private int vgap; 76 77 /** LEFT, RIGHT, CENTER or BOTH...how the components are justified. */ 78 private int alignment; 79 80 /** 81 * TOP, BOTTOM or CENTER ...where are the components positioned in an 82 * overlarge space. 83 */ 84 private int anchor; 85 86 // Constructors 87 /** 88 * Constructs an instance of VerticalLayout with a vertical vgap of 5 89 * pixels, horizontal centering and anchored to the top of the display area. 90 */ 91 public VerticalLayout() { 92 this(5, CENTER, TOP); 93 } 94 95 /** 96 * Constructs a VerticalLayout instance with horizontal centering, anchored 97 * to the top with the specified vgap. 98 * 99 * @param vgap 100 * an int value indicating the vertical seperation of the 101 * components 102 */ 103 public VerticalLayout(int vgap) { 104 this(vgap, CENTER, TOP); 105 } 106 107 /** 108 * Constructs a VerticalLayout instance anchored to the top with the 109 * specified vgap and horizontal alignment. 110 * 111 * @param vgap 112 * an int value indicating the vertical seperation of the 113 * components 114 * @param alignment 115 * an int value which is one of <code>RIGHT, LEFT, 116 * CENTER, BOTH</code> 117 * for the horizontal alignment. 118 */ 119 public VerticalLayout(int vgap, int alignment) { 120 this(vgap, alignment, TOP); 121 } 122 123 /** 124 * Constructs a VerticalLayout instance with the specified vgap, horizontal 125 * alignment and anchoring 126 * 127 * @param vgap 128 * an int value indicating the vertical seperation of the 129 * components 130 * @param alignment 131 * an int value which is one of <code>RIGHT, LEFT, CENTER, 132 * BOTH</code> 133 * for the horizontal alignment. 134 * @param anchor 135 * an int value which is one of <code>TOP, BOTTOM, 136 * CENTER</code> 137 * indicating where the components are to appear if the display 138 * area exceeds the minimum necessary. 139 */ 140 public VerticalLayout(int vgap, int alignment, int anchor) { 141 this.vgap = vgap; 142 this.alignment = alignment; 143 this.anchor = anchor; 144 } 145 146 /** 147 * Lays out the container. 148 */ 149 public void layoutContainer(Container parent) { 150 Insets insets = parent.getInsets(); 151 // NOTUSED Dimension dim = layoutSize(parent, false); 152 synchronized (parent.getTreeLock()) { 153 int n = parent.getComponentCount(); 154 Dimension pd = parent.getSize(); 155 int y = 0; 156 // work out the total size 157 for (int i = 0; i < n; i++) { 158 Component c = parent.getComponent(i); 159 Dimension d = c.getPreferredSize(); 160 y += d.height + vgap; 161 } 162 y -= vgap; // otherwise there's a vgap too many 163 // Work out the anchor paint 164 if (anchor == TOP) { 165 y = insets.top; 166 } else if (anchor == CENTER) { 167 y = (pd.height - y) / 2; 168 } else { 169 y = pd.height - y - insets.bottom; 170 } 171 // do layout 172 for (int i = 0; i < n; i++) { 173 Component c = parent.getComponent(i); 174 Dimension d = c.getPreferredSize(); 175 int x = insets.left; 176 int wid = d.width; 177 if (alignment == CENTER) { 178 x = (pd.width - d.width) / 2; 179 } else if (alignment == RIGHT) { 180 x = pd.width - d.width - insets.right; 181 } else if (alignment == BOTH) { 182 wid = pd.width - insets.left - insets.right; 183 } 184 c.setBounds(x, y, wid, d.height); 185 y += d.height + vgap; 186 } 187 } 188 } 189 190 public Dimension minimumLayoutSize(Container parent) { 191 return layoutSize(parent, true); 192 } 193 194 public Dimension preferredLayoutSize(Container parent) { 195 return layoutSize(parent, false); 196 } 197 198 /** 199 * Not used by this class. 200 */ 201 public void addLayoutComponent(String name, Component comp) { 202 } 203 204 /** 205 * Not used by this class. 206 */ 207 public void removeLayoutComponent(Component comp) { 208 } 209 210 public String toString() { 211 return getClass().getName() + "[vgap=" + vgap + " align=" + alignment + " anchor=" + anchor + "]"; 212 } 213 214 private Dimension layoutSize(Container parent, boolean minimum) { 215 Dimension dim = new Dimension(0, 0); 216 Dimension d; 217 synchronized (parent.getTreeLock()) { 218 int n = parent.getComponentCount(); 219 for (int i = 0; i < n; i++) { 220 Component c = parent.getComponent(i); 221 if (c.isVisible()) { 222 d = minimum ? c.getMinimumSize() : c.getPreferredSize(); 223 dim.width = Math.max(dim.width, d.width); 224 dim.height += d.height; 225 if (i > 0) { 226 dim.height += vgap; 227 } 228 } 229 } 230 } 231 Insets insets = parent.getInsets(); 232 dim.width += insets.left + insets.right; 233 dim.height += insets.top + insets.bottom + vgap + vgap; 234 return dim; 235 } 236 }