    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * -----------------------
    028     * InsetsChooserPanel.java
    029     * -----------------------
    030     * (C) Copyright 2000-2005, by Andrzej Porebski and Contributors.
    031     *
    032     * Original Author:  Andrzej Porebski;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *                   Arnaud Lelievre;
    035     *
    036     * $Id: InsetsChooserPanel.java,v 1.5 2005/10/18 13:18:34 mungady Exp $
    037     *
    038     * Changes (from 7-Nov-2001)
    039     * -------------------------
    040     * 07-Nov-2001 : Added to com.jrefinery.ui package (DG);
    041     * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    042     * 03-Feb-2003 : Added Math.abs() to ensure no negative insets can be set (DG);
    043     * 08-Sep-2003 : Added internationalization via use of properties
    044     *               resourceBundle (RFE 690236) (AL);
    045     * 07-Oct-2005 : Renamed getInsets() --> getInsetsValue() to avoid conflict
    046     *               with JComponent's getInsets() (DG);
    047     *
    048     */
    049
    050    package org.jfree.ui;
    051
    052    import java.awt.BorderLayout;
    053    import java.awt.GridBagConstraints;
    054    import java.awt.GridBagLayout;
    055    import java.awt.Insets;
    056    import java.util.ResourceBundle;
    057
    058    import javax.swing.JLabel;
    059    import javax.swing.JPanel;
    060    import javax.swing.JTextField;
    061    import javax.swing.border.TitledBorder;
    062
    063    /**
    064     * A component for editing an instance of the Insets class.
    065     *
    066     * @author Andrzej Porebski
    067     */
    068    public class InsetsChooserPanel extends JPanel {
    069
    070        /** A text field for the 'top' setting. */
    071        private JTextField topValueEditor;
    072
    073        /** A text field for the 'left' setting. */
    074        private JTextField leftValueEditor;
    075
    076        /** A text field for the 'bottom' setting. */
    077        private JTextField bottomValueEditor;
    078
    079        /** A text field for the 'right' setting. */
    080        private JTextField rightValueEditor;
    081
    082        /** The resourceBundle for the localization. */
    083        protected static ResourceBundle localizationResources =
    084            ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
    085
    086        /**
    087         * Creates a chooser panel that allows manipulation of Insets values.
    088         * The values are initialized to the empty insets (0,0,0,0).
    089         */
    090        public InsetsChooserPanel() {
    091            this(new Insets(0, 0, 0, 0));
    092        }
    093
    094        /**
    095         * Creates a chooser panel that allows manipulation of Insets values.
    096         * The values are initialized to the current values of provided insets.
    097         *
    098         * @param current  the insets.
    099         */
    100        public InsetsChooserPanel(Insets current) {
    101            current = (current == null) ? new Insets(0, 0, 0, 0) : current;
    102
    103            this.topValueEditor = new JTextField(new IntegerDocument(), ""
    104                    + current.top, 0);
    105            this.leftValueEditor = new JTextField(new IntegerDocument(), ""
    106                    + current.left, 0);
    107            this.bottomValueEditor = new JTextField(new IntegerDocument(), ""
    108                    + current.bottom, 0);
    109            this.rightValueEditor = new JTextField(new IntegerDocument(), ""
    110                    + current.right, 0);
    111
    112            final JPanel panel = new JPanel(new GridBagLayout());
    113            panel.setBorder(
    114                    new TitledBorder(localizationResources.getString("Insets")));
    115
    116            // First row
    117            panel.add(new JLabel(localizationResources.getString("Top")),
    118                new GridBagConstraints(1, 0, 3, 1, 0.0, 0.0,
    119                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    120                new Insets(0, 0, 0, 0), 0, 0));
    121
    122            // Second row
    123            panel.add(new JLabel(" "), new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
    124                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    125                new Insets(0, 12, 0, 12), 8, 0));
    126            panel.add(this.topValueEditor, new GridBagConstraints(2, 1, 1, 1, 0.0,
    127                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    128                new Insets(0, 0, 0, 0), 0, 0));
    129            panel.add(new JLabel(" "),  new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
    130                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    131                new Insets(0, 12, 0, 11), 8, 0));
    132
    133            // Third row
    134            panel.add(new JLabel(localizationResources.getString("Left")),
    135                new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
    136                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    137                new Insets(0, 4, 0, 4), 0, 0));
    138            panel.add(this.leftValueEditor, new GridBagConstraints(1, 2, 1, 1,
    139                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    140                new Insets(0, 0, 0, 0), 0, 0));
    141            panel.add(new JLabel(" "), new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
    142                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    143                new Insets(0, 12, 0, 12), 8, 0));
    144            panel.add(this.rightValueEditor, new GridBagConstraints(3, 2, 1, 1,
    145                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    146                new Insets(0, 0, 0, 0), 0, 0));
    147            panel.add(new JLabel(localizationResources.getString("Right")),
    148                new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
    149                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    150                new Insets(0, 4, 0, 4), 0, 0));
    151
    152            // Fourth row
    153            panel.add(this.bottomValueEditor, new GridBagConstraints(2, 3, 1, 1,
    154                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    155                new Insets(0, 0, 0, 0), 0, 0));
    156
    157            // Fifth row
    158            panel.add(new JLabel(localizationResources.getString("Bottom")),
    159                new GridBagConstraints(1, 4, 3, 1, 0.0, 0.0,
    160                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    161                new Insets(0, 0, 0, 0), 0, 0));
    162            setLayout(new BorderLayout());
    163            add(panel, BorderLayout.CENTER);
    164
    165        }
    166
    167        /**
    168         * Returns a new <code>Insets</code> instance to match the values entered
    169         * on the panel.
    170         *
    171         * @return The insets.
    172         */
    173        public Insets getInsetsValue() {
    174            return new Insets(
    175                Math.abs(stringToInt(this.topValueEditor.getText())),
    176                Math.abs(stringToInt(this.leftValueEditor.getText())),
    177                Math.abs(stringToInt(this.bottomValueEditor.getText())),
    178                Math.abs(stringToInt(this.rightValueEditor.getText())));
    179        }
    180
    181        /**
    182         * Converts a string representing an integer into its numerical value.
    183         * If this string does not represent a valid integer value, value of 0
    184         * is returned.
    185         *
    186         * @param value  the string.
    187         *
    188         * @return the value.
    189         */
    190        protected int stringToInt(String value) {
    191            value = value.trim();
    192            if (value.length() == 0) {
    193                return 0;
    194            }
    195            else {
    196                try {
    197                    return Integer.parseInt(value);
    198                }
    199                catch (NumberFormatException e) {
    200                    return 0;
    201                }
    202            }
    203        }
    204
    205        /**
    206         * Calls super removeNotify and removes all subcomponents from this panel.
    207         */
    208        public void removeNotify() {
    209            super.removeNotify();
    210            removeAll();
    211        }
    212
    213    }