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.6 2005/11/16 15:58:41 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:
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 = ResourceBundle
084: .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(new TitledBorder(localizationResources
114: .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,
120: GridBagConstraints.NONE,
121: new Insets(0, 0, 0, 0), 0, 0));
122:
123: // Second row
124: panel.add(new JLabel(" "),
125: new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
126: GridBagConstraints.CENTER,
127: GridBagConstraints.BOTH, new Insets(0, 12, 0,
128: 12), 8, 0));
129: panel.add(this .topValueEditor, new GridBagConstraints(2, 1, 1,
130: 1, 0.0, 0.0, GridBagConstraints.CENTER,
131: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
132: 0, 0));
133: panel.add(new JLabel(" "),
134: new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
135: GridBagConstraints.CENTER,
136: GridBagConstraints.BOTH, new Insets(0, 12, 0,
137: 11), 8, 0));
138:
139: // Third row
140: panel.add(new JLabel(localizationResources.getString("Left")),
141: new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
142: GridBagConstraints.CENTER,
143: GridBagConstraints.BOTH,
144: new Insets(0, 4, 0, 4), 0, 0));
145: panel.add(this .leftValueEditor, new GridBagConstraints(1, 2, 1,
146: 1, 0.0, 0.0, GridBagConstraints.CENTER,
147: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
148: panel.add(new JLabel(" "),
149: new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
150: GridBagConstraints.CENTER,
151: GridBagConstraints.NONE, new Insets(0, 12, 0,
152: 12), 8, 0));
153: panel.add(this .rightValueEditor, new GridBagConstraints(3, 2,
154: 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
155: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
156: 0, 0));
157: panel.add(new JLabel(localizationResources.getString("Right")),
158: new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
159: GridBagConstraints.CENTER,
160: GridBagConstraints.NONE,
161: new Insets(0, 4, 0, 4), 0, 0));
162:
163: // Fourth row
164: panel.add(this .bottomValueEditor, new GridBagConstraints(2, 3,
165: 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
166: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
167: 0, 0));
168:
169: // Fifth row
170: panel.add(
171: new JLabel(localizationResources.getString("Bottom")),
172: new GridBagConstraints(1, 4, 3, 1, 0.0, 0.0,
173: GridBagConstraints.CENTER,
174: GridBagConstraints.NONE,
175: new Insets(0, 0, 0, 0), 0, 0));
176: setLayout(new BorderLayout());
177: add(panel, BorderLayout.CENTER);
178:
179: }
180:
181: /**
182: * Returns a new <code>Insets</code> instance to match the values entered
183: * on the panel.
184: *
185: * @return The insets.
186: */
187: public Insets getInsetsValue() {
188: return new Insets(Math.abs(stringToInt(this .topValueEditor
189: .getText())), Math.abs(stringToInt(this .leftValueEditor
190: .getText())), Math
191: .abs(stringToInt(this .bottomValueEditor.getText())),
192: Math.abs(stringToInt(this .rightValueEditor.getText())));
193: }
194:
195: /**
196: * Converts a string representing an integer into its numerical value.
197: * If this string does not represent a valid integer value, value of 0
198: * is returned.
199: *
200: * @param value the string.
201: *
202: * @return the value.
203: */
204: protected int stringToInt(String value) {
205: value = value.trim();
206: if (value.length() == 0) {
207: return 0;
208: } else {
209: try {
210: return Integer.parseInt(value);
211: } catch (NumberFormatException e) {
212: return 0;
213: }
214: }
215: }
216:
217: /**
218: * Calls super removeNotify and removes all subcomponents from this panel.
219: */
220: public void removeNotify() {
221: super.removeNotify();
222: removeAll();
223: }
224:
225: }
|