001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.lib.awtextra;
015:
016: import java.awt.*;
017:
018: /** AbsoluteLayout is a LayoutManager that works as a replacement for "null" layout to
019: * allow placement of components in absolute positions.
020: *
021: * @see AbsoluteConstraints
022: * @version 1.01, Aug 19, 1998
023: */
024: public class AbsoluteLayout implements LayoutManager2,
025: java.io.Serializable {
026: /** generated Serialized Version UID */
027: static final long serialVersionUID = -1919857869177070440L;
028:
029: /** Adds the specified component with the specified name to
030: * the layout.
031: * @param name the component name
032: * @param comp the component to be added
033: */
034: public void addLayoutComponent(String name, Component comp) {
035: throw new IllegalArgumentException();
036: }
037:
038: /** Removes the specified component from the layout.
039: * @param comp the component to be removed
040: */
041: public void removeLayoutComponent(Component comp) {
042: constraints.remove(comp);
043: }
044:
045: /** Calculates the preferred dimension for the specified
046: * panel given the components in the specified parent container.
047: * @param parent the component to be laid out
048: *
049: * @see #minimumLayoutSize
050: */
051: public Dimension preferredLayoutSize(Container parent) {
052: int maxWidth = 0;
053: int maxHeight = 0;
054: for (java.util.Enumeration e = constraints.keys(); e
055: .hasMoreElements();) {
056: Component comp = (Component) e.nextElement();
057: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
058: .get(comp);
059: Dimension size = comp.getPreferredSize();
060:
061: int width = ac.getWidth();
062: if (width == -1)
063: width = size.width;
064: int height = ac.getHeight();
065: if (height == -1)
066: height = size.height;
067:
068: if (ac.x + width > maxWidth)
069: maxWidth = ac.x + width;
070: if (ac.y + height > maxHeight)
071: maxHeight = ac.y + height;
072: }
073: return new Dimension(maxWidth, maxHeight);
074: }
075:
076: /** Calculates the minimum dimension for the specified
077: * panel given the components in the specified parent container.
078: * @param parent the component to be laid out
079: * @see #preferredLayoutSize
080: */
081: public Dimension minimumLayoutSize(Container parent) {
082: int maxWidth = 0;
083: int maxHeight = 0;
084: for (java.util.Enumeration e = constraints.keys(); e
085: .hasMoreElements();) {
086: Component comp = (Component) e.nextElement();
087: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
088: .get(comp);
089:
090: Dimension size = comp.getMinimumSize();
091:
092: int width = ac.getWidth();
093: if (width == -1)
094: width = size.width;
095: int height = ac.getHeight();
096: if (height == -1)
097: height = size.height;
098:
099: if (ac.x + width > maxWidth)
100: maxWidth = ac.x + width;
101: if (ac.y + height > maxHeight)
102: maxHeight = ac.y + height;
103: }
104: return new Dimension(maxWidth, maxHeight);
105: }
106:
107: /** Lays out the container in the specified panel.
108: * @param parent the component which needs to be laid out
109: */
110: public void layoutContainer(Container parent) {
111: for (java.util.Enumeration e = constraints.keys(); e
112: .hasMoreElements();) {
113: Component comp = (Component) e.nextElement();
114: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
115: .get(comp);
116: Dimension size = comp.getPreferredSize();
117: int width = ac.getWidth();
118: if (width == -1)
119: width = size.width;
120: int height = ac.getHeight();
121: if (height == -1)
122: height = size.height;
123:
124: comp.setBounds(ac.x, ac.y, width, height);
125: }
126: }
127:
128: /** Adds the specified component to the layout, using the specified
129: * constraint object.
130: * @param comp the component to be added
131: * @param constr where/how the component is added to the layout.
132: */
133: public void addLayoutComponent(Component comp, Object constr) {
134: if (!(constr instanceof AbsoluteConstraints))
135: throw new IllegalArgumentException();
136: constraints.put(comp, constr);
137: }
138:
139: /** Returns the maximum size of this component.
140: * @see java.awt.Component#getMinimumSize()
141: * @see java.awt.Component#getPreferredSize()
142: * @see LayoutManager
143: */
144: public Dimension maximumLayoutSize(Container target) {
145: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
146: }
147:
148: /** Returns the alignment along the x axis. This specifies how
149: * the component would like to be aligned relative to other
150: * components. The value should be a number between 0 and 1
151: * where 0 represents alignment along the origin, 1 is aligned
152: * the furthest away from the origin, 0.5 is centered, etc.
153: */
154: public float getLayoutAlignmentX(Container target) {
155: return 0;
156: }
157:
158: /** Returns the alignment along the y axis. This specifies how
159: * the component would like to be aligned relative to other
160: * components. The value should be a number between 0 and 1
161: * where 0 represents alignment along the origin, 1 is aligned
162: * the furthest away from the origin, 0.5 is centered, etc.
163: */
164: public float getLayoutAlignmentY(Container target) {
165: return 0;
166: }
167:
168: /** Invalidates the layout, indicating that if the layout manager
169: * has cached information it should be discarded.
170: */
171: public void invalidateLayout(Container target) {
172: }
173:
174: /** A mapping <Component, AbsoluteConstraints> */
175: protected java.util.Hashtable constraints = new java.util.Hashtable();
176: }
|