001: /*
002: * Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.awt.*;
029:
030: /**
031: * A horizontal 'bag' of Components. Allocates space for each Component
032: * from left to right.
033: *
034: * @version 1.20 06/05/07
035: * @author Herb Jellinek
036: */
037: public class HorizBagLayout implements LayoutManager {
038:
039: int hgap;
040:
041: /**
042: * Constructs a new HorizBagLayout.
043: */
044: public HorizBagLayout() {
045: this (0);
046: }
047:
048: /**
049: * Constructs a HorizBagLayout with the specified gaps.
050: * @param hgap the horizontal gap
051: */
052: public HorizBagLayout(int hgap) {
053: this .hgap = hgap;
054: }
055:
056: /**
057: * Adds the specified named component to the layout.
058: * @param name the String name
059: * @param comp the component to be added
060: */
061: public void addLayoutComponent(String name, Component comp) {
062: }
063:
064: /**
065: * Removes the specified component from the layout.
066: * @param comp the component to be removed
067: */
068: public void removeLayoutComponent(Component comp) {
069: }
070:
071: /**
072: * Returns the minimum dimensions needed to lay out the components
073: * contained in the specified target container.
074: * @param target the Container on which to do the layout
075: * @see Container
076: * @see #preferredLayoutSize
077: */
078: public Dimension minimumLayoutSize(Container target) {
079: Dimension dim = new Dimension();
080:
081: for (int i = 0; i < target.countComponents(); i++) {
082: Component comp = target.getComponent(i);
083: if (comp.isVisible()) {
084: Dimension d = comp.minimumSize();
085: dim.width += d.width + hgap;
086: dim.height = Math.max(d.height, dim.height);
087: }
088: }
089:
090: Insets insets = target.insets();
091: dim.width += insets.left + insets.right;
092: dim.height += insets.top + insets.bottom;
093:
094: return dim;
095: }
096:
097: /**
098: * Returns the preferred dimensions for this layout given the components
099: * in the specified target container.
100: * @param target the component which needs to be laid out
101: * @see Container
102: * @see #minimumLayoutSize
103: */
104: public Dimension preferredLayoutSize(Container target) {
105: Dimension dim = new Dimension();
106:
107: for (int i = 0; i < target.countComponents(); i++) {
108: Component comp = target.getComponent(i);
109: if (comp.isVisible()) {
110: Dimension d = comp.preferredSize();
111: dim.width += d.width + hgap;
112: dim.height = Math.max(dim.height, d.height);
113: }
114: }
115:
116: Insets insets = target.insets();
117: dim.width += insets.left + insets.right;
118: dim.height += insets.top + insets.bottom;
119:
120: return dim;
121: }
122:
123: /**
124: * Lays out the specified container. This method will actually reshape the
125: * components in the specified target container in order to satisfy the
126: * constraints of the HorizBagLayout object.
127: * @param target the component being laid out
128: * @see Container
129: */
130: public void layoutContainer(Container target) {
131: Insets insets = target.insets();
132: int top = insets.top;
133: int bottom = target.size().height - insets.bottom;
134: int left = insets.left;
135: int right = target.size().width - insets.right;
136:
137: for (int i = 0; i < target.countComponents(); i++) {
138: Component comp = target.getComponent(i);
139: if (comp.isVisible()) {
140: int compWidth = comp.size().width;
141: comp.resize(compWidth, bottom - top);
142: Dimension d = comp.preferredSize();
143: comp.reshape(left, top, d.width, bottom - top);
144: left += d.width + hgap;
145: }
146: }
147: }
148:
149: /**
150: * Returns the String representation of this HorizBagLayout's values.
151: */
152: public String toString() {
153: return getClass().getName() + "[hgap=" + hgap + "]";
154: }
155: }
|