Source for org.jfree.ui.InsetsChooserPanel

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2008, 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-2008, 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.8 2008/12/18 09:57:32 mungady 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:  * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
  48:  *               Jess Thrysoee (DG);
  49:  *
  50:  */
  51: 
  52: package org.jfree.ui;
  53: 
  54: import java.awt.BorderLayout;
  55: import java.awt.GridBagConstraints;
  56: import java.awt.GridBagLayout;
  57: import java.awt.Insets;
  58: import java.util.ResourceBundle;
  59: 
  60: import javax.swing.JLabel;
  61: import javax.swing.JPanel;
  62: import javax.swing.JTextField;
  63: import javax.swing.border.TitledBorder;
  64: 
  65: import org.jfree.util.ResourceBundleWrapper;
  66: 
  67: /**
  68:  * A component for editing an instance of the Insets class.
  69:  *
  70:  * @author Andrzej Porebski
  71:  */
  72: public class InsetsChooserPanel extends JPanel {
  73: 
  74:     /** A text field for the 'top' setting. */
  75:     private JTextField topValueEditor;
  76: 
  77:     /** A text field for the 'left' setting. */
  78:     private JTextField leftValueEditor;
  79: 
  80:     /** A text field for the 'bottom' setting. */
  81:     private JTextField bottomValueEditor;
  82: 
  83:     /** A text field for the 'right' setting. */
  84:     private JTextField rightValueEditor;
  85: 
  86:     /** The resourceBundle for the localization. */
  87:     protected static ResourceBundle localizationResources
  88:             = ResourceBundleWrapper.getBundle(
  89:                     "org.jfree.ui.LocalizationBundle");
  90: 
  91:     /**
  92:      * Creates a chooser panel that allows manipulation of Insets values.
  93:      * The values are initialized to the empty insets (0,0,0,0).
  94:      */
  95:     public InsetsChooserPanel() {
  96:         this(new Insets(0, 0, 0, 0));
  97:     }
  98: 
  99:     /**
 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: }