KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > swing > layout > XYLayout


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