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: LayoutUtil.java,v 1.11 2005/02/16 11:28:12 jesper Exp $
023: package net.infonode.gui.layout;
024:
025: import net.infonode.util.Direction;
026:
027: import java.awt.*;
028:
029: public class LayoutUtil {
030: private LayoutUtil() {
031: }
032:
033: public static Component[] getVisibleChildren(Container parent) {
034: return getVisibleChildren(parent.getComponents());
035: }
036:
037: public static Component[] getVisibleChildren(Component[] components) {
038: int count = 0;
039:
040: for (int i = 0; i < components.length; i++)
041: if (components[i].isVisible())
042: count++;
043:
044: Component[] c = new Component[count];
045: int index = 0;
046:
047: for (int i = 0; i < components.length; i++)
048: if (components[i].isVisible())
049: c[index++] = components[i];
050:
051: return c;
052: }
053:
054: public static Rectangle getInteriorArea(Container container) {
055: Insets insets = container.getInsets();
056: return new Rectangle(insets.left, insets.top, container
057: .getWidth()
058: - insets.left - insets.right, container.getHeight()
059: - insets.top - insets.bottom);
060: }
061:
062: public static Dimension getInteriorSize(Container container) {
063: Insets insets = container.getInsets();
064: return new Dimension(container.getWidth() - insets.left
065: - insets.right, container.getHeight() - insets.top
066: - insets.bottom);
067: }
068:
069: public static Dimension rotate(Dimension dim, Direction dir) {
070: return rotate(dim, dir.isHorizontal());
071: }
072:
073: public static Dimension rotate(Dimension dim, boolean horizontal) {
074: return dim == null ? null : horizontal ? dim : new Dimension(
075: dim.height, dim.width);
076: }
077:
078: public static boolean isDescendingFrom(Component component,
079: Component parent) {
080: return component == parent
081: || (component != null && isDescendingFrom(component
082: .getParent(), parent));
083: }
084:
085: public static Dimension getMaxMinimumSize(Component[] components) {
086: int maxWidth = 0;
087: int maxHeight = 0;
088:
089: for (int i = 0; i < components.length; i++) {
090: if (components[i] != null) {
091: Dimension min = components[i].getMinimumSize();
092: int w = min.width;
093: int h = min.height;
094:
095: if (maxHeight < h)
096: maxHeight = h;
097:
098: if (maxWidth < w)
099: maxWidth = w;
100: }
101: }
102:
103: return new Dimension(maxWidth, maxHeight);
104: }
105:
106: public static Dimension getMaxPreferredSize(Component[] components) {
107: int maxWidth = 0;
108: int maxHeight = 0;
109:
110: for (int i = 0; i < components.length; i++) {
111: if (components[i] != null) {
112: Dimension min = components[i].getPreferredSize();
113: int w = min.width;
114: int h = min.height;
115:
116: if (maxHeight < h)
117: maxHeight = h;
118:
119: if (maxWidth < w)
120: maxWidth = w;
121: }
122: }
123:
124: return new Dimension(maxWidth, maxHeight);
125: }
126:
127: public static Dimension getMinMaximumSize(Component[] components) {
128: int minWidth = Integer.MAX_VALUE;
129: int minHeight = Integer.MAX_VALUE;
130:
131: for (int i = 0; i < components.length; i++) {
132: if (components[i] != null) {
133: Dimension min = components[i].getMaximumSize();
134: int w = min.width;
135: int h = min.height;
136:
137: if (minWidth > w)
138: minWidth = w;
139:
140: if (minHeight > h)
141: minHeight = h;
142: }
143: }
144:
145: return new Dimension(minWidth, minHeight);
146: }
147:
148: public static Insets rotate(Direction dir, Insets insets) {
149: return dir == Direction.RIGHT ? insets
150: : dir == Direction.DOWN ? new Insets(insets.right,
151: insets.top, insets.left, insets.bottom)
152: : dir == Direction.LEFT ? new Insets(
153: insets.bottom, insets.right,
154: insets.top, insets.left) : new Insets(
155: insets.left, insets.bottom,
156: insets.right, insets.top);
157: }
158:
159: public static Insets unrotate(Direction dir, Insets insets) {
160: return dir == Direction.RIGHT ? insets
161: : dir == Direction.DOWN ? new Insets(insets.left,
162: insets.bottom, insets.right, insets.top)
163: : dir == Direction.LEFT ? new Insets(
164: insets.bottom, insets.right,
165: insets.top, insets.left) : new Insets(
166: insets.right, insets.top, insets.left,
167: insets.bottom);
168: }
169:
170: public static Dimension add(Dimension dim, Insets insets) {
171: return new Dimension(dim.width + insets.left + insets.right,
172: dim.height + insets.top + insets.bottom);
173: }
174:
175: public static Dimension getValidSize(Dimension dim,
176: Component component) {
177: Dimension minSize = component.getMinimumSize();
178: Dimension maxSize = component.getMaximumSize();
179: return new Dimension(Math.max(minSize.width, Math.min(
180: dim.width, maxSize.width)), Math.max(minSize.height,
181: Math.min(dim.height, maxSize.height)));
182: }
183:
184: public static Component getChildContaining(Component parent,
185: Component component) {
186: return component == null ? null
187: : component.getParent() == parent ? component
188: : getChildContaining(parent, component
189: .getParent());
190: }
191:
192: public static String getBorderLayoutOrientation(Direction direction) {
193: return direction == Direction.UP ? BorderLayout.NORTH
194: : direction == Direction.DOWN ? BorderLayout.SOUTH
195: : direction == Direction.LEFT ? BorderLayout.WEST
196: : BorderLayout.EAST;
197: }
198:
199: }
|