001: /*
002: * XYLayout.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
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 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, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.layouts;
023:
024: import java.awt.Component;
025: import java.awt.Container;
026: import java.awt.Dimension;
027: import java.awt.Insets;
028: import java.awt.LayoutManager2;
029: import java.awt.Rectangle;
030:
031: import java.io.Serializable;
032:
033: import java.util.Hashtable;
034:
035: /* ----------------------------------------------------------
036: * CVS NOTE: Changes to the CVS repository prior to the
037: * release of version 3.0.0beta1 has meant a
038: * resetting of CVS revision numbers.
039: * ----------------------------------------------------------
040: */
041:
042: /**
043: *
044: * @author Takis Diakoumis
045: * @version $Revision: 1.4 $
046: * @date $Date: 2006/05/14 06:56:07 $
047: */
048: public class XYLayout implements LayoutManager2, Serializable {
049:
050: private static XYConstraints defaultConstraints;
051:
052: private int width;
053: private int height;
054: private Hashtable info;
055:
056: public XYLayout() {
057: info = new Hashtable();
058: }
059:
060: public XYLayout(int i, int j) {
061: info = new Hashtable();
062: width = i;
063: height = j;
064: }
065:
066: public int getWidth() {
067: return width;
068: }
069:
070: public void setWidth(int i) {
071: width = i;
072: }
073:
074: public int getHeight() {
075: return height;
076: }
077:
078: public void setHeight(int i) {
079: height = i;
080: }
081:
082: public String toString() {
083: return "XYLayout [width = " + width + ", height = " + height
084: + "]";
085: }
086:
087: public void addLayoutComponent(String s, Component component) {
088: }
089:
090: public void removeLayoutComponent(Component component) {
091: info.remove(component);
092: }
093:
094: public Dimension preferredLayoutSize(Container container) {
095: return getLayoutSize(container, true);
096: }
097:
098: public Dimension minimumLayoutSize(Container container) {
099: return getLayoutSize(container, false);
100: }
101:
102: public void layoutContainer(Container container) {
103: Insets insets = container.getInsets();
104: int i = container.getComponentCount();
105:
106: for (int j = 0; j < i; j++) {
107: Component component = container.getComponent(j);
108: if (component.isVisible()) {
109: Rectangle rectangle = getComponentBounds(component,
110: true);
111: component.setBounds(insets.left + rectangle.x,
112: insets.top + rectangle.y, rectangle.width,
113: rectangle.height);
114: }
115: }
116: }
117:
118: /*
119: public void addLayoutComponent(Component component, Object obj) {
120: if(obj instanceof XYConstraints)
121: info.put(component, obj);
122: }
123: */
124:
125: public void addLayoutComponent(Component component, Object obj) {
126: if (obj instanceof XYConstraints)
127: info.put(component, getComponentBounds(component,
128: (XYConstraints) obj, true));
129: }
130:
131: public Dimension maximumLayoutSize(Container container) {
132: return new Dimension(0x7fffffff, 0x7fffffff);
133: }
134:
135: public float getLayoutAlignmentX(Container container) {
136: return 0.5F;
137: }
138:
139: public float getLayoutAlignmentY(Container container) {
140: return 0.5F;
141: }
142:
143: public void invalidateLayout(Container container) {
144: }
145:
146: Rectangle getComponentBounds(Component component,
147: XYConstraints xyconstraints, boolean flag) {
148:
149: if (xyconstraints == null) {
150:
151: if (defaultConstraints == null)
152: defaultConstraints = new XYConstraints();
153:
154: xyconstraints = defaultConstraints;
155:
156: }
157:
158: Rectangle rectangle = new Rectangle(xyconstraints.x,
159: xyconstraints.y, xyconstraints.width,
160: xyconstraints.height);
161:
162: if (rectangle.width <= 0 || rectangle.height <= 0) {
163: Dimension dimension = flag ? component.getPreferredSize()
164: : component.getMinimumSize();
165:
166: if (rectangle.width <= 0)
167: rectangle.width = dimension.width;
168: if (rectangle.height <= 0)
169: rectangle.height = dimension.height;
170:
171: }
172:
173: return rectangle;
174: }
175:
176: Rectangle getComponentBounds(Component component, boolean flag) {
177: // XYConstraints xyconstraints = (XYConstraints)info.get(component);
178: Rectangle rectangle = (Rectangle) info.get(component);
179: // if(xyconstraints == null)
180: // xyconstraints = defaultConstraints;
181:
182: // Rectangle rectangle = new Rectangle(xyconstraints.x, xyconstraints.y,
183: // xyconstraints.width, xyconstraints.height);
184:
185: if (rectangle.width <= 0 || rectangle.height <= 0) {
186: Dimension dimension = flag ? component.getPreferredSize()
187: : component.getMinimumSize();
188:
189: if (rectangle.width <= 0)
190: rectangle.width = dimension.width;
191: if (rectangle.height <= 0)
192: rectangle.height = dimension.height;
193:
194: }
195:
196: return rectangle;
197: }
198:
199: Dimension getLayoutSize(Container container, boolean flag) {
200: Dimension dimension = new Dimension(0, 0);
201:
202: if (width <= 0 || height <= 0) {
203:
204: int i = container.getComponentCount();
205:
206: for (int j = 0; j < i; j++) {
207: Component component = container.getComponent(j);
208:
209: if (component.isVisible()) {
210: Rectangle rectangle = getComponentBounds(component,
211: flag);
212: dimension.width = Math.max(dimension.width,
213: rectangle.x + rectangle.width);
214: dimension.height = Math.max(dimension.height,
215: rectangle.y + rectangle.height);
216: }
217:
218: }
219:
220: }
221:
222: if (width > 0)
223: dimension.width = width;
224:
225: if (height > 0)
226: dimension.height = height;
227:
228: Insets insets = container.getInsets();
229: dimension.width += insets.left + insets.right;
230: dimension.height += insets.top + insets.bottom;
231:
232: return dimension;
233: }
234:
235: } // class
|