1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.layouts;
17
18 import java.awt.Component;
19 import java.awt.Container;
20 import java.awt.Dimension;
21 import java.awt.Insets;
22 import java.awt.LayoutManager2;
23 import java.awt.Point;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.xulux.api.gui.IWidget;
28
29
30
31
32
33
34
35
36
37 public class AutoSizeLayoutManager extends SwingLayoutAbstract implements LayoutManager2 {
38
39
40
41
42
43 private Map map;
44
45 public AutoSizeLayoutManager() {
46 map = new HashMap();
47 }
48
49
50
51
52 public AutoSizeLayoutManager(IWidget parent) {
53 this();
54 setParent(parent);
55 }
56
57
58
59
60 public float getLayoutAlignmentX(Container target) {
61 return 0;
62 }
63
64
65
66
67 public float getLayoutAlignmentY(Container target) {
68 return 0;
69 }
70
71
72
73
74 public void invalidateLayout(Container target) {
75 }
76
77
78
79
80 public Dimension maximumLayoutSize(Container target) {
81 return null;
82 }
83
84
85
86
87 public void addLayoutComponent(Component comp, Object constraints) {
88 map.put(comp, constraints);
89 }
90
91
92
93
94 public void removeLayoutComponent(Component comp) {
95 }
96
97
98
99
100 public void layoutContainer(Container parent) {
101
102 int compCount = parent.getComponentCount();
103 Insets insets = parent.getInsets();
104 for (int i = 0; i < compCount; i++) {
105 Component component = parent.getComponent(i);
106 IWidget widget = null;
107 Object obj = map.get(component);
108 if (obj instanceof IWidget) {
109 widget = (IWidget) obj;
110 }
111 Dimension prefSize = null;
112 Point location = new Point(0,0);
113 if (widget != null) {
114 prefSize = widget.getRectangle().getRectangle().getSize();
115 location = widget.getRectangle().getRectangle().getLocation();
116 } else {
117 prefSize = component.getPreferredSize();
118 if (obj instanceof String) {
119 String newValue = ((String) obj).toLowerCase();
120 if (newValue.indexOf("width=auto") != -1) {
121 prefSize.width = parent.getSize().width;
122 }
123 if (newValue.indexOf("height=auto") != -1) {
124 prefSize.height = parent.getSize().height;
125 }
126 }
127 }
128 int height = prefSize.height+insets.top+insets.bottom;
129 int width = prefSize.width+insets.left;
130 if (widget != null && widget.getProperty("width") != null) {
131 String widthProp = widget.getProperty("width");
132 if ("auto".equalsIgnoreCase(widthProp)) {
133 int newX = parent.getSize().width;
134 width = newX;
135 }
136 String heightProp = widget.getProperty("height");
137 if ("auto".equalsIgnoreCase(heightProp)) {
138 height = parent.getSize().height-location.y;
139 }
140 }
141 component.setBounds(location.x, location.y, width, height);
142 }
143 }
144
145
146
147
148 public void addLayoutComponent(String name, Component comp) {
149 }
150
151
152
153
154 public Dimension minimumLayoutSize(Container parent) {
155 return null;
156 }
157
158
159
160
161 public Dimension preferredLayoutSize(Container parent) {
162
163 int compCount = parent.getComponentCount();
164 Insets insets = parent.getInsets();
165 int width = insets.left;
166 int height = insets.top=insets.bottom;
167 for (int i = 0; i < compCount; i++) {
168 Component component = parent.getComponent(i);
169 Object obj = map.get(component);
170 IWidget widget = null;
171 if (obj instanceof IWidget) {
172 widget = (IWidget) obj;
173 }
174 Dimension prefSize = null;
175 if (widget != null) {
176 prefSize = widget.getRectangle().getRectangle().getSize();
177 } else {
178 prefSize = component.getPreferredSize();
179 }
180 width = Math.max(width, prefSize.width);
181 height+=prefSize.height;
182 }
183
184 return new Dimension(width, height);
185 }
186
187
188
189
190 public void addWidget(IWidget widget) {
191 Object nativeWidget = widget.getNativeWidget();
192 if (nativeWidget instanceof Component) {
193 addLayoutComponent((Component) nativeWidget, widget);
194 }
195 }
196
197
198
199
200 public void removeWidget(IWidget widget) {
201 }
202
203
204
205
206 public void destroy() {
207 if (map != null) {
208 map.clear();
209 map = null;
210 }
211 }
212
213 }