KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > monitor > AbsoluteLayout


1 //$Id: AbsoluteLayout.java,v 1.3 2002/08/27 08:32:26 per_nyfelt Exp $
2
//from the NetBeans redist directory
3

4 package org.ozoneDB.core.monitor;
5
6 import java.awt.*;
7
8 /**
9  * AbsoluteLayout is a LayoutManager that works as a replacement
10  * for "null" layout to allow placement of components in absolute
11  * positions.
12  *
13  *
14  * @version 1.01, Aug 19, 1998 ($Revision: 1.3 $)
15  * @author Ian Formanek (modified softwarebuero m&b
16  */

17 public class AbsoluteLayout implements LayoutManager2, java.io.Serializable JavaDoc {
18
19     /** A mapping <Component, AbsoluteConstraints> */
20     protected java.util.Hashtable JavaDoc constraints = new java.util.Hashtable JavaDoc();
21
22     /** urspruengliche groesse des umgebenden containers*/
23     public Dimension bounds;
24
25     /** generated Serialized Version UID */
26     final static long serialVersionUID = -1919857869177070440L;
27
28
29     /**
30      * Adds the specified component with the specified name to
31      * the layout.
32      * @param name the component name
33      * @param comp the component to be added
34      */

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

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

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

84     public Dimension minimumLayoutSize( Container parent ) {
85         int maxWidth = 0;
86         int maxHeight = 0;
87         for (java.util.Enumeration JavaDoc e = constraints.keys(); e.hasMoreElements();) {
88             Component comp = (Component)e.nextElement();
89             AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get( comp );
90
91             Dimension size = comp.getMinimumSize();
92
93             int width = ac.getWidth() == -1 ? size.width : ac.getWidth();
94             int height = ac.getHeight() == -1 ? size.height : ac.getHeight();
95
96             if (ac.x + width > maxWidth) {
97                 maxWidth = ac.x + width;
98             }
99             if (ac.y + height > maxHeight) {
100                 maxHeight = ac.y + height;
101             }
102         }
103         return new Dimension( maxWidth, maxHeight );
104     }
105
106
107     /**
108      * Sets the original size of the parent container. Needs not
109      * to be called in any case.
110      */

111     public void realizeSize( Dimension bounds ) {
112         bounds = new Dimension( bounds );
113     }
114
115
116     /**
117      * Lays out the container in the specified panel.
118      * @param parent the component which needs to be laid out
119      */

120     public void layoutContainer( Container parent ) {
121         Dimension parentSize = parent.getSize();
122         if (bounds == null) {
123             bounds = new Dimension( parentSize );
124         }
125
126         for (java.util.Enumeration JavaDoc e = constraints.keys(); e.hasMoreElements();) {
127             Component comp = (Component)e.nextElement();
128             AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get( comp );
129             Dimension size = comp.getPreferredSize();
130
131             int x = ac.x;
132             int y = ac.y;
133             int width = ac.getWidth() == -1 ? size.width : ac.getWidth();
134             int height = ac.getHeight() == -1 ? size.height : ac.getHeight();
135
136             if ((ac.policy & AbsoluteConstraints.X_PROP) != 0) {
137                 x = (int)((double)ac.x / (double)bounds.width * (double)parentSize.width);
138             }
139             if ((ac.policy & AbsoluteConstraints.Y_PROP) != 0) {
140                 y = (int)((double)ac.y / (double)bounds.height * (double)parentSize.height);
141             }
142             if ((ac.policy & AbsoluteConstraints.X_ABS) != 0) {
143                 x = (parentSize.width - (bounds.width - ac.x));
144             }
145             if ((ac.policy & AbsoluteConstraints.Y_ABS) != 0) {
146                 y = (parentSize.height - (bounds.height - ac.y));
147             }
148
149             if ((ac.policy & AbsoluteConstraints.X2_PROP) != 0) {
150                 width = ((int)((double)(ac.x + width) / (double)bounds.width * (double)parentSize.width) - x);
151             }
152             if ((ac.policy & AbsoluteConstraints.Y2_PROP) != 0) {
153                 height = ((int)((double)(ac.y + height) / (double)bounds.height * (double)parentSize.height) - y);
154             }
155             if ((ac.policy & AbsoluteConstraints.X2_ABS) != 0) {
156                 int x2 = bounds.width - ac.x + width;
157                 width = (parentSize.width - x - x2);
158             }
159             if ((ac.policy & AbsoluteConstraints.Y2_ABS) != 0) {
160                 int y2 = bounds.height - ac.y + height;
161                 height = (parentSize.height - y - y2);
162             }
163
164             comp.setBounds( x, y, width, height );
165         }
166     }
167
168
169     /**
170      * Adds the specified component to the layout, using the specified
171      * constraint object.
172      * @param comp the component to be added
173      * @param constr where/how the component is added to the layout.
174      */

175     public void addLayoutComponent( Component comp, Object JavaDoc constr ) {
176         if (!(constr instanceof AbsoluteConstraints)) {
177             throw new IllegalArgumentException JavaDoc();
178         }
179         constraints.put( comp, constr );
180     }
181
182
183     /**
184      * Returns the maximum size of this component.
185      * @see java.awt.Component#getMinimumSize()
186      * @see java.awt.Component#getPreferredSize()
187      * @see LayoutManager
188      */

189     public Dimension maximumLayoutSize( Container target ) {
190         return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
191     }
192
193
194     /**
195      * Returns the alignment along the x axis. This specifies how
196      * the component would like to be aligned relative to other
197      * components. The value should be a number between 0 and 1
198      * where 0 represents alignment along the origin, 1 is aligned
199      * the furthest away from the origin, 0.5 is centered, etc.
200      */

201     public float getLayoutAlignmentX( Container target ) {
202         return 0;
203     }
204
205
206     /**
207      * Returns the alignment along the y axis. This specifies how
208      * the component would like to be aligned relative to other
209      * components. The value should be a number between 0 and 1
210      * where 0 represents alignment along the origin, 1 is aligned
211      * the furthest away from the origin, 0.5 is centered, etc.
212      */

213     public float getLayoutAlignmentY( Container target ) {
214         return 0;
215     }
216
217
218     /**
219      * Invalidates the layout, indicating that if the layout manager
220      * has cached information it should be discarded.
221      */

222     public void invalidateLayout( Container target ) {
223     }
224 }
225
Popular Tags