1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.layout;
17
18 import java.awt.Component;
19 import java.awt.GridBagConstraints;
20 import java.awt.GridBagLayout;
21
22 import javax.swing.JLabel;
23 import javax.swing.JPanel;
24
25 import junit.framework.TestCase;
26
27 public class GridBagLayoutBuilderTests extends TestCase {
28
29
30
31
32 public void testAppend1() throws Exception {
33 GridBagLayoutBuilder builder = new GridBagLayoutBuilder();
34 builder.append(new JPanel());
35 builder.append(new JPanel(), 2, 2);
36 builder.append(new JPanel());
37 builder.nextLine();
38 builder.append(new JPanel()).append(new JPanel()).nextLine();
39 builder.append(new JPanel()).append(new JPanel()).append(new JPanel()).append(new JPanel()).nextLine();
40
41 JPanel panel = builder.getPanel();
42 final Component[] comps = panel.getComponents();
43 final GridBagLayout layout = ((GridBagLayout)panel.getLayout());
44
45 check(layout.getConstraints(comps[0]), 0, 0, 1, 1, false, false);
46 check(layout.getConstraints(comps[1]), 1, 0, 2, 2, false, false);
47 check(layout.getConstraints(comps[2]), 3, 0, 1, 1, false, false);
48 check(layout.getConstraints(comps[3]), 0, 1, 1, 1, false, false);
49 check(layout.getConstraints(comps[4]), 3, 1, 1, 1, false, false);
50 check(layout.getConstraints(comps[5]), 0, 2, 1, 1, false, false);
51 check(layout.getConstraints(comps[6]), 1, 2, 1, 1, false, false);
52 check(layout.getConstraints(comps[7]), 2, 2, 1, 1, false, false);
53 check(layout.getConstraints(comps[8]), 3, 2, 1, 1, false, false);
54 }
55
56 public void testAppend2() throws Exception {
57 GridBagLayoutBuilder builder = new GridBagLayoutBuilder();
58 builder.append(new JPanel()).append(new JPanel(), 2, 1).append(new JPanel()).nextLine();
59 builder.append(new JPanel()).append(new JPanel()).append(new JPanel()).append(new JPanel()).nextLine();
60
61 JPanel panel = builder.getPanel();
62 final Component[] comps = panel.getComponents();
63 final GridBagLayout layout = ((GridBagLayout)panel.getLayout());
64
65 check(layout.getConstraints(comps[0]), 0, 0, 1, 1, false, false);
66 check(layout.getConstraints(comps[1]), 1, 0, 2, 1, false, false);
67 check(layout.getConstraints(comps[2]), 3, 0, 1, 1, false, false);
68 check(layout.getConstraints(comps[3]), 0, 1, 1, 1, false, false);
69 check(layout.getConstraints(comps[4]), 1, 1, 1, 1, false, false);
70 check(layout.getConstraints(comps[5]), 2, 1, 1, 1, false, false);
71 check(layout.getConstraints(comps[6]), 3, 1, 1, 1, false, false);
72 }
73
74 private void check(GridBagConstraints gbc, final int x, final int y, final int width, final int height,
75 boolean expandX, boolean expandY) {
76 assertEquals(x, gbc.gridx);
77 assertEquals(y, gbc.gridy);
78 assertEquals(width, gbc.gridwidth);
79 assertEquals(height, gbc.gridheight);
80 assertEquals(expandX, gbc.weightx > 0.0);
81 assertEquals(expandY, gbc.weighty > 0.0);
82 }
83
84 public void testAppendLabeledField() throws Exception {
85 GridBagLayoutBuilder builder = new GridBagLayoutBuilder();
86
87 builder.appendLabeledField(new JLabel("0"), new JLabel("3"), LabelOrientation.TOP, 1, 2, true, true);
88 builder.appendLabeledField(new JLabel("1"), new JLabel("2"), LabelOrientation.LEFT, 2, 1, true, false);
89 builder.nextLine();
90
91 builder.appendLabeledField(new JLabel("5"), new JLabel("4"), LabelOrientation.BOTTOM, 3, 1, true, false);
92 builder.nextLine();
93 builder.nextLine();
94
95 builder.appendLabeledField(new JLabel("7"), new JLabel("6"), LabelOrientation.RIGHT, 3, 1, true, false);
96 builder.nextLine();
97
98 JPanel panel = builder.getPanel();
99 final Component[] comps = panel.getComponents();
100 final GridBagLayout layout = ((GridBagLayout)panel.getLayout());
101
102 check(layout.getConstraints(comps[0]), 0, 0, 1, 1, true, false);
103 check(layout.getConstraints(comps[1]), 1, 0, 1, 1, false, false);
104 check(layout.getConstraints(comps[2]), 2, 0, 2, 1, true, false);
105 check(layout.getConstraints(comps[3]), 0, 1, 1, 2, true, true);
106 check(layout.getConstraints(comps[4]), 1, 1, 3, 1, true, false);
107 check(layout.getConstraints(comps[5]), 1, 2, 3, 1, true, false);
108 check(layout.getConstraints(comps[6]), 0, 3, 3, 1, true, false);
109 check(layout.getConstraints(comps[7]), 3, 3, 1, 1, false, false);
110 }
111
112 }