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