001: //$Id: AbsoluteLayout.java,v 1.2 2002/07/29 11:30:23 per_nyfelt Exp $
002: //from the NetBeans redist directory
003:
004: package org.ozoneDB.core.monitor;
005:
006: import java.awt.*;
007:
008: /**
009: * AbsoluteLayout is a LayoutManager that works as a replacement
010: * for "null" layout to allow placement of components in absolute
011: * positions.
012: *
013: *
014: * @version 1.01, Aug 19, 1998 ($Revision: 1.2 $)
015: * @author Ian Formanek (modified softwarebuero m&b
016: */
017: public class AbsoluteLayout implements LayoutManager2,
018: java.io.Serializable {
019:
020: /** A mapping <Component, AbsoluteConstraints> */
021: protected java.util.Hashtable constraints = new java.util.Hashtable();
022:
023: /** urspruengliche groesse des umgebenden containers*/
024: public Dimension bounds;
025:
026: /** generated Serialized Version UID */
027: final static long serialVersionUID = -1919857869177070440L;
028:
029: /**
030: * Adds the specified component with the specified name to
031: * the layout.
032: * @param name the component name
033: * @param comp the component to be added
034: */
035: public void addLayoutComponent(String name, Component comp) {
036: throw new IllegalArgumentException();
037: }
038:
039: /**
040: * Removes the specified component from the layout.
041: * @param comp the component to be removed
042: */
043: public void removeLayoutComponent(Component comp) {
044: constraints.remove(comp);
045: }
046:
047: /**
048: * Calculates the preferred dimension for the specified
049: * panel given the components in the specified parent container.
050: * @param parent the component to be laid out
051: *
052: * @see #minimumLayoutSize
053: */
054: public Dimension preferredLayoutSize(Container parent) {
055: int maxWidth = 0;
056: int maxHeight = 0;
057: for (java.util.Enumeration e = constraints.keys(); e
058: .hasMoreElements();) {
059: Component comp = (Component) e.nextElement();
060: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
061: .get(comp);
062: Dimension size = comp.getPreferredSize();
063:
064: int width = ac.getWidth() == -1 ? size.width : ac
065: .getWidth();
066: int height = ac.getHeight() == -1 ? size.height : ac
067: .getHeight();
068:
069: if (ac.x + width > maxWidth) {
070: maxWidth = ac.x + width;
071: }
072: if (ac.y + height > maxHeight) {
073: maxHeight = ac.y + height;
074: }
075: }
076: return new Dimension(maxWidth, maxHeight);
077: }
078:
079: /**
080: * Calculates the minimum dimension for the specified
081: * panel given the components in the specified parent container.
082: * @param parent the component to be laid out
083: * @see #preferredLayoutSize
084: */
085: public Dimension minimumLayoutSize(Container parent) {
086: int maxWidth = 0;
087: int maxHeight = 0;
088: for (java.util.Enumeration e = constraints.keys(); e
089: .hasMoreElements();) {
090: Component comp = (Component) e.nextElement();
091: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
092: .get(comp);
093:
094: Dimension size = comp.getMinimumSize();
095:
096: int width = ac.getWidth() == -1 ? size.width : ac
097: .getWidth();
098: int height = ac.getHeight() == -1 ? size.height : ac
099: .getHeight();
100:
101: if (ac.x + width > maxWidth) {
102: maxWidth = ac.x + width;
103: }
104: if (ac.y + height > maxHeight) {
105: maxHeight = ac.y + height;
106: }
107: }
108: return new Dimension(maxWidth, maxHeight);
109: }
110:
111: /**
112: * Sets the original size of the parent container. Needs not
113: * to be called in any case.
114: */
115: public void realizeSize(Dimension bounds) {
116: bounds = new Dimension(bounds);
117: }
118:
119: /**
120: * Lays out the container in the specified panel.
121: * @param parent the component which needs to be laid out
122: */
123: public void layoutContainer(Container parent) {
124: Dimension parentSize = parent.getSize();
125: if (bounds == null) {
126: bounds = new Dimension(parentSize);
127: }
128:
129: for (java.util.Enumeration e = constraints.keys(); e
130: .hasMoreElements();) {
131: Component comp = (Component) e.nextElement();
132: AbsoluteConstraints ac = (AbsoluteConstraints) constraints
133: .get(comp);
134: Dimension size = comp.getPreferredSize();
135:
136: int x = ac.x;
137: int y = ac.y;
138: int width = ac.getWidth() == -1 ? size.width : ac
139: .getWidth();
140: int height = ac.getHeight() == -1 ? size.height : ac
141: .getHeight();
142:
143: if ((ac.policy & AbsoluteConstraints.X_PROP) != 0) {
144: x = (int) ((double) ac.x / (double) bounds.width * (double) parentSize.width);
145: }
146: if ((ac.policy & AbsoluteConstraints.Y_PROP) != 0) {
147: y = (int) ((double) ac.y / (double) bounds.height * (double) parentSize.height);
148: }
149: if ((ac.policy & AbsoluteConstraints.X_ABS) != 0) {
150: x = (parentSize.width - (bounds.width - ac.x));
151: }
152: if ((ac.policy & AbsoluteConstraints.Y_ABS) != 0) {
153: y = (parentSize.height - (bounds.height - ac.y));
154: }
155:
156: if ((ac.policy & AbsoluteConstraints.X2_PROP) != 0) {
157: width = ((int) ((double) (ac.x + width)
158: / (double) bounds.width * (double) parentSize.width) - x);
159: }
160: if ((ac.policy & AbsoluteConstraints.Y2_PROP) != 0) {
161: height = ((int) ((double) (ac.y + height)
162: / (double) bounds.height * (double) parentSize.height) - y);
163: }
164: if ((ac.policy & AbsoluteConstraints.X2_ABS) != 0) {
165: int x2 = bounds.width - ac.x + width;
166: width = (parentSize.width - x - x2);
167: }
168: if ((ac.policy & AbsoluteConstraints.Y2_ABS) != 0) {
169: int y2 = bounds.height - ac.y + height;
170: height = (parentSize.height - y - y2);
171: }
172:
173: comp.setBounds(x, y, width, height);
174: }
175: }
176:
177: /**
178: * Adds the specified component to the layout, using the specified
179: * constraint object.
180: * @param comp the component to be added
181: * @param constr where/how the component is added to the layout.
182: */
183: public void addLayoutComponent(Component comp, Object constr) {
184: if (!(constr instanceof AbsoluteConstraints)) {
185: throw new IllegalArgumentException();
186: }
187: constraints.put(comp, constr);
188: }
189:
190: /**
191: * Returns the maximum size of this component.
192: * @see java.awt.Component#getMinimumSize()
193: * @see java.awt.Component#getPreferredSize()
194: * @see LayoutManager
195: */
196: public Dimension maximumLayoutSize(Container target) {
197: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
198: }
199:
200: /**
201: * Returns the alignment along the x axis. This specifies how
202: * the component would like to be aligned relative to other
203: * components. The value should be a number between 0 and 1
204: * where 0 represents alignment along the origin, 1 is aligned
205: * the furthest away from the origin, 0.5 is centered, etc.
206: */
207: public float getLayoutAlignmentX(Container target) {
208: return 0;
209: }
210:
211: /**
212: * Returns the alignment along the y axis. This specifies how
213: * the component would like to be aligned relative to other
214: * components. The value should be a number between 0 and 1
215: * where 0 represents alignment along the origin, 1 is aligned
216: * the furthest away from the origin, 0.5 is centered, etc.
217: */
218: public float getLayoutAlignmentY(Container target) {
219: return 0;
220: }
221:
222: /**
223: * Invalidates the layout, indicating that if the layout manager
224: * has cached information it should be discarded.
225: */
226: public void invalidateLayout(Container target) {
227: }
228: }
|