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 javax.swing.*;
8   import java.awt.Component;
9   import java.awt.Container;
10  import java.awt.GridBagConstraints;
11  import java.awt.GridBagLayout;
12  import java.awt.Insets;
13  
14  public class GridBagHelper {
15  
16      GridBagLayout gridbag;
17      Container container;
18      GridBagConstraints c;
19      int x = 0;
20      int y = 0;
21      int labelAlignment = SwingConstants.RIGHT;
22      double[] weights;
23  
24      public GridBagHelper(Container container, double[] weights) {
25          this.container = container;
26          this.weights = weights;
27  
28          gridbag = new GridBagLayout();
29          container.setLayout(gridbag);
30  
31          c = new GridBagConstraints();
32          c.insets = new Insets(2, 2, 2, 2);
33          c.anchor = GridBagConstraints.EAST;
34          c.fill = GridBagConstraints.HORIZONTAL;
35      }
36  
37      public void add(Component component) {
38          add(component, 1);
39      }
40  
41      public void add(Component component, int width) {
42          c.gridx = x;
43          c.gridy = y;
44          c.weightx = weights[x];
45          c.gridwidth = width;
46          gridbag.setConstraints(component, c);
47          container.add(component);
48          x += width;
49      }
50  
51      public void nextRow() {
52          y++;
53          x = 0;
54      }
55  
56      public void addLabel(String label) {
57          add(new JLabel(label, labelAlignment));
58      }
59  
60  }
61