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 jagafa.util.view;
015    
016    import java.awt.Component;
017    import java.awt.Container;
018    import java.awt.Dimension;
019    import java.awt.LayoutManager;
020    import java.awt.LayoutManager2;
021    
022    /**
023     * AbsoluteLayout is a LayoutManager that works as a replacement for "null"
024     * layout to allow placement of components in absolute positions.
025     * 
026     * @see AbsConstr
027     * @version 1.01, Aug 19, 1998
028     */
029    public class AbsoluteLayout implements LayoutManager2, java.io.Serializable {
030            /** generated Serialized Version UID */
031            static final long serialVersionUID = -1919857869177070440L;
032    
033            /**
034             * Adds the specified component with the specified name to the layout.
035             * 
036             * @param name
037             *            the component name
038             * @param comp
039             *            the component to be added
040             */
041            public void addLayoutComponent(String name, Component comp) {
042                    throw new IllegalArgumentException();
043            }
044    
045            /**
046             * Removes the specified component from the layout.
047             * 
048             * @param comp
049             *            the component to be removed
050             */
051            public void removeLayoutComponent(Component comp) {
052                    constraints.remove(comp);
053            }
054    
055            /**
056             * Calculates the preferred dimension for the specified panel given the
057             * components in the specified parent container.
058             * 
059             * @param parent
060             *            the component to be laid out
061             * 
062             * @see #minimumLayoutSize
063             */
064            public Dimension preferredLayoutSize(Container parent) {
065                    int maxWidth = 0;
066                    int maxHeight = 0;
067                    for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
068                            Component comp = (Component) e.nextElement();
069                            AbsConstr ac = (AbsConstr) constraints
070                                            .get(comp);
071                            Dimension size = comp.getPreferredSize();
072    
073                            int width = ac.getWidth();
074                            if (width == -1)
075                                    width = size.width;
076                            int height = ac.getHeight();
077                            if (height == -1)
078                                    height = size.height;
079    
080                            if (ac.x + width > maxWidth)
081                                    maxWidth = ac.x + width;
082                            if (ac.y + height > maxHeight)
083                                    maxHeight = ac.y + height;
084                    }
085                    return new Dimension(maxWidth, maxHeight);
086            }
087    
088            /**
089             * Calculates the minimum dimension for the specified panel given the
090             * components in the specified parent container.
091             * 
092             * @param parent
093             *            the component to be laid out
094             * @see #preferredLayoutSize
095             */
096            public Dimension minimumLayoutSize(Container parent) {
097                    int maxWidth = 0;
098                    int maxHeight = 0;
099                    for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
100                            Component comp = (Component) e.nextElement();
101                            AbsConstr ac = (AbsConstr) constraints
102                                            .get(comp);
103    
104                            Dimension size = comp.getMinimumSize();
105    
106                            int width = ac.getWidth();
107                            if (width == -1)
108                                    width = size.width;
109                            int height = ac.getHeight();
110                            if (height == -1)
111                                    height = size.height;
112    
113                            if (ac.x + width > maxWidth)
114                                    maxWidth = ac.x + width;
115                            if (ac.y + height > maxHeight)
116                                    maxHeight = ac.y + height;
117                    }
118                    return new Dimension(maxWidth, maxHeight);
119            }
120    
121            /**
122             * Lays out the container in the specified panel.
123             * 
124             * @param parent
125             *            the component which needs to be laid out
126             */
127            public void layoutContainer(Container parent) {
128                    for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
129                            Component comp = (Component) e.nextElement();
130                            AbsConstr ac = (AbsConstr) constraints
131                                            .get(comp);
132                            Dimension size = comp.getPreferredSize();
133                            int width = ac.getWidth();
134                            if (width == -1)
135                                    width = size.width;
136                            int height = ac.getHeight();
137                            if (height == -1)
138                                    height = size.height;
139    
140                            comp.setBounds(ac.x, ac.y, width, height);
141                    }
142            }
143    
144            /**
145             * Adds the specified component to the layout, using the specified
146             * constraint object.
147             * 
148             * @param comp
149             *            the component to be added
150             * @param constr
151             *            where/how the component is added to the layout.
152             */
153            public void addLayoutComponent(Component comp, Object constr) {
154                    if (!(constr instanceof AbsConstr))
155                            throw new IllegalArgumentException();
156                    constraints.put(comp, constr);
157            }
158    
159            /**
160             * Returns the maximum size of this component.
161             * 
162             * @see java.awt.Component#getMinimumSize()
163             * @see java.awt.Component#getPreferredSize()
164             * @see LayoutManager
165             */
166            public Dimension maximumLayoutSize(Container target) {
167                    return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
168            }
169    
170            /**
171             * Returns the alignment along the x axis. This specifies how the component
172             * would like to be aligned relative to other components. The value should
173             * be a number between 0 and 1 where 0 represents alignment along the
174             * origin, 1 is aligned the furthest away from the origin, 0.5 is centered,
175             * etc.
176             */
177            public float getLayoutAlignmentX(Container target) {
178                    return 0;
179            }
180    
181            /**
182             * Returns the alignment along the y axis. This specifies how the component
183             * would like to be aligned relative to other components. The value should
184             * be a number between 0 and 1 where 0 represents alignment along the
185             * origin, 1 is aligned the furthest away from the origin, 0.5 is centered,
186             * etc.
187             */
188            public float getLayoutAlignmentY(Container target) {
189                    return 0;
190            }
191    
192            /**
193             * Invalidates the layout, indicating that if the layout manager has cached
194             * information it should be discarded.
195             */
196            public void invalidateLayout(Container target) {
197            }
198    
199            /** A mapping <Component, AbsoluteConstraints> */
200            protected java.util.Hashtable<Object,Object> constraints = new java.util.Hashtable<Object,Object>();
201    }