01: package uk.org.ponder.swingutil;
02:
03: import java.awt.GridBagConstraints;
04: import java.awt.GridBagLayout;
05: import java.awt.Container;
06: import java.awt.Component;
07:
08: /** The GBLWrap class wraps up the functionality of the complex GridBagLayout and
09: * GridBagConstraints classes into a convenient package, saving large amounts of
10: * repetitive client code*/
11:
12: public class GBLWrap {
13: GridBagLayout gbl;
14: public GridBagConstraints gbc = new GridBagConstraints();
15: Container target;
16:
17: /// Create a new GBL wrapper targetted at a particular container. The layout of the target container is set to a GridBagLayout and the wrapper object is initialised ready to begin adding subcomponents to the container at its top left corner.
18: public GBLWrap(Container target) {
19: this .target = target;
20: target.setLayout(gbl = new GridBagLayout());
21: gbc.weightx = gbc.weighty = 1;
22: }
23:
24: /// Adds a new component to the container layout with the specified alignment. The component is added in the next available position in the left-to-right, top-to-bottom scanning order, with either left alignment ("l"), centered alignment ("c") or right alignment ("r").
25: public GBLWrap apply(Component toadd, String align) {
26: gbc.anchor = GridBagConstraints.CENTER;
27: if (align.equals("l")) {
28: gbc.anchor = GridBagConstraints.WEST;
29: } else if (align.equals("c")) {
30: gbc.anchor = GridBagConstraints.CENTER;
31: } else if (align.equals("r")) {
32: gbc.anchor = GridBagConstraints.EAST;
33: }
34: gbl.setConstraints(toadd, gbc);
35: target.add(toadd);
36: reset();
37: return this ;
38: }
39:
40: /// This call is issued by the client immediately prior to an apply() call to indicate the next added component will be the last in its row.
41: public GBLWrap endrow() {
42: gbc.gridwidth = GridBagConstraints.REMAINDER;
43: return this ;
44: }
45:
46: /// This call is issued by the client immediately prior to an apply() call to indicate the next component will be the last in its column.
47: public GBLWrap endcol() {
48: gbc.gridheight = GridBagConstraints.REMAINDER;
49: return this ;
50: }
51:
52: /// This call is issued by the client to clear any state in the GBLWrap object set by a prior call to endrow() or endcol(). This method is called automatically by apply().
53: public GBLWrap reset() {
54: gbc.gridwidth = gbc.gridheight = 1;
55: return this;
56: }
57: }
|