001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.awtextra;
043:
044: import java.awt.*;
045:
046: /** AbsoluteLayout is a LayoutManager that works as a replacement for "null" layout to
047: * allow placement of components in absolute positions.
048: *
049: * @see AbsoluteConstraints
050: * @version 1.01, Aug 19, 1998
051: */
052: public class AbsoluteLayout implements LayoutManager2,
053: java.io.Serializable {
054: /** generated Serialized Version UID */
055: static final long serialVersionUID = -1919857869177070440L;
056:
057: /** Adds the specified component with the specified name to
058: * the layout.
059: * @param name the component name
060: * @param comp the component to be added
061: */
062: public void addLayoutComponent(String name, Component comp) {
063: throw new IllegalArgumentException();
064: }
065:
066: /** Removes the specified component from the layout.
067: * @param comp the component to be removed
068: */
069: public void removeLayoutComponent(Component comp) {
070: constraints.remove(comp);
071: }
072:
073: /** Calculates the preferred dimension for the specified
074: * panel given the components in the specified parent container.
075: * @param parent the component to be laid out
076: *
077: * @see #minimumLayoutSize
078: */
079: public Dimension preferredLayoutSize(Container parent) {
080: int maxWidth = 0;
081: int maxHeight = 0;
082: for (java.util.Enumeration e = constraints.keys(); e
083: .hasMoreElements();) {
084: Component comp = (Component) e.nextElement();
085: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
086: .get(comp);
087: Dimension size = comp.getPreferredSize();
088:
089: int width = ac.getWidth();
090: if (width == -1)
091: width = size.width;
092: int height = ac.getHeight();
093: if (height == -1)
094: height = size.height;
095:
096: if (ac.x + width > maxWidth)
097: maxWidth = ac.x + width;
098: if (ac.y + height > maxHeight)
099: maxHeight = ac.y + height;
100: }
101: return new Dimension(maxWidth, maxHeight);
102: }
103:
104: /** Calculates the minimum dimension for the specified
105: * panel given the components in the specified parent container.
106: * @param parent the component to be laid out
107: * @see #preferredLayoutSize
108: */
109: public Dimension minimumLayoutSize(Container parent) {
110: int maxWidth = 0;
111: int maxHeight = 0;
112: for (java.util.Enumeration e = constraints.keys(); e
113: .hasMoreElements();) {
114: Component comp = (Component) e.nextElement();
115: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
116: .get(comp);
117:
118: Dimension size = comp.getMinimumSize();
119:
120: int width = ac.getWidth();
121: if (width == -1)
122: width = size.width;
123: int height = ac.getHeight();
124: if (height == -1)
125: height = size.height;
126:
127: if (ac.x + width > maxWidth)
128: maxWidth = ac.x + width;
129: if (ac.y + height > maxHeight)
130: maxHeight = ac.y + height;
131: }
132: return new Dimension(maxWidth, maxHeight);
133: }
134:
135: /** Lays out the container in the specified panel.
136: * @param parent the component which needs to be laid out
137: */
138: public void layoutContainer(Container parent) {
139: for (java.util.Enumeration e = constraints.keys(); e
140: .hasMoreElements();) {
141: Component comp = (Component) e.nextElement();
142: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
143: .get(comp);
144: Dimension size = comp.getPreferredSize();
145: int width = ac.getWidth();
146: if (width == -1)
147: width = size.width;
148: int height = ac.getHeight();
149: if (height == -1)
150: height = size.height;
151:
152: comp.setBounds(ac.x, ac.y, width, height);
153: }
154: }
155:
156: /** Adds the specified component to the layout, using the specified
157: * constraint object.
158: * @param comp the component to be added
159: * @param constr where/how the component is added to the layout.
160: */
161: public void addLayoutComponent(Component comp, Object constr) {
162: if (!(constr instanceof AbsoluteConstraints))
163: throw new IllegalArgumentException();
164: constraints.put(comp, constr);
165: }
166:
167: /** Returns the maximum size of this component.
168: * @see java.awt.Component#getMinimumSize()
169: * @see java.awt.Component#getPreferredSize()
170: * @see LayoutManager
171: */
172: public Dimension maximumLayoutSize(Container target) {
173: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
174: }
175:
176: /** Returns the alignment along the x axis. This specifies how
177: * the component would like to be aligned relative to other
178: * components. The value should be a number between 0 and 1
179: * where 0 represents alignment along the origin, 1 is aligned
180: * the furthest away from the origin, 0.5 is centered, etc.
181: */
182: public float getLayoutAlignmentX(Container target) {
183: return 0;
184: }
185:
186: /** Returns the alignment along the y axis. This specifies how
187: * the component would like to be aligned relative to other
188: * components. The value should be a number between 0 and 1
189: * where 0 represents alignment along the origin, 1 is aligned
190: * the furthest away from the origin, 0.5 is centered, etc.
191: */
192: public float getLayoutAlignmentY(Container target) {
193: return 0;
194: }
195:
196: /** Invalidates the layout, indicating that if the layout manager
197: * has cached information it should be discarded.
198: */
199: public void invalidateLayout(Container target) {
200: }
201:
202: /** A mapping <Component, AbsoluteConstraints> */
203: protected java.util.Hashtable constraints = new java.util.Hashtable();
204: }
|