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