Source for org.jfree.ui.InsetsTextField

   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:  * InsetsTextField.java
  29:  * --------------------
  30:  * (C) Copyright 2000-2008, by Andrzej Porebski.
  31:  *
  32:  * Original Author:  Andrzej Porebski;
  33:  * Contributor(s):   Arnaud Lelievre;
  34:  *
  35:  * $Id: InsetsTextField.java,v 1.4 2008/12/18 09:57:32 mungady Exp $
  36:  *
  37:  * Changes (from 7-Nov-2001)
  38:  * -------------------------
  39:  * 07-Nov-2001 : Added to com.jrefinery.ui package (DG);
  40:  * 08-Sep-2003 : Added internationalization via use of properties
  41:  *               resourceBundle (RFE 690236) (AL);
  42:  * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
  43:  *               Jess Thrysoee (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.ui;
  48: 
  49: import java.awt.Insets;
  50: import java.util.ResourceBundle;
  51: 
  52: import javax.swing.JTextField;
  53: 
  54: import org.jfree.util.ResourceBundleWrapper;
  55: 
  56: /**
  57:  * A JTextField for displaying insets.
  58:  *
  59:  * @author Andrzej Porebski
  60:  */
  61: public class InsetsTextField extends JTextField {
  62: 
  63:     /** The resourceBundle for the localization. */
  64:     protected static ResourceBundle localizationResources
  65:             = ResourceBundleWrapper.getBundle(
  66:                     "org.jfree.ui.LocalizationBundle");
  67: 
  68:     /**
  69:      * Default constructor. Initializes this text field with formatted string
  70:      * describing provided insets.
  71:      *
  72:      * @param insets  the insets.
  73:      */
  74:     public InsetsTextField(final Insets insets) {
  75:         super();
  76:         setInsets(insets);
  77:         setEnabled(false);
  78:     }
  79: 
  80:     /**
  81:      * Returns a formatted string describing provided insets.
  82:      *
  83:      * @param insets  the insets.
  84:      *
  85:      * @return the string.
  86:      */
  87:     public String formatInsetsString(Insets insets) {
  88:         insets = (insets == null) ? new Insets(0, 0, 0, 0) : insets;
  89:         return
  90:             localizationResources.getString("T") + insets.top + ", "
  91:              + localizationResources.getString("L") + insets.left + ", "
  92:              + localizationResources.getString("B") + insets.bottom + ", "
  93:              + localizationResources.getString("R") + insets.right;
  94:     }
  95: 
  96:     /**
  97:      * Sets the text of this text field to the formatted string
  98:      * describing provided insets. If insets is null, empty insets
  99:      * (0,0,0,0) are used.
 100:      *
 101:      * @param insets  the insets.
 102:      */
 103:     public void setInsets(final Insets insets) {
 104:         setText(formatInsetsString(insets));
 105:     }
 106: 
 107: }