    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     * FontChooserPanel.java
    029     * ---------------------
    030     * (C) Copyright 2000-2004, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   Arnaud Lelievre;
    034     *
    035     * $Id: FontChooserPanel.java,v 1.3 2005/10/18 13:18:34 mungady Exp $
    036     *
    037     * Changes (from 26-Oct-2001)
    038     * --------------------------
    039     * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
    040     * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    041     * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
    042     * 21-Feb-2004 : The FontParameter of the constructor was never used (TM);
    043     */
    044
    045    package org.jfree.ui;
    046
    047    import java.awt.BorderLayout;
    048    import java.awt.Font;
    049    import java.awt.GraphicsEnvironment;
    050    import java.awt.GridLayout;
    051    import java.util.ResourceBundle;
    052
    053    import javax.swing.BorderFactory;
    054    import javax.swing.JCheckBox;
    055    import javax.swing.JList;
    056    import javax.swing.JPanel;
    057    import javax.swing.JScrollPane;
    058    import javax.swing.ListModel;
    059
    060    /**
    061     * A panel for choosing a font from the available system fonts - still a bit of a hack at the
    062     * moment, but good enough for demonstration applications.
    063     *
    064     * @author David Gilbert
    065     */
    066    public class FontChooserPanel extends JPanel {
    067
    068        /** The font sizes that can be selected. */
    069        public static final String[] SIZES = {"9", "10", "11", "12", "14", "16", "18",
    070                                              "20", "22", "24", "28", "36", "48", "72"};
    071
    072        /** The list of fonts. */
    073        private JList fontlist;
    074
    075        /** The list of sizes. */
    076        private JList sizelist;
    077
    078        /** The checkbox that indicates whether the font is bold. */
    079        private JCheckBox bold;
    080
    081        /** The checkbox that indicates whether or not the font is italic. */
    082        private JCheckBox italic;
    083
    084        /** The resourceBundle for the localization. */
    085        protected static ResourceBundle localizationResources =
    086            ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
    087
    088        /**
    089         * Standard constructor - builds a FontChooserPanel initialised with the specified font.
    090         *
    091         * @param font  the initial font to display.
    092         */
    093        public FontChooserPanel(final Font font) {
    094
    095            final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
    096            final String[] fonts = g.getAvailableFontFamilyNames();
    097
    098            setLayout(new BorderLayout());
    099            final JPanel right = new JPanel(new BorderLayout());
    100
    101            final JPanel fontPanel = new JPanel(new BorderLayout());
    102            fontPanel.setBorder(BorderFactory.createTitledBorder(
    103                                BorderFactory.createEtchedBorder(),
    104                                localizationResources.getString("Font")));
    105            this.fontlist = new JList(fonts);
    106            final JScrollPane fontpane = new JScrollPane(this.fontlist);
    107            fontpane.setBorder(BorderFactory.createEtchedBorder());
    108            fontPanel.add(fontpane);
    109            add(fontPanel);
    110
    111            final JPanel sizePanel = new JPanel(new BorderLayout());
    112            sizePanel.setBorder(BorderFactory.createTitledBorder(
    113                                BorderFactory.createEtchedBorder(),
    114                                localizationResources.getString("Size")));
    115            this.sizelist = new JList(SIZES);
    116            final JScrollPane sizepane = new JScrollPane(this.sizelist);
    117            sizepane.setBorder(BorderFactory.createEtchedBorder());
    118            sizePanel.add(sizepane);
    119
    120            final JPanel attributes = new JPanel(new GridLayout(1, 2));
    121            this.bold = new JCheckBox(localizationResources.getString("Bold"));
    122            this.italic = new JCheckBox(localizationResources.getString("Italic"));
    123            attributes.add(this.bold);
    124            attributes.add(this.italic);
    125            attributes.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
    126                                 localizationResources.getString("Attributes")));
    127
    128            right.add(sizePanel, BorderLayout.CENTER);
    129            right.add(attributes, BorderLayout.SOUTH);
    130
    131            add(right, BorderLayout.EAST);
    132
    133            setSelectedFont(font);
    134        }
    135
    136        /**
    137         * Returns a Font object representing the selection in the panel.
    138         *
    139         * @return the font.
    140         */
    141        public Font getSelectedFont() {
    142            return new Font(getSelectedName(), getSelectedStyle(), getSelectedSize());
    143        }
    144
    145        /**
    146         * Returns the selected name.
    147         *
    148         * @return the name.
    149         */
    150        public String getSelectedName() {
    151            return (String) this.fontlist.getSelectedValue();
    152        }
    153
    154        /**
    155         * Returns the selected style.
    156         *
    157         * @return the style.
    158         */
    159        public int getSelectedStyle() {
    160            if (this.bold.isSelected() && this.italic.isSelected()) {
    161                return Font.BOLD + Font.ITALIC;
    162            }
    163            if (this.bold.isSelected()) {
    164                return Font.BOLD;
    165            }
    166            if (this.italic.isSelected()) {
    167                return Font.ITALIC;
    168            }
    169            else {
    170                return Font.PLAIN;
    171            }
    172        }
    173
    174        /**
    175         * Returns the selected size.
    176         *
    177         * @return the size.
    178         */
    179        public int getSelectedSize() {
    180            final String selected = (String) this.sizelist.getSelectedValue();
    181            if (selected != null) {
    182                return Integer.parseInt(selected);
    183            }
    184            else {
    185                return 10;
    186            }
    187        }
    188
    189        /**
    190         * Initializes the contents of the dialog from the given font
    191         * object.
    192         *
    193         * @param font the font from which to read the properties.
    194         */
    195        public void setSelectedFont (final Font font) {
    196            if (font == null) {
    197                throw new NullPointerException();
    198            }
    199            this.bold.setSelected(font.isBold());
    200            this.italic.setSelected(font.isItalic());
    201
    202            final String fontName = font.getName();
    203            ListModel model = this.fontlist.getModel();
    204            this.fontlist.clearSelection();
    205            for (int i = 0; i < model.getSize(); i++) {
    206                if (fontName.equals(model.getElementAt(i))) {
    207                    this.fontlist.setSelectedIndex(i);
    208                    break;
    209                }
    210            }
    211
    212            final String fontSize = String.valueOf(font.getSize());
    213            model = this.sizelist.getModel();
    214            this.sizelist.clearSelection();
    215            for (int i = 0; i < model.getSize(); i++) {
    216                if (fontSize.equals(model.getElementAt(i))) {
    217                    this.sizelist.setSelectedIndex(i);
    218                    break;
    219                }
    220            }
    221        }
    222    }