001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: BorderLayout2.java,v 1.8 2005/02/16 11:28:12 jesper Exp $
023: package net.infonode.gui.layout;
024:
025: import java.awt.*;
026:
027: /**
028: * @author $Author: jesper $
029: * @version $Revision: 1.8 $
030: */
031: public class BorderLayout2 implements LayoutManager2 {
032: private Component[][] components;
033:
034: public BorderLayout2() {
035: components = new Component[3][];
036:
037: for (int i = 0; i < components.length; i++) {
038: components[i] = new Component[3];
039: }
040: }
041:
042: public void addLayoutComponent(String name, Component comp) {
043: }
044:
045: public void addLayoutComponent(Component comp, Object constraints) {
046: if (constraints instanceof Point) {
047: Point p = (Point) constraints;
048: components[p.x][p.y] = comp;
049: } else
050: throw new RuntimeException(
051: "BorderLayout2 constraint must be a Point!");
052: }
053:
054: public float getLayoutAlignmentX(Container target) {
055: return target.getAlignmentX();
056: }
057:
058: public float getLayoutAlignmentY(Container target) {
059: return target.getAlignmentY();
060: }
061:
062: public void invalidateLayout(Container target) {
063: }
064:
065: public Dimension maximumLayoutSize(Container target) {
066: int width = 0;
067: int height = 0;
068:
069: for (int i = 0; i < 3; i++) {
070: width += getMaximumWidth(i);
071: height += getMaximumHeight(i);
072: }
073:
074: return new Dimension(width, height);
075: }
076:
077: private int getPreferredHeight(int row) {
078: int maxHeight = 0;
079:
080: for (int i = 0; i < 3; i++) {
081: Component c = components[i][row];
082:
083: if (c != null && c.isVisible()) {
084: int height = c.getPreferredSize().height;
085:
086: if (height > maxHeight)
087: maxHeight = height;
088: }
089: }
090:
091: return maxHeight;
092: }
093:
094: // private static int tt = 0;
095:
096: private int getPreferredWidth(int column) {
097: int maxWidth = 0;
098:
099: for (int i = 0; i < 3; i++) {
100: Component c = components[column][i];
101: if (c != null && c.isVisible()) {
102: int width = c.getPreferredSize().width;
103:
104: if (width > maxWidth)
105: maxWidth = width;
106: }
107: }
108:
109: return maxWidth;
110: }
111:
112: private int getMinimumHeight(int row) {
113: int maxHeight = 0;
114:
115: for (int i = 0; i < 3; i++) {
116: Component c = components[i][row];
117:
118: if (c != null && c.isVisible()) {
119: int height = c.getMinimumSize().height;
120:
121: if (height > maxHeight)
122: maxHeight = height;
123: }
124: }
125:
126: return maxHeight;
127: }
128:
129: private int getMinimumWidth(int column) {
130: int maxWidth = 0;
131:
132: for (int i = 0; i < 3; i++) {
133: Component c = components[column][i];
134:
135: if (c != null && c.isVisible()) {
136: int width = c.getMinimumSize().width;
137:
138: if (width > maxWidth)
139: maxWidth = width;
140: }
141: }
142:
143: return maxWidth;
144: }
145:
146: private int getMaximumHeight(int row) {
147: int minHeight = Integer.MAX_VALUE;
148:
149: for (int i = 0; i < 3; i++) {
150: Component c = components[i][row];
151:
152: if (c != null && c.isVisible()) {
153: int height = c.getMaximumSize().height;
154:
155: if (height < minHeight)
156: minHeight = height;
157: }
158: }
159:
160: return minHeight;
161: }
162:
163: private int getMaximumWidth(int column) {
164: int minWidth = 0;
165:
166: for (int i = 0; i < 3; i++) {
167: Component c = components[column][i];
168:
169: if (c != null && c.isVisible()) {
170: int width = c.getMaximumSize().width;
171:
172: if (width < minWidth)
173: minWidth = width;
174: }
175: }
176:
177: return minWidth;
178: }
179:
180: private static void setBounds(Component component, Rectangle bounds) {
181: int width = Math.min(component.getMaximumSize().width,
182: bounds.width);
183: int height = Math.min(component.getMaximumSize().height,
184: bounds.height);
185: Rectangle r = new Rectangle(
186: bounds.x
187: + (int) (component.getAlignmentX() * (bounds.width - width)),
188: bounds.y
189: + (int) (component.getAlignmentY() * (bounds.height - height)),
190: width, height);
191: component.setBounds(r);
192: }
193:
194: public void layoutContainer(Container parent) {
195: //System.out.println("Laying out container: " + components[2][0].isVisible());
196: Insets insets = parent.getInsets();
197: Dimension innerSize = LayoutUtil.getInteriorSize(parent);
198: int[] w = { getPreferredWidth(0), getPreferredWidth(2) };
199: int[] h = { getPreferredHeight(0), getPreferredHeight(2) };
200:
201: int y = insets.top;
202:
203: for (int row = 0; row < 3; row++) {
204: int height = row == 1 ? innerSize.height - h[0] - h[1]
205: : h[row / 2];
206: int x = insets.left;
207:
208: for (int col = 0; col < 3; col++) {
209: int width = col == 1 ? innerSize.width - w[0] - w[1]
210: : w[col / 2];
211: Component c = components[col][row];
212:
213: if (c != null && c.isVisible()) {
214: setBounds(c, new Rectangle(x, y, width, height));
215: }
216:
217: x += width;
218: }
219:
220: y += height;
221: }
222: //System.out.println("Layout complete: " + components[2][0].isVisible());
223: }
224:
225: public Dimension minimumLayoutSize(Container parent) {
226: int width = 0;
227: int height = 0;
228:
229: for (int i = 0; i < 3; i++) {
230: width += getMinimumWidth(i);
231: height += getMinimumHeight(i);
232: }
233:
234: return new Dimension(width, height);
235: }
236:
237: public Dimension preferredLayoutSize(Container parent) {
238: int width = 0;
239: int height = 0;
240:
241: for (int i = 0; i < 3; i++) {
242: width += getPreferredWidth(i);
243: height += getPreferredHeight(i);
244: }
245:
246: return new Dimension(width, height);
247: }
248:
249: public void removeLayoutComponent(Component comp) {
250: for (int col = 0; col < 3; col++) {
251: for (int row = 0; row < 3; row++) {
252: if (components[col][row] == comp) {
253: components[col][row] = null;
254: return;
255: }
256: }
257: }
258: }
259: }
|