KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > dods > XYLayout


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.dods;
24
25 import java.awt.*;
26 import java.io.Serializable JavaDoc;
27 import java.util.Hashtable JavaDoc;
28
29
30
31 public class XYLayout
32     implements LayoutManager2, Serializable JavaDoc
33 {
34
35     private static final long serialVersionUID = 200L;
36     int width;
37     int height;
38     Hashtable JavaDoc info;
39     static final XYConstraints defaultConstraints = new XYConstraints();
40
41     public XYLayout()
42     {
43         info = new Hashtable JavaDoc();
44     }
45
46     public XYLayout(int width, int height)
47     {
48         info = new Hashtable JavaDoc();
49         this.width = width;
50         this.height = height;
51     }
52
53     public int getWidth()
54     {
55         return width;
56     }
57
58     public void setWidth(int width)
59     {
60         this.width = width;
61     }
62
63     public int getHeight()
64     {
65         return height;
66     }
67
68     public void setHeight(int height)
69     {
70         this.height = height;
71     }
72
73     public String JavaDoc toString()
74     {
75         return String.valueOf(String.valueOf((new StringBuffer JavaDoc("XYLayout[width=")).append(width).append(",height=").append(height).append("]")));
76     }
77
78     public void addLayoutComponent(String JavaDoc s, Component component1)
79     {
80     }
81
82     public void removeLayoutComponent(Component component)
83     {
84         info.remove(component);
85     }
86
87     public Dimension preferredLayoutSize(Container target)
88     {
89         return getLayoutSize(target, true);
90     }
91
92     public Dimension minimumLayoutSize(Container target)
93     {
94         return getLayoutSize(target, false);
95     }
96
97     public void layoutContainer(Container target)
98     {
99         Insets insets = target.getInsets();
100         int count = target.getComponentCount();
101         for(int i = 0; i < count; i++)
102         {
103             Component component = target.getComponent(i);
104             if(component.isVisible())
105             {
106                 Rectangle r = getComponentBounds(component, true);
107                 component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height);
108             }
109         }
110
111     }
112
113     public void addLayoutComponent(Component component, Object JavaDoc constraints)
114     {
115         if(constraints instanceof XYConstraints)
116             info.put(component, constraints);
117     }
118
119     public Dimension maximumLayoutSize(Container target)
120     {
121         return new Dimension(0x7fffffff, 0x7fffffff);
122     }
123
124     public float getLayoutAlignmentX(Container target)
125     {
126         return 0.5F;
127     }
128
129     public float getLayoutAlignmentY(Container target)
130     {
131         return 0.5F;
132     }
133
134     public void invalidateLayout(Container container)
135     {
136     }
137
138     Rectangle getComponentBounds(Component component, boolean doPreferred)
139     {
140         XYConstraints constraints = (XYConstraints)info.get(component);
141         if(constraints == null)
142             constraints = defaultConstraints;
143         Rectangle r = new Rectangle(constraints.x, constraints.y, constraints.width, constraints.height);
144         if(r.width <= 0 || r.height <= 0)
145         {
146             Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize();
147             if(r.width <= 0)
148                 r.width = d.width;
149             if(r.height <= 0)
150                 r.height = d.height;
151         }
152         return r;
153     }
154
155     Dimension getLayoutSize(Container target, boolean doPreferred)
156     {
157         Dimension dim = new Dimension(0, 0);
158         if(width <= 0 || height <= 0)
159         {
160             int count = target.getComponentCount();
161             for(int i = 0; i < count; i++)
162             {
163                 Component component = target.getComponent(i);
164                 if(component.isVisible())
165                 {
166                     Rectangle r = getComponentBounds(component, doPreferred);
167                     dim.width = Math.max(dim.width, r.x + r.width);
168                     dim.height = Math.max(dim.height, r.y + r.height);
169                 }
170             }
171
172         }
173         if(width > 0)
174             dim.width = width;
175         if(height > 0)
176             dim.height = height;
177         Insets insets = target.getInsets();
178         dim.width += insets.left + insets.right;
179         dim.height += insets.top + insets.bottom;
180         return dim;
181     }
182
183 }
184
Popular Tags