Source for org.jfree.ui.FontChooserPanel

   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:  * FontChooserPanel.java
  29:  * ---------------------
  30:  * (C) Copyright 2000-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Arnaud Lelievre;
  34:  *
  35:  * $Id: FontChooserPanel.java,v 1.6 2008/12/18 09:57:32 mungady Exp $
  36:  *
  37:  * Changes (from 26-Oct-2001)
  38:  * --------------------------
  39:  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
  40:  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
  42:  * 21-Feb-2004 : The FontParameter of the constructor was never used (TM);
  43:  * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
  44:  *               Jess Thrysoee (DG);
  45:  *
  46:  */
  47: 
  48: package org.jfree.ui;
  49: 
  50: import java.awt.BorderLayout;
  51: import java.awt.Font;
  52: import java.awt.GraphicsEnvironment;
  53: import java.awt.GridLayout;
  54: import java.util.ResourceBundle;
  55: 
  56: import javax.swing.BorderFactory;
  57: import javax.swing.JCheckBox;
  58: import javax.swing.JList;
  59: import javax.swing.JPanel;
  60: import javax.swing.JScrollPane;
  61: import javax.swing.ListModel;
  62: 
  63: import org.jfree.util.ResourceBundleWrapper;
  64: 
  65: /**
  66:  * A panel for choosing a font from the available system fonts - still a bit of
  67:  * a hack at the moment, but good enough for demonstration applications.
  68:  *
  69:  * @author David Gilbert
  70:  */
  71: public class FontChooserPanel extends JPanel {
  72: 
  73:     /** The font sizes that can be selected. */
  74:     public static final String[] SIZES = {"9", "10", "11", "12", "14", "16",
  75:             "18", "20", "22", "24", "28", "36", "48", "72"};
  76: 
  77:     /** The list of fonts. */
  78:     private JList fontlist;
  79: 
  80:     /** The list of sizes. */
  81:     private JList sizelist;
  82: 
  83:     /** The checkbox that indicates whether the font is bold. */
  84:     private JCheckBox bold;
  85: 
  86:     /** The checkbox that indicates whether or not the font is italic. */
  87:     private JCheckBox italic;
  88: 
  89:     /** The resourceBundle for the localization. */
  90:     protected static ResourceBundle localizationResources =
  91:         ResourceBundleWrapper.getBundle("org.jfree.ui.LocalizationBundle");
  92: 
  93:     /**
  94:      * Standard constructor - builds a FontChooserPanel initialised with the
  95:      * specified font.
  96:      *
  97:      * @param font  the initial font to display.
  98:      */
  99:     public FontChooserPanel(final Font font) {
 100: 
 101:         final GraphicsEnvironment g
 102:                 = GraphicsEnvironment.getLocalGraphicsEnvironment();
 103:         final String[] fonts = g.getAvailableFontFamilyNames();
 104: 
 105:         setLayout(new BorderLayout());
 106:         final JPanel right = new JPanel(new BorderLayout());
 107: 
 108:         final JPanel fontPanel = new JPanel(new BorderLayout());
 109:         fontPanel.setBorder(BorderFactory.createTitledBorder(
 110:                             BorderFactory.createEtchedBorder(),
 111:                             localizationResources.getString("Font")));
 112:         this.fontlist = new JList(fonts);
 113:         final JScrollPane fontpane = new JScrollPane(this.fontlist);
 114:         fontpane.setBorder(BorderFactory.createEtchedBorder());
 115:         fontPanel.add(fontpane);
 116:         add(fontPanel);
 117: 
 118:         final JPanel sizePanel = new JPanel(new BorderLayout());
 119:         sizePanel.setBorder(BorderFactory.createTitledBorder(
 120:                             BorderFactory.createEtchedBorder(),
 121:                             localizationResources.getString("Size")));
 122:         this.sizelist = new JList(SIZES);
 123:         final JScrollPane sizepane = new JScrollPane(this.sizelist);
 124:         sizepane.setBorder(BorderFactory.createEtchedBorder());
 125:         sizePanel.add(sizepane);
 126: 
 127:         final JPanel attributes = new JPanel(new GridLayout(1, 2));
 128:         this.bold = new JCheckBox(localizationResources.getString("Bold"));
 129:         this.italic = new JCheckBox(localizationResources.getString("Italic"));
 130:         attributes.add(this.bold);
 131:         attributes.add(this.italic);
 132:         attributes.setBorder(BorderFactory.createTitledBorder(
 133:                 BorderFactory.createEtchedBorder(),
 134:                 localizationResources.getString("Attributes")));
 135: 
 136:         right.add(sizePanel, BorderLayout.CENTER);
 137:         right.add(attributes, BorderLayout.SOUTH);
 138: 
 139:         add(right, BorderLayout.EAST);
 140: 
 141:         setSelectedFont(font);
 142:     }
 143: 
 144:     /**
 145:      * Returns a Font object representing the selection in the panel.
 146:      *
 147:      * @return the font.
 148:      */
 149:     public Font getSelectedFont() {
 150:         return new Font(getSelectedName(), getSelectedStyle(),
 151:                 getSelectedSize());
 152:     }
 153: 
 154:     /**
 155:      * Returns the selected name.
 156:      *
 157:      * @return the name.
 158:      */
 159:     public String getSelectedName() {
 160:         return (String) this.fontlist.getSelectedValue();
 161:     }
 162: 
 163:     /**
 164:      * Returns the selected style.
 165:      *
 166:      * @return the style.
 167:      */
 168:     public int getSelectedStyle() {
 169:         if (this.bold.isSelected() && this.italic.isSelected()) {
 170:             return Font.BOLD + Font.ITALIC;
 171:         }
 172:         if (this.bold.isSelected()) {
 173:             return Font.BOLD;
 174:         }
 175:         if (this.italic.isSelected()) {
 176:             return Font.ITALIC;
 177:         }
 178:         else {
 179:             return Font.PLAIN;
 180:         }
 181:     }
 182: 
 183:     /**
 184:      * Returns the selected size.
 185:      *
 186:      * @return the size.
 187:      */
 188:     public int getSelectedSize() {
 189:         final String selected = (String) this.sizelist.getSelectedValue();
 190:         if (selected != null) {
 191:             return Integer.parseInt(selected);
 192:         }
 193:         else {
 194:             return 10;
 195:         }
 196:     }
 197: 
 198:     /**
 199:      * Initializes the contents of the dialog from the given font
 200:      * object.
 201:      *
 202:      * @param font the font from which to read the properties.
 203:      */
 204:     public void setSelectedFont (final Font font) {
 205:         if (font == null) {
 206:             throw new NullPointerException();
 207:         }
 208:         this.bold.setSelected(font.isBold());
 209:         this.italic.setSelected(font.isItalic());
 210: 
 211:         final String fontName = font.getName();
 212:         ListModel model = this.fontlist.getModel();
 213:         this.fontlist.clearSelection();
 214:         for (int i = 0; i < model.getSize(); i++) {
 215:             if (fontName.equals(model.getElementAt(i))) {
 216:                 this.fontlist.setSelectedIndex(i);
 217:                 break;
 218:             }
 219:         }
 220: 
 221:         final String fontSize = String.valueOf(font.getSize());
 222:         model = this.sizelist.getModel();
 223:         this.sizelist.clearSelection();
 224:         for (int i = 0; i < model.getSize(); i++) {
 225:             if (fontSize.equals(model.getElementAt(i))) {
 226:                 this.sizelist.setSelectedIndex(i);
 227:                 break;
 228:             }
 229:         }
 230:     }
 231: }