1 |
/* BoxLayout.java -- A layout for swing components. |
/* BoxLayout.java -- A layout for swing components. |
2 |
Copyright (C) 2002 Free Software Foundation, Inc. |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
|
|
|
38 |
package javax.swing; |
package javax.swing; |
39 |
|
|
40 |
import java.awt.Container; |
import java.awt.AWTError; |
41 |
import java.awt.Component; |
import java.awt.Component; |
42 |
|
import java.awt.ComponentOrientation; |
43 |
|
import java.awt.Container; |
44 |
import java.awt.Dimension; |
import java.awt.Dimension; |
45 |
import java.awt.GridLayout; |
import java.awt.GridLayout; |
46 |
import java.awt.LayoutManager2; |
import java.awt.LayoutManager2; |
47 |
import java.io.Serializable; |
import java.io.Serializable; |
48 |
|
|
49 |
|
|
50 |
/** |
/** |
51 |
* A layout for swing components. |
* A layout for swing components. |
52 |
* This implementation delegates its methods to |
* This implementation delegates its methods to |
56 |
*/ |
*/ |
57 |
public class BoxLayout implements LayoutManager2, Serializable |
public class BoxLayout implements LayoutManager2, Serializable |
58 |
{ |
{ |
59 |
GridLayout gridbag; |
/** |
60 |
|
* Specifies that components are laid out left to right. |
61 |
|
*/ |
62 |
|
public static final int X_AXIS = 0; |
63 |
|
|
64 |
|
/** |
65 |
|
* Specifies that components are laid out top to bottom. |
66 |
|
*/ |
67 |
|
public static final int Y_AXIS = 1; |
68 |
|
|
69 |
|
/** |
70 |
|
* Specifies that components are laid out in the direction of a line of text. |
71 |
|
*/ |
72 |
|
public static final int LINE_AXIS = 2; |
73 |
|
|
74 |
|
/** |
75 |
|
* Sepcifies that components are laid out in the direction of the line flow. |
76 |
|
*/ |
77 |
|
public static final int PAGE_AXIS = 3; |
78 |
|
|
79 |
|
/* |
80 |
|
* Needed for serialization. |
81 |
|
*/ |
82 |
|
private static final long serialVersionUID = -2474455742719112368L; |
83 |
|
|
84 |
|
/* |
85 |
|
* The container given to the constructor. |
86 |
|
*/ |
87 |
|
private Container container; |
88 |
|
|
89 |
|
/* |
90 |
|
* Internal layout. |
91 |
|
*/ |
92 |
|
private GridLayout grid; |
93 |
|
|
94 |
|
/* |
95 |
|
* Current type of component layouting. Defaults to X_AXIS. |
96 |
|
*/ |
97 |
|
private int way = X_AXIS; |
98 |
|
|
99 |
|
/** |
100 |
|
* Constructs a <code>BoxLayout</code> object. |
101 |
|
* |
102 |
|
* @param container The container that needs to be laid out. |
103 |
|
* @param way The orientation of the components. |
104 |
|
* |
105 |
|
* @exception AWTError If way has an invalid value. |
106 |
|
*/ |
107 |
|
public BoxLayout(Container container, int way) |
108 |
|
{ |
109 |
|
int width = 0; |
110 |
|
int height = 0; |
111 |
|
ComponentOrientation orientation = container.getComponentOrientation(); |
112 |
|
|
113 |
|
this.container = container; |
114 |
|
this.way = way; |
115 |
|
|
116 |
|
switch (way) |
117 |
|
{ |
118 |
|
case X_AXIS: |
119 |
|
width = 1; |
120 |
|
break; |
121 |
|
case Y_AXIS: |
122 |
|
height = 1; |
123 |
|
break; |
124 |
|
case LINE_AXIS: |
125 |
|
if (orientation.isHorizontal()) |
126 |
|
height = 1; |
127 |
|
else |
128 |
|
width = 1; |
129 |
|
break; |
130 |
|
case PAGE_AXIS: |
131 |
|
if (!orientation.isHorizontal()) |
132 |
|
height = 1; |
133 |
|
else |
134 |
|
width = 1; |
135 |
|
break; |
136 |
|
default: |
137 |
|
throw new AWTError("Invalid value for way"); |
138 |
|
} |
139 |
|
|
140 |
|
grid = new GridLayout(width, height); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
|
* Adds a component to the layout. |
145 |
|
* |
146 |
|
* @param name The name of the component to add. |
147 |
|
* @param component the component to add to the layout. |
148 |
|
*/ |
149 |
|
public void addLayoutComponent(String name, Component component) |
150 |
|
{ |
151 |
|
if (way == X_AXIS |
152 |
|
|| (way == LINE_AXIS |
153 |
|
&& component.getComponentOrientation().isHorizontal()) |
154 |
|
|| (way == PAGE_AXIS |
155 |
|
&& !component.getComponentOrientation().isHorizontal())) |
156 |
|
grid.setColumns(grid.getColumns() + 1); |
157 |
|
else |
158 |
|
grid.setRows(grid.getRows() + 1); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
|
* Removes a component from the layout. |
163 |
|
* |
164 |
|
* @param component The component to remove from the layout. |
165 |
|
*/ |
166 |
|
public void removeLayoutComponent(Component component) |
167 |
|
{ |
168 |
|
grid.removeLayoutComponent(component); |
169 |
|
|
170 |
|
if (way == X_AXIS |
171 |
|
|| (way == LINE_AXIS |
172 |
|
&& component.getComponentOrientation().isHorizontal()) |
173 |
|
|| (way == PAGE_AXIS |
174 |
|
&& !component.getComponentOrientation().isHorizontal())) |
175 |
|
grid.setColumns(grid.getColumns() - 1); |
176 |
|
else |
177 |
|
grid.setRows(grid.getRows() - 1); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
|
* Returns the preferred size of the layout. |
182 |
|
* |
183 |
|
* @param parent The container that needs to be laid out. |
184 |
|
* |
185 |
|
* @return The dimension of the layout. |
186 |
|
*/ |
187 |
|
public Dimension preferredLayoutSize(Container parent) |
188 |
|
{ |
189 |
|
if (parent != container) |
190 |
|
throw new AWTError("invalid parent"); |
191 |
|
|
192 |
final static int X_AXIS = 0; |
return grid.preferredLayoutSize(parent); |
193 |
final static int Y_AXIS = 1; |
} |
194 |
|
|
195 |
int way = X_AXIS; |
/** |
196 |
|
* Returns the minimum size of the layout. |
197 |
|
* |
198 |
|
* @param parent The container that needs to be laid out. |
199 |
|
* |
200 |
|
* @return The dimension of the layout. |
201 |
|
*/ |
202 |
|
public Dimension minimumLayoutSize(Container parent) |
203 |
|
{ |
204 |
|
if (parent != container) |
205 |
|
throw new AWTError("invalid parent"); |
206 |
|
|
207 |
|
return grid.minimumLayoutSize(parent); |
208 |
|
} |
209 |
|
|
210 |
BoxLayout(JComponent p, |
/** |
211 |
int way) |
* Lays out the specified container using this layout. |
212 |
{ |
* |
213 |
int width = 0; |
* @param parent The container that needs to be laid out. |
214 |
int height = 0; |
*/ |
215 |
|
public void layoutContainer(Container parent) |
216 |
|
{ |
217 |
|
if (parent != container) |
218 |
|
throw new AWTError("invalid parent"); |
219 |
|
|
220 |
|
grid.layoutContainer(parent); |
221 |
|
} |
222 |
|
|
223 |
this.way = way; |
/** |
224 |
|
* Adds a component to the layout. |
225 |
|
* |
226 |
|
* @param child The component to add to the layout. |
227 |
|
* @param constraints The constraints for the component in the layout. |
228 |
|
*/ |
229 |
|
public void addLayoutComponent(Component child, Object constraints) |
230 |
|
{ |
231 |
|
addLayoutComponent("", child); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* Returns the alignment along the X axis for the container. |
236 |
|
* |
237 |
|
* @param parent The container that needs to be laid out. |
238 |
|
* |
239 |
|
* @return The alignment. |
240 |
|
*/ |
241 |
|
public float getLayoutAlignmentX(Container parent) |
242 |
|
{ |
243 |
|
if (parent != container) |
244 |
|
throw new AWTError("invalid parent"); |
245 |
|
|
246 |
|
return 0; |
247 |
|
} |
248 |
|
|
249 |
if (way == X_AXIS) |
/** |
250 |
{ |
* Returns the alignment along the Y axis for the container. |
251 |
width = 1; |
* |
252 |
} |
* @param parent The container that needs to be laid out. |
253 |
else |
* |
254 |
{ |
* @return The alignment. |
255 |
height = 1; |
*/ |
256 |
} |
public float getLayoutAlignmentY(Container parent) |
257 |
|
{ |
258 |
|
if (parent != container) |
259 |
gridbag = new GridLayout(width, height); |
throw new AWTError("invalid parent"); |
260 |
} |
|
261 |
|
return 0; |
262 |
BoxLayout(int way) |
} |
263 |
{ |
|
264 |
this(null,way); |
/** |
265 |
} |
* Invalidates the layout. |
266 |
|
* |
267 |
|
* @param parent The container that needs to be laid out. |
268 |
public void addLayoutComponent(String name, Component comp) |
*/ |
269 |
{ |
public void invalidateLayout(Container parent) |
270 |
if (way == X_AXIS) |
{ |
271 |
{ |
if (parent != container) |
272 |
gridbag.setColumns( gridbag.getColumns() + 1); |
throw new AWTError("invalid parent"); |
273 |
} |
} |
274 |
else |
|
275 |
{ |
/** |
276 |
gridbag.setRows( gridbag.getRows() + 1); |
* Returns the maximum size of the layout gived the components |
277 |
} |
* in the given container. |
278 |
} |
* |
279 |
|
* @param parent The container that needs to be laid out. |
280 |
public void removeLayoutComponent(Component comp) |
* |
281 |
{ |
* @return The dimension of the layout. |
282 |
gridbag.removeLayoutComponent(comp); |
*/ |
283 |
if (way == X_AXIS) |
public Dimension maximumLayoutSize(Container parent) |
284 |
{ |
{ |
285 |
gridbag.setColumns( gridbag.getColumns() - 1); |
if (parent != container) |
286 |
} |
throw new AWTError("invalid parent"); |
287 |
else |
|
288 |
{ |
return preferredLayoutSize(parent); |
289 |
gridbag.setRows( gridbag.getRows() - 1); |
} |
|
} |
|
|
} |
|
|
|
|
|
public Dimension preferredLayoutSize(Container parent) |
|
|
{ |
|
|
return gridbag.preferredLayoutSize(parent); |
|
|
} |
|
|
|
|
|
public Dimension minimumLayoutSize(Container parent) |
|
|
{ |
|
|
return gridbag.minimumLayoutSize(parent); |
|
|
} |
|
|
|
|
|
public void layoutContainer(Container parent) |
|
|
{ |
|
|
gridbag.layoutContainer(parent); |
|
|
} |
|
|
|
|
|
public void addLayoutComponent ( Component child, Object constraints ) |
|
|
{ |
|
|
addLayoutComponent("", child); |
|
|
} |
|
|
|
|
|
public float getLayoutAlignmentX ( Container parent ) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public float getLayoutAlignmentY ( Container parent ) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public void invalidateLayout ( Container parent ) |
|
|
{ |
|
|
} |
|
|
|
|
|
public Dimension maximumLayoutSize ( Container parent ) |
|
|
{ |
|
|
return preferredLayoutSize(parent); |
|
|
} |
|
290 |
} |
} |