]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/GuiGridLayout.java
703f7dd1d1be00f6cca07525fe54ae28e9db125f
[GpsPrune.git] / src / tim / prune / gui / GuiGridLayout.java
1 package tim.prune.gui;
2
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6
7 import javax.swing.JComponent;
8 import javax.swing.JPanel;
9
10 /**
11  * Class to make it easier to use GridBagLayout for a non-equal-width layout
12  * Default is two columns but can handle more
13  */
14 public class GuiGridLayout
15 {
16         private GridBagLayout _layout = null;
17         private GridBagConstraints _constraints = null;
18         private JPanel _panel = null;
19         private int _numColumns = 0;
20         private double[] _colWeights = null;
21         private boolean[] _rightAligns = null;
22         private int _x = 0;
23         private int _y = 0;
24
25         /**
26          * Constructor
27          * @param inPanel panel using layout
28          */
29         public GuiGridLayout(JPanel inPanel)
30         {
31                 // Default is two columns, with more weight to the right-hand one; first column is right-aligned
32                 this(inPanel, null, null);
33         }
34
35         /**
36          * Constructor
37          * @param inPanel panel using layout
38          * @param inColumnWeights array of column weights
39          * @param inAlignRights array of booleans, true for right alignment, false for left
40          */
41         public GuiGridLayout(JPanel inPanel, double[] inColumnWeights, boolean[] inAlignRights)
42         {
43                 _panel = inPanel;
44                 _layout = new GridBagLayout();
45                 _constraints = new GridBagConstraints();
46                 _colWeights = inColumnWeights;
47                 _rightAligns = inAlignRights;
48                 if (_colWeights == null || _rightAligns == null || _colWeights.length != _rightAligns.length
49                         || _colWeights.length < 2)
50                 {
51                         _colWeights = new double[] {0.5, 1.0};
52                         _rightAligns = new boolean[] {true, false};
53                 }
54                 _numColumns = _colWeights.length;
55                 _constraints.weightx = 1.0;
56                 _constraints.weighty = 0.0;
57                 _constraints.ipadx = 10;
58                 _constraints.ipady = 1;
59                 _constraints.insets = new Insets(1, 5, 1, 5);
60                 // Apply layout to panel
61                 _panel.setLayout(_layout);
62         }
63
64         /**
65          * Add the given component to the grid
66          * @param inComponent component to add
67          */
68         public void add(JComponent inComponent)
69         {
70                 _constraints.gridx = _x;
71                 _constraints.gridy = _y;
72                 _constraints.weightx = _colWeights[_x];
73                 // set anchor
74                 _constraints.anchor = (_rightAligns[_x]?GridBagConstraints.LINE_END:GridBagConstraints.LINE_START);
75                 _layout.setConstraints(inComponent, _constraints);
76                 // add to panel
77                 _panel.add(inComponent);
78                 // work out next position
79                 _x++;
80                 if (_x >= _numColumns) {
81                         nextRow();
82                 }
83         }
84
85         /**
86          * Go to the next row of the grid
87          */
88         public void nextRow()
89         {
90                 _x = 0;
91                 _y++;
92         }
93 }