1   /**
2    *  Copyright 1999-2002 Matthew Robinson and Pavel Vorobiev.
3    *  All Rights Reserved.
4    *
5    *  ===================================================
6    *  This program contains code from the book "Swing"
7    *  2nd Edition by Matthew Robinson and Pavel Vorobiev
8    *  http://www.spindoczine.com/sbe
9    *  ===================================================
10   *
11   *  The above paragraph must be included in full, unmodified
12   *  and completely intact in the beginning of any source code
13   *  file that references, copies or uses (in any way, shape
14   *  or form) code contained in this file.
15   */
16  package org.crosswire.common.swing;
17  
18  import java.awt.GridBagConstraints;
19  import java.awt.GridBagLayout;
20  import java.awt.Insets;
21  
22  import javax.swing.JComponent;
23  import javax.swing.JPanel;
24  
25  /**
26   * A simple helper class that provides various easy to understand methods that
27   * lays out the components using the GridBagLayout.
28   * 
29   * @author James Tan
30   */
31  public class GriddedPanel extends JPanel {
32      /**
33       * Creates a grid bag layout panel using a default insets constraints.
34       */
35      public GriddedPanel() {
36          this(new Insets(2, 2, 2, 2));
37      }
38  
39      /**
40       * Creates a grid bag layout panel using the specified insets constraints.
41       */
42      public GriddedPanel(Insets insets) {
43          super(new GridBagLayout());
44          // creates the constraints object and set the desired
45          // default values
46          constraints = new GridBagConstraints();
47          constraints.anchor = GridBagConstraints.WEST;
48          constraints.insets = insets;
49      }
50  
51      /**
52       * Adds the component to the specified row and col.
53       */
54      public void addComponent(JComponent component, int row, int col) {
55          addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, C_NONE);
56      }
57  
58      /**
59       * Adds the component to the specified row and col that spans across a
60       * specified number of columns and rows.
61       */
62      public void addComponent(JComponent component, int row, int col, int width, int height) {
63          addComponent(component, row, col, width, height, C_WEST, C_NONE);
64      }
65  
66      /**
67       * Adds the component to the specified row and col that anchors at the
68       * specified position.
69       */
70      public void addAnchoredComponent(JComponent component, int row, int col, int anchor) {
71          addComponent(component, row, col, C_WIDTH, C_HEIGHT, anchor, C_NONE);
72      }
73  
74      /**
75       * Adds the component to the specified row and col that spans across a
76       * specified number of columns and rows that anchors at the specified
77       * position.
78       */
79      public void addAnchoredComponent(JComponent component, int row, int col, int width, int height, int anchor) {
80          addComponent(component, row, col, width, height, anchor, C_NONE);
81      }
82  
83      /**
84       * Adds the component to the specified row and col filling the column
85       * horizontally.
86       */
87      public void addFilledComponent(JComponent component, int row, int col) {
88          addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, C_HORZ);
89      }
90  
91      /**
92       * Adds the component to the specified row and col with the specified
93       * filling direction.
94       */
95      public void addFilledComponent(JComponent component, int row, int col, int fill) {
96          addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, fill);
97      }
98  
99      /**
100      * Adds the component to the specified row and col that spans across a
101      * specified number of columns and rows with the specified filling
102      * direction.
103      */
104     public void addFilledComponent(JComponent component, int row, int col, int width, int height, int fill) {
105         addComponent(component, row, col, width, height, C_WEST, fill);
106     }
107 
108     /**
109      * Adds the component to the specified row and col that spans across a
110      * specified number of columns and rows with the specified filling direction
111      * and an anchoring position.
112      */
113     public void addComponent(JComponent component, int row, int col, int width, int height, int anchor, int fill) {
114         // sets the constraints object
115         constraints.gridx = col;
116         constraints.gridy = row;
117         constraints.gridwidth = width;
118         constraints.gridheight = height;
119         constraints.anchor = anchor;
120         double weightx = 0.0;
121         double weighty = 0.0;
122 
123         // only use the extra horizontal or vertical spaces if the component
124         // spans more than one column or/and row.
125         if (width > 1) {
126             weightx = 1.0;
127         }
128 
129         if (height > 1) {
130             weighty = 1.0;
131         }
132 
133         switch (fill) {
134         case GridBagConstraints.HORIZONTAL:
135             constraints.weightx = weightx;
136             constraints.weighty = 0.0;
137             break;
138         case GridBagConstraints.VERTICAL:
139             constraints.weighty = weighty;
140             constraints.weightx = 0.0;
141             break;
142         case GridBagConstraints.BOTH:
143             constraints.weightx = weightx;
144             constraints.weighty = weighty;
145             break;
146         case GridBagConstraints.NONE:
147             constraints.weightx = 0.0;
148             constraints.weighty = 0.0;
149             break;
150         default:
151             break;
152         }
153         constraints.fill = fill;
154         add(component, constraints);
155     }
156 
157     private GridBagConstraints constraints;
158     // define some default constraints values
159     private static final int C_HORZ = GridBagConstraints.HORIZONTAL;
160     private static final int C_NONE = GridBagConstraints.NONE;
161     private static final int C_WEST = GridBagConstraints.WEST;
162     private static final int C_WIDTH = 1;
163     private static final int C_HEIGHT = 1;
164 
165     /**
166      * Serialization ID
167      */
168     private static final long serialVersionUID = 5924150977979727523L;
169 }
170