001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.gui.layout;
020:
021: import java.awt.Component;
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.Insets;
025: import java.awt.LayoutManager;
026: import java.io.Serializable;
027:
028: /**
029: * A vertical layout manager similar to java.awt.FlowLayout. Like FlowLayout
030: * components do not expand to fill available space except when the horizontal
031: * alignment is <code>BOTH</code> in which case components are stretched
032: * horizontally. Unlike FlowLayout, components will not wrap to form another
033: * column if there isn't enough space vertically. VerticalLayout can optionally
034: * anchor components to the top or bottom of the display area or center them
035: * between the top and bottom. Revision date 04 April 1999
036: *
037: * @author Colin Mummery e-mail:equitysoft@iname.com
038: * Homepage:www.kagi.com/equitysoft - Based on 'FlexLayout' in Java
039: * class libraries Vol 2 Chan/Lee Addison-Wesley 1998
040: * @version $Revision: 493784 $
041: */
042: public class VerticalLayout implements LayoutManager, Serializable {
043: /**
044: * The horizontal alignment constant that designates centering. Also used to
045: * designate center anchoring.
046: */
047: public final static int CENTER = 0;
048:
049: /**
050: * The horizontal alignment constant that designates right justification.
051: */
052: public final static int RIGHT = 1;
053:
054: /**
055: * The horizontal alignment constant that designates left justification.
056: */
057: public final static int LEFT = 2;
058:
059: /**
060: * The horizontal alignment constant that designates stretching the
061: * component horizontally.
062: */
063: public final static int BOTH = 3;
064:
065: /**
066: * The anchoring constant that designates anchoring to the top of the
067: * display area.
068: */
069: public final static int TOP = 1;
070:
071: /**
072: * The anchoring constant that designates anchoring to the bottom of the
073: * display area.
074: */
075: public final static int BOTTOM = 2;
076:
077: /** The vertical vgap between components...defaults to 5. */
078: private int vgap;
079:
080: /** LEFT, RIGHT, CENTER or BOTH...how the components are justified. */
081: private int alignment;
082:
083: /**
084: * TOP, BOTTOM or CENTER ...where are the components positioned in an
085: * overlarge space.
086: */
087: private int anchor;
088:
089: // Constructors
090: /**
091: * Constructs an instance of VerticalLayout with a vertical vgap of 5
092: * pixels, horizontal centering and anchored to the top of the display area.
093: */
094: public VerticalLayout() {
095: this (5, CENTER, TOP);
096: }
097:
098: /**
099: * Constructs a VerticalLayout instance with horizontal centering, anchored
100: * to the top with the specified vgap.
101: *
102: * @param vgap
103: * an int value indicating the vertical seperation of the
104: * components
105: */
106: public VerticalLayout(int vgap) {
107: this (vgap, CENTER, TOP);
108: }
109:
110: /**
111: * Constructs a VerticalLayout instance anchored to the top with the
112: * specified vgap and horizontal alignment.
113: *
114: * @param vgap
115: * an int value indicating the vertical seperation of the
116: * components
117: * @param alignment
118: * an int value which is one of <code>RIGHT, LEFT,
119: * CENTER, BOTH</code>
120: * for the horizontal alignment.
121: */
122: public VerticalLayout(int vgap, int alignment) {
123: this (vgap, alignment, TOP);
124: }
125:
126: /**
127: * Constructs a VerticalLayout instance with the specified vgap, horizontal
128: * alignment and anchoring
129: *
130: * @param vgap
131: * an int value indicating the vertical seperation of the
132: * components
133: * @param alignment
134: * an int value which is one of <code>RIGHT, LEFT, CENTER,
135: * BOTH</code>
136: * for the horizontal alignment.
137: * @param anchor
138: * an int value which is one of <code>TOP, BOTTOM,
139: * CENTER</code>
140: * indicating where the components are to appear if the display
141: * area exceeds the minimum necessary.
142: */
143: public VerticalLayout(int vgap, int alignment, int anchor) {
144: this .vgap = vgap;
145: this .alignment = alignment;
146: this .anchor = anchor;
147: }
148:
149: /**
150: * Lays out the container.
151: */
152: public void layoutContainer(Container parent) {
153: Insets insets = parent.getInsets();
154: // NOTUSED Dimension dim = layoutSize(parent, false);
155: synchronized (parent.getTreeLock()) {
156: int n = parent.getComponentCount();
157: Dimension pd = parent.getSize();
158: int y = 0;
159: // work out the total size
160: for (int i = 0; i < n; i++) {
161: Component c = parent.getComponent(i);
162: Dimension d = c.getPreferredSize();
163: y += d.height + vgap;
164: }
165: y -= vgap; // otherwise there's a vgap too many
166: // Work out the anchor paint
167: if (anchor == TOP) {
168: y = insets.top;
169: } else if (anchor == CENTER) {
170: y = (pd.height - y) / 2;
171: } else {
172: y = pd.height - y - insets.bottom;
173: }
174: // do layout
175: for (int i = 0; i < n; i++) {
176: Component c = parent.getComponent(i);
177: Dimension d = c.getPreferredSize();
178: int x = insets.left;
179: int wid = d.width;
180: if (alignment == CENTER) {
181: x = (pd.width - d.width) / 2;
182: } else if (alignment == RIGHT) {
183: x = pd.width - d.width - insets.right;
184: } else if (alignment == BOTH) {
185: wid = pd.width - insets.left - insets.right;
186: }
187: c.setBounds(x, y, wid, d.height);
188: y += d.height + vgap;
189: }
190: }
191: }
192:
193: public Dimension minimumLayoutSize(Container parent) {
194: return layoutSize(parent, true);
195: }
196:
197: public Dimension preferredLayoutSize(Container parent) {
198: return layoutSize(parent, false);
199: }
200:
201: /**
202: * Not used by this class.
203: */
204: public void addLayoutComponent(String name, Component comp) {
205: }
206:
207: /**
208: * Not used by this class.
209: */
210: public void removeLayoutComponent(Component comp) {
211: }
212:
213: public String toString() {
214: return getClass().getName() + "[vgap=" + vgap + " align="
215: + alignment + " anchor=" + anchor + "]";
216: }
217:
218: private Dimension layoutSize(Container parent, boolean minimum) {
219: Dimension dim = new Dimension(0, 0);
220: Dimension d;
221: synchronized (parent.getTreeLock()) {
222: int n = parent.getComponentCount();
223: for (int i = 0; i < n; i++) {
224: Component c = parent.getComponent(i);
225: if (c.isVisible()) {
226: d = minimum ? c.getMinimumSize() : c
227: .getPreferredSize();
228: dim.width = Math.max(dim.width, d.width);
229: dim.height += d.height;
230: if (i > 0) {
231: dim.height += vgap;
232: }
233: }
234: }
235: }
236: Insets insets = parent.getInsets();
237: dim.width += insets.left + insets.right;
238: dim.height += insets.top + insets.bottom + vgap + vgap;
239: return dim;
240: }
241: }
|