View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   // stolen from XPath Explorer (http://www.xpathexplorer.com)
5   package net.sourceforge.pmd.cpd;
6   
7   import java.awt.Component;
8   import java.awt.Container;
9   import java.awt.GridBagConstraints;
10  import java.awt.GridBagLayout;
11  import java.awt.Insets;
12  
13  import javax.swing.JLabel;
14  import javax.swing.SwingConstants;
15  
16  public class GridBagHelper {
17  
18      GridBagLayout gridbag;
19      Container container;
20      GridBagConstraints c;
21      int x = 0;
22      int y = 0;
23      int labelAlignment = SwingConstants.RIGHT;
24      double[] weights;
25  
26      public GridBagHelper(Container container, double[] weights) {
27          this.container = container;
28          this.weights = weights;
29  
30          gridbag = new GridBagLayout();
31          container.setLayout(gridbag);
32  
33          c = new GridBagConstraints();
34          c.insets = new Insets(2, 2, 2, 2);
35          c.anchor = GridBagConstraints.EAST;
36          c.fill = GridBagConstraints.HORIZONTAL;
37      }
38  
39      public void add(Component component) {
40          add(component, 1);
41      }
42  
43      public void add(Component component, int width) {
44          c.gridx = x;
45          c.gridy = y;
46          c.weightx = weights[x];
47          c.gridwidth = width;
48          gridbag.setConstraints(component, c);
49          container.add(component);
50          x += width;
51      }
52  
53      public void nextRow() {
54          y++;
55          x = 0;
56      }
57  
58      public void addLabel(String label) {
59          add(new JLabel(label, labelAlignment));
60      }
61  
62  }
63