001    /*
002     * Copyright (C) 2003 Adam Olsen This program is free software; you can
003     * redistribute it and/or modify it under the terms of the GNU General Public
004     * License as published by the Free Software Foundation; either version 1, or
005     * (at your option) any later version. This program is distributed in the hope
006     * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
007     * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
008     * General Public License for more details. You should have received a copy of
009     * the GNU General Public License along with this program; if not, write to the
010     * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
011     */
012    package com.valhalla.jbother;
013    
014    import java.awt.GridBagConstraints;
015    import java.awt.GridBagLayout;
016    import java.awt.Insets;
017    
018    import javax.swing.BorderFactory;
019    import javax.swing.JComponent;
020    import javax.swing.JLabel;
021    import javax.swing.JPanel;
022    
023    /**
024     * A panel that provides a form in the following format:
025     *        label:     field
026     *        label:     field
027     * and pads the end so that when the panel is resized, the form stays at the
028     * top left of the dialog
029     */
030    public class AbstractOptionPanel extends JPanel {
031        private GridBagLayout grid = new GridBagLayout();
032    
033        private GridBagConstraints c = new GridBagConstraints();
034    
035        /**
036         * Sets up the panel and the GridBagLayout
037         */
038        public AbstractOptionPanel() {
039            c.gridx = 0;
040            c.gridy = 0;
041            c.insets = new Insets(1, 1, 1, 1);
042            c.ipadx = 1;
043            c.ipady = 1;
044            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
045            setLayout(grid);
046        }
047    
048        /**
049         * Adds a component to the form and sets up the layout manager for the
050         * next component
051         * @param text The text for the label
052         * @param component the component field (JTextField, etc)
053         */
054        public void addComponent(String text, JComponent component) {
055            c.anchor = GridBagConstraints.WEST;
056            JLabel label = new JLabel(text + ": ");
057    
058            c.weightx = 0;
059            grid.setConstraints(label, c);
060            add(label);
061            c.gridx++;
062            grid.setConstraints(component, c);
063            add(component);
064            c.gridy++;
065            c.gridx = 0;
066        }
067    
068        /**
069         * Returns the GridBagLayout that this form is using
070         */
071        public GridBagConstraints getConstraints() {
072            return c;
073        }
074    
075        /**
076         * Adds a component, and allows you to specify where in the grid to put it
077         * @param component the component to add
078         * @param gridx the column to place the component in
079         * @param anchor which side of the column cell to place the component in
080         */
081        public void addComponent(JComponent component, int gridx, int anchor) {
082            c.anchor = anchor;
083            c.gridx = gridx;
084            c.fill = GridBagConstraints.VERTICAL;
085            grid.setConstraints(component, c);
086            add(component);
087            c.gridx = 0;
088            c.gridy++;
089        }
090    
091        /**
092         * Call to add a spacer to the end of the form
093         */
094        public void end() {
095            c.weightx = .9;
096            c.weighty = .9;
097            c.gridwidth = 2;
098            JLabel label = new JLabel();
099            grid.setConstraints(label, c);
100            add(label);
101        }
102    }