KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > wizard > XYLayout


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