    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.7 2007/11/02 17:50:36 taqua 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    import javax.swing.JLabel;
    058    import javax.swing.JPanel;
    059    import javax.swing.JTextField;
    060    import javax.swing.border.TitledBorder;
    061
    062    /**
    063     * A component for editing an instance of the Insets class.
    064     *
    065     * @author Andrzej Porebski
    066     */
    067    public class InsetsChooserPanel extends JPanel {
    068
    069        /** A text field for the 'top' setting. */
    070        private JTextField topValueEditor;
    071
    072        /** A text field for the 'left' setting. */
    073        private JTextField leftValueEditor;
    074
    075        /** A text field for the 'bottom' setting. */
    076        private JTextField bottomValueEditor;
    077
    078        /** A text field for the 'right' setting. */
    079        private JTextField rightValueEditor;
    080
    081        /** The resourceBundle for the localization. */
    082        protected static ResourceBundle localizationResources =
    083            ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
    084
    085        /**
    086         * Creates a chooser panel that allows manipulation of Insets values.
    087         * The values are initialized to the empty insets (0,0,0,0).
    088         */
    089        public InsetsChooserPanel() {
    090            this(new Insets(0, 0, 0, 0));
    091        }
    092
    093        /**
    094         * Creates a chooser panel that allows manipulation of Insets values.
    095         * The values are initialized to the current values of provided insets.
    096         *
    097         * @param current  the insets.
    098         */
    099        public InsetsChooserPanel(Insets current) {
    100            current = (current == null) ? new Insets(0, 0, 0, 0) : current;
    101
    102            this.topValueEditor = new JTextField(new IntegerDocument(), ""
    103                    + current.top, 0);
    104            this.leftValueEditor = new JTextField(new IntegerDocument(), ""
    105                    + current.left, 0);
    106            this.bottomValueEditor = new JTextField(new IntegerDocument(), ""
    107                    + current.bottom, 0);
    108            this.rightValueEditor = new JTextField(new IntegerDocument(), ""
    109                    + current.right, 0);
    110
    111            final JPanel panel = new JPanel(new GridBagLayout());
    112            panel.setBorder(
    113                    new TitledBorder(localizationResources.getString("Insets")));
    114
    115            // First row
    116            panel.add(new JLabel(localizationResources.getString("Top")),
    117                new GridBagConstraints(1, 0, 3, 1, 0.0, 0.0,
    118                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    119                new Insets(0, 0, 0, 0), 0, 0));
    120
    121            // Second row
    122            panel.add(new JLabel(" "), new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
    123                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    124                new Insets(0, 12, 0, 12), 8, 0));
    125            panel.add(this.topValueEditor, new GridBagConstraints(2, 1, 1, 1, 0.0,
    126                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    127                new Insets(0, 0, 0, 0), 0, 0));
    128            panel.add(new JLabel(" "),  new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
    129                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    130                new Insets(0, 12, 0, 11), 8, 0));
    131
    132            // Third row
    133            panel.add(new JLabel(localizationResources.getString("Left")),
    134                new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
    135                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    136                new Insets(0, 4, 0, 4), 0, 0));
    137            panel.add(this.leftValueEditor, new GridBagConstraints(1, 2, 1, 1,
    138                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    139                new Insets(0, 0, 0, 0), 0, 0));
    140            panel.add(new JLabel(" "), new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
    141                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    142                new Insets(0, 12, 0, 12), 8, 0));
    143            panel.add(this.rightValueEditor, new GridBagConstraints(3, 2, 1, 1,
    144                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    145                new Insets(0, 0, 0, 0), 0, 0));
    146            panel.add(new JLabel(localizationResources.getString("Right")),
    147                new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
    148                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    149                new Insets(0, 4, 0, 4), 0, 0));
    150
    151            // Fourth row
    152            panel.add(this.bottomValueEditor, new GridBagConstraints(2, 3, 1, 1,
    153                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    154                new Insets(0, 0, 0, 0), 0, 0));
    155
    156            // Fifth row
    157            panel.add(new JLabel(localizationResources.getString("Bottom")),
    158                new GridBagConstraints(1, 4, 3, 1, 0.0, 0.0,
    159                GridBagConstraints.CENTER, GridBagConstraints.NONE,
    160                new Insets(0, 0, 0, 0), 0, 0));
    161            setLayout(new BorderLayout());
    162            add(panel, BorderLayout.CENTER);
    163
    164        }
    165
    166        /**
    167         * Returns a new <code>Insets</code> instance to match the values entered
    168         * on the panel.
    169         *
    170         * @return The insets.
    171         */
    172        public Insets getInsetsValue() {
    173            return new Insets(
    174                Math.abs(stringToInt(this.topValueEditor.getText())),
    175                Math.abs(stringToInt(this.leftValueEditor.getText())),
    176                Math.abs(stringToInt(this.bottomValueEditor.getText())),
    177                Math.abs(stringToInt(this.rightValueEditor.getText())));
    178        }
    179
    180        /**
    181         * Converts a string representing an integer into its numerical value.
    182         * If this string does not represent a valid integer value, value of 0
    183         * is returned.
    184         *
    185         * @param value  the string.
    186         *
    187         * @return the value.
    188         */
    189        protected int stringToInt(String value) {
    190            value = value.trim();
    191            if (value.length() == 0) {
    192                return 0;
    193            }
    194            else {
    195                try {
    196                    return Integer.parseInt(value);
    197                }
    198                catch (NumberFormatException e) {
    199                    return 0;
    200                }
    201            }
    202        }
    203
    204        /**
    205         * Calls super removeNotify and removes all subcomponents from this panel.
    206         */
    207        public void removeNotify() {
    208            super.removeNotify();
    209            removeAll();
    210        }
    211
    212    }