1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.gui;
17
18import java.awt.GridBagConstraints;
19import java.awt.Insets;
20/**
21 * GBCF  Grib Bag Constraints Factory.  This class exists mostly
22 * for tidying up code that used the exsivly verbose GridBagConstraints
23 * object, hence the shortened class name.
24 * By default a two column equal with table is assumed, to support other
25 * column layouts use the constructor that take an array of column wieghts
26 * Factory class for generating constraints for a equal width column table.
27 * the table is similar to the following table rendered in HTML
28 * <table border="2">
29 *   <tr>
30 *     <td>data</td><td>data</td>
31 *   </tr>
32 *   <tr>
33 *     <td colspan="2">data</td>
34 *   </tr>
35 *   <tr>
36 *     <td>data</td><td>data</td>
37 *   </tr>
38 *   <tr>
39 *     <td>data</td><td>data</td>
40 *   </tr>
41 * <table>
42 * @author not attributable
43 * @version 1.0
44 */
45public class GBCF {
46
47    protected Insets insets = new Insets(1,4,1,4);
48    protected Insets noinsets = new Insets(0,0,0,0);
49    protected double[] columnWeights= new double[]{0.5,0.5};
50
51    public GBCF(){
52
53    }
54    /**
55     * to support more than two columns or vary the column wieghtings
56     * @param columnWeights double[]
57     */
58    public GBCF(double[] columnWeights){
59        this.columnWeights=columnWeights;
60    }
61    /**
62     *
63     * @param row int the 0 indexed row
64     * @param col int the 0 indexed column
65     * @param span boolean does cell span both columns
66     * @return GridBagConstraints
67     */
68    public GridBagConstraints getCell(int row,int col){
69        return new GridBagConstraints(col,              //gridx
70                                      row,              //gridy
71                                      1,                //gridwidth
72                                      1,                //gridheight
73                                      columnWeights[col],//weightx
74                                      0.0,              //weithty
75                                      GridBagConstraints.WEST, // anchor
76                                      GridBagConstraints.NONE, //fill
77                                      insets,  // insets
78                                      1, //ipadx
79                                      1  //ipady
80                                      );
81   }
82   /**
83    *
84    * @param row int the 0 indexed row
85    * @param first boolean is this the first column or the second
86    * @param span boolean does cell span both columns
87    * @return GridBagConstraints
88    */
89   public GridBagConstraints getSpan(int row){
90       return new GridBagConstraints(0,               //gridx
91                                     row,             //gridy
92                                     2,               //gridwidth
93                                     1,               //gridheight
94                                     columnWeights[0],//weightx
95                                     0.0,             //weithty
96                                     GridBagConstraints.WEST, // anchor
97                                     GridBagConstraints.NONE, //fill
98                                     insets,  // insets
99                                     1, //ipadx
00                                     1  //ipady
01                                     );
02  }
03  public GridBagConstraints getVertGlue(int row){
04      return new GridBagConstraints(0,               //gridx
05                                    row,             //gridy
06                                    2,               //gridwidth
07                                    1,               //gridheight
08                                    columnWeights[0],//weightx
09                                    1.0,             //weithty
10                                    GridBagConstraints.NORTHWEST, // anchor
11                                    GridBagConstraints.NONE, //fill
12                                    noinsets,  // insets
13                                    0, //ipadx
14                                    0  //ipady
15                                    );
16  }
17}
18