2-APL UPC project.
1package blockworld.lib;
2
3import java.awt.GridBagConstraints;
4import java.awt.GridBagLayout;
5import java.awt.Insets;
6
7import javax.swing.BorderFactory;
8import javax.swing.JComponent;
9import javax.swing.JLabel;
10import javax.swing.JPanel;
11import javax.swing.border.Border;
12
13/**
14 * Groups a set of attribute views together into a Panel. The group has a title
15 * that is shown in a border.
16 */
17public class AttrGroup extends JPanel {
18
19 private static final long serialVersionUID = -1420253091328660372L;
20
21 protected GridBagLayout gridbag = new GridBagLayout();
22
23 protected GridBagConstraints constraints = new GridBagConstraints();
24
25 /** Construct a AttrGroup with the title "Attributes". */
26 public AttrGroup() {
27 this( "Attributes" );
28 }
29
30 /**
31 * Construct a group with the specified title.
32 *
33 * @param title
34 * The title of this group, this shown in a border.
35 */
36 public AttrGroup( String title ) {
37 Border etche = BorderFactory.createEtchedBorder();
38
39 setBorder( BorderFactory.createTitledBorder( etche, title ) );
40 // setLayout(new GridLayout(0, 2, 5, 0));
41
42 // / set initial layout settings
43 setLayout( gridbag );
44 constraints.anchor = GridBagConstraints.EAST;
45 constraints.insets = new Insets( 1, 5, 2, 5 );
46 }
47
48 protected void addRow( String descr, JComponent view ) {
49 JLabel label = new JLabel( descr );
50
51 label.setLabelFor( view );
52
53 constraints.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
54 constraints.fill = GridBagConstraints.NONE; // reset to default
55 constraints.weightx = 0.0; // reset to default
56
57 gridbag.setConstraints( label, constraints );
58 add( label );
59
60 constraints.gridwidth = GridBagConstraints.REMAINDER; // end row
61 constraints.fill = GridBagConstraints.HORIZONTAL;
62 constraints.weightx = 1.0;
63 gridbag.setConstraints( view, constraints );
64 add( view );
65 }
66
67 /**
68 * Add a read only int attribute to this attribute group. This constructs
69 * the correct view and adds a label showing the attribute name.
70 */
71 public void add( ROIntegerAttr attr ) {
72 addRow( attr.getName(), new ROIntegerView( attr ) );
73 }
74}