KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > awtextra > AbsoluteLayout


1 /*
2  * Sun Public License Notice
3  *
4  * The contents of this file are subject to the Sun Public License
5  * Version 1.0 (the "License"). You may not use this file except in
6  * compliance with the License. A copy of the License is available at
7  * http://www.sun.com/
8  *
9  * The Original Code is NetBeans. The Initial Developer of the Original
10  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
11  * Microsystems, Inc. All Rights Reserved.
12  */

13
14 package org.netbeans.lib.awtextra;
15
16 import java.awt.*;
17
18 /** AbsoluteLayout is a LayoutManager that works as a replacement for "null" layout to
19 * allow placement of components in absolute positions.
20 *
21 * @see AbsoluteConstraints
22 * @version 1.01, Aug 19, 1998
23 */

24 public class AbsoluteLayout implements LayoutManager2, java.io.Serializable JavaDoc {
25     /** generated Serialized Version UID */
26     static final long serialVersionUID = -1919857869177070440L;
27
28     /** Adds the specified component with the specified name to
29     * the layout.
30     * @param name the component name
31     * @param comp the component to be added
32     */

33     public void addLayoutComponent(String JavaDoc name, Component comp) {
34         throw new IllegalArgumentException JavaDoc();
35     }
36
37     /** Removes the specified component from the layout.
38     * @param comp the component to be removed
39     */

40     public void removeLayoutComponent(Component comp) {
41         constraints.remove(comp);
42     }
43
44     /** Calculates the preferred dimension for the specified
45     * panel given the components in the specified parent container.
46     * @param parent the component to be laid out
47     *
48     * @see #minimumLayoutSize
49     */

50     public Dimension preferredLayoutSize(Container parent) {
51         int maxWidth = 0;
52         int maxHeight = 0;
53         for (java.util.Enumeration JavaDoc e = constraints.keys(); e.hasMoreElements();) {
54             Component comp = (Component)e.nextElement();
55             AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
56             Dimension size = comp.getPreferredSize();
57
58             int width = ac.getWidth ();
59             if (width == -1) width = size.width;
60             int height = ac.getHeight ();
61             if (height == -1) height = size.height;
62
63             if (ac.x + width > maxWidth)
64                 maxWidth = ac.x + width;
65             if (ac.y + height > maxHeight)
66                 maxHeight = ac.y + height;
67         }
68         return new Dimension (maxWidth, maxHeight);
69     }
70
71     /** Calculates the minimum dimension for the specified
72     * panel given the components in the specified parent container.
73     * @param parent the component to be laid out
74     * @see #preferredLayoutSize
75     */

76     public Dimension minimumLayoutSize(Container parent) {
77         int maxWidth = 0;
78         int maxHeight = 0;
79         for (java.util.Enumeration JavaDoc e = constraints.keys(); e.hasMoreElements();) {
80             Component comp = (Component)e.nextElement();
81             AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
82
83             Dimension size = comp.getMinimumSize();
84
85             int width = ac.getWidth ();
86             if (width == -1) width = size.width;
87             int height = ac.getHeight ();
88             if (height == -1) height = size.height;
89
90             if (ac.x + width > maxWidth)
91                 maxWidth = ac.x + width;
92             if (ac.y + height > maxHeight)
93                 maxHeight = ac.y + height;
94         }
95         return new Dimension (maxWidth, maxHeight);
96     }
97
98     /** Lays out the container in the specified panel.
99     * @param parent the component which needs to be laid out
100     */

101     public void layoutContainer(Container parent) {
102         for (java.util.Enumeration JavaDoc e = constraints.keys(); e.hasMoreElements();) {
103             Component comp = (Component)e.nextElement();
104             AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
105             Dimension size = comp.getPreferredSize();
106             int width = ac.getWidth ();
107             if (width == -1) width = size.width;
108             int height = ac.getHeight ();
109             if (height == -1) height = size.height;
110
111             comp.setBounds(ac.x, ac.y, width, height);
112         }
113     }
114
115     /** Adds the specified component to the layout, using the specified
116     * constraint object.
117     * @param comp the component to be added
118     * @param constr where/how the component is added to the layout.
119     */

120     public void addLayoutComponent(Component comp, Object JavaDoc constr) {
121         if (!(constr instanceof AbsoluteConstraints))
122             throw new IllegalArgumentException JavaDoc();
123         constraints.put(comp, constr);
124     }
125
126     /** Returns the maximum size of this component.
127     * @see java.awt.Component#getMinimumSize()
128     * @see java.awt.Component#getPreferredSize()
129     * @see LayoutManager
130     */

131     public Dimension maximumLayoutSize(Container target) {
132         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
133     }
134
135     /** Returns the alignment along the x axis. This specifies how
136     * the component would like to be aligned relative to other
137     * components. The value should be a number between 0 and 1
138     * where 0 represents alignment along the origin, 1 is aligned
139     * the furthest away from the origin, 0.5 is centered, etc.
140     */

141     public float getLayoutAlignmentX(Container target) {
142         return 0;
143     }
144
145     /** Returns the alignment along the y axis. This specifies how
146     * the component would like to be aligned relative to other
147     * components. The value should be a number between 0 and 1
148     * where 0 represents alignment along the origin, 1 is aligned
149     * the furthest away from the origin, 0.5 is centered, etc.
150     */

151     public float getLayoutAlignmentY(Container target) {
152         return 0;
153     }
154
155     /** Invalidates the layout, indicating that if the layout manager
156     * has cached information it should be discarded.
157     */

158     public void invalidateLayout(Container target) {
159     }
160
161
162     /** A mapping <Component, AbsoluteConstraints> */
163     protected java.util.Hashtable JavaDoc constraints = new java.util.Hashtable JavaDoc();
164 }
165
166
Popular Tags