KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > InsetsChooserPanel


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------
28  * InsetsChooserPanel.java
29  * -----------------------
30  * (C) Copyright 2000-2005, by Andrzej Porebski and Contributors.
31  *
32  * Original Author: Andrzej Porebski;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  * Arnaud Lelievre;
35  *
36  * $Id: InsetsChooserPanel.java,v 1.6 2005/11/16 15:58:41 taqua Exp $
37  *
38  * Changes (from 7-Nov-2001)
39  * -------------------------
40  * 07-Nov-2001 : Added to com.jrefinery.ui package (DG);
41  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
42  * 03-Feb-2003 : Added Math.abs() to ensure no negative insets can be set (DG);
43  * 08-Sep-2003 : Added internationalization via use of properties
44  * resourceBundle (RFE 690236) (AL);
45  * 07-Oct-2005 : Renamed getInsets() --> getInsetsValue() to avoid conflict
46  * with JComponent's getInsets() (DG);
47  *
48  */

49
50 package org.jfree.ui;
51
52 import java.awt.BorderLayout JavaDoc;
53 import java.awt.GridBagConstraints JavaDoc;
54 import java.awt.GridBagLayout JavaDoc;
55 import java.awt.Insets JavaDoc;
56 import java.util.ResourceBundle JavaDoc;
57
58 import javax.swing.JLabel JavaDoc;
59 import javax.swing.JPanel JavaDoc;
60 import javax.swing.JTextField JavaDoc;
61 import javax.swing.border.TitledBorder JavaDoc;
62
63 /**
64  * A component for editing an instance of the Insets class.
65  *
66  * @author Andrzej Porebski
67  */

68 public class InsetsChooserPanel extends JPanel JavaDoc {
69
70     /** A text field for the 'top' setting. */
71     private JTextField JavaDoc topValueEditor;
72
73     /** A text field for the 'left' setting. */
74     private JTextField JavaDoc leftValueEditor;
75
76     /** A text field for the 'bottom' setting. */
77     private JTextField JavaDoc bottomValueEditor;
78
79     /** A text field for the 'right' setting. */
80     private JTextField JavaDoc rightValueEditor;
81
82     /** The resourceBundle for the localization. */
83     protected static ResourceBundle JavaDoc localizationResources =
84         ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
85
86     /**
87      * Creates a chooser panel that allows manipulation of Insets values.
88      * The values are initialized to the empty insets (0,0,0,0).
89      */

90     public InsetsChooserPanel() {
91         this(new Insets JavaDoc(0, 0, 0, 0));
92     }
93
94     /**
95      * Creates a chooser panel that allows manipulation of Insets values.
96      * The values are initialized to the current values of provided insets.
97      *
98      * @param current the insets.
99      */

100     public InsetsChooserPanel(Insets JavaDoc current) {
101         current = (current == null) ? new Insets JavaDoc(0, 0, 0, 0) : current;
102
103         this.topValueEditor = new JTextField JavaDoc(new IntegerDocument(), ""
104                 + current.top, 0);
105         this.leftValueEditor = new JTextField JavaDoc(new IntegerDocument(), ""
106                 + current.left, 0);
107         this.bottomValueEditor = new JTextField JavaDoc(new IntegerDocument(), ""
108                 + current.bottom, 0);
109         this.rightValueEditor = new JTextField JavaDoc(new IntegerDocument(), ""
110                 + current.right, 0);
111
112         final JPanel JavaDoc panel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
113         panel.setBorder(
114                 new TitledBorder JavaDoc(localizationResources.getString("Insets")));
115
116         // First row
117
panel.add(new JLabel JavaDoc(localizationResources.getString("Top")),
118             new GridBagConstraints JavaDoc(1, 0, 3, 1, 0.0, 0.0,
119             GridBagConstraints.CENTER, GridBagConstraints.NONE,
120             new Insets JavaDoc(0, 0, 0, 0), 0, 0));
121
122         // Second row
123
panel.add(new JLabel JavaDoc(" "), new GridBagConstraints JavaDoc(1, 1, 1, 1, 0.0, 0.0,
124             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
125             new Insets JavaDoc(0, 12, 0, 12), 8, 0));
126         panel.add(this.topValueEditor, new GridBagConstraints JavaDoc(2, 1, 1, 1, 0.0,
127             0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
128             new Insets JavaDoc(0, 0, 0, 0), 0, 0));
129         panel.add(new JLabel JavaDoc(" "), new GridBagConstraints JavaDoc(3, 1, 1, 1, 0.0, 0.0,
130             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
131             new Insets JavaDoc(0, 12, 0, 11), 8, 0));
132         
133         // Third row
134
panel.add(new JLabel JavaDoc(localizationResources.getString("Left")),
135             new GridBagConstraints JavaDoc(0, 2, 1, 1, 0.0, 0.0,
136             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
137             new Insets JavaDoc(0, 4, 0, 4), 0, 0));
138         panel.add(this.leftValueEditor, new GridBagConstraints JavaDoc(1, 2, 1, 1,
139             0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
140             new Insets JavaDoc(0, 0, 0, 0), 0, 0));
141         panel.add(new JLabel JavaDoc(" "), new GridBagConstraints JavaDoc(2, 2, 1, 1, 0.0, 0.0,
142             GridBagConstraints.CENTER, GridBagConstraints.NONE,
143             new Insets JavaDoc(0, 12, 0, 12), 8, 0));
144         panel.add(this.rightValueEditor, new GridBagConstraints JavaDoc(3, 2, 1, 1,
145             0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
146             new Insets JavaDoc(0, 0, 0, 0), 0, 0));
147         panel.add(new JLabel JavaDoc(localizationResources.getString("Right")),
148             new GridBagConstraints JavaDoc(4, 2, 1, 1, 0.0, 0.0,
149             GridBagConstraints.CENTER, GridBagConstraints.NONE,
150             new Insets JavaDoc(0, 4, 0, 4), 0, 0));
151         
152         // Fourth row
153
panel.add(this.bottomValueEditor, new GridBagConstraints JavaDoc(2, 3, 1, 1,
154             0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
155             new Insets JavaDoc(0, 0, 0, 0), 0, 0));
156         
157         // Fifth row
158
panel.add(new JLabel JavaDoc(localizationResources.getString("Bottom")),
159             new GridBagConstraints JavaDoc(1, 4, 3, 1, 0.0, 0.0,
160             GridBagConstraints.CENTER, GridBagConstraints.NONE,
161             new Insets JavaDoc(0, 0, 0, 0), 0, 0));
162         setLayout(new BorderLayout JavaDoc());
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 JavaDoc getInsetsValue() {
174         return new Insets JavaDoc(
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 JavaDoc 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 JavaDoc 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 }
214
Popular Tags