KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > FontChooserPanel


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, 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-2004, 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.4 2005/11/16 15:58:41 taqua 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  */

44
45 package org.jfree.ui;
46
47 import java.awt.BorderLayout JavaDoc;
48 import java.awt.Font JavaDoc;
49 import java.awt.GraphicsEnvironment JavaDoc;
50 import java.awt.GridLayout JavaDoc;
51 import java.util.ResourceBundle JavaDoc;
52
53 import javax.swing.BorderFactory JavaDoc;
54 import javax.swing.JCheckBox JavaDoc;
55 import javax.swing.JList JavaDoc;
56 import javax.swing.JPanel JavaDoc;
57 import javax.swing.JScrollPane JavaDoc;
58 import javax.swing.ListModel JavaDoc;
59
60 /**
61  * A panel for choosing a font from the available system fonts - still a bit of a hack at the
62  * moment, but good enough for demonstration applications.
63  *
64  * @author David Gilbert
65  */

66 public class FontChooserPanel extends JPanel JavaDoc {
67
68     /** The font sizes that can be selected. */
69     public static final String JavaDoc[] SIZES = {"9", "10", "11", "12", "14", "16", "18",
70                                           "20", "22", "24", "28", "36", "48", "72"};
71
72     /** The list of fonts. */
73     private JList JavaDoc fontlist;
74
75     /** The list of sizes. */
76     private JList JavaDoc sizelist;
77
78     /** The checkbox that indicates whether the font is bold. */
79     private JCheckBox JavaDoc bold;
80
81     /** The checkbox that indicates whether or not the font is italic. */
82     private JCheckBox JavaDoc italic;
83
84     /** The resourceBundle for the localization. */
85     protected static ResourceBundle JavaDoc localizationResources =
86         ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
87
88     /**
89      * Standard constructor - builds a FontChooserPanel initialised with the specified font.
90      *
91      * @param font the initial font to display.
92      */

93     public FontChooserPanel(final Font JavaDoc font) {
94
95         final GraphicsEnvironment JavaDoc g = GraphicsEnvironment.getLocalGraphicsEnvironment();
96         final String JavaDoc[] fonts = g.getAvailableFontFamilyNames();
97
98         setLayout(new BorderLayout JavaDoc());
99         final JPanel JavaDoc right = new JPanel JavaDoc(new BorderLayout JavaDoc());
100
101         final JPanel JavaDoc fontPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
102         fontPanel.setBorder(BorderFactory.createTitledBorder(
103                             BorderFactory.createEtchedBorder(),
104                             localizationResources.getString("Font")));
105         this.fontlist = new JList JavaDoc(fonts);
106         final JScrollPane JavaDoc fontpane = new JScrollPane JavaDoc(this.fontlist);
107         fontpane.setBorder(BorderFactory.createEtchedBorder());
108         fontPanel.add(fontpane);
109         add(fontPanel);
110
111         final JPanel JavaDoc sizePanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
112         sizePanel.setBorder(BorderFactory.createTitledBorder(
113                             BorderFactory.createEtchedBorder(),
114                             localizationResources.getString("Size")));
115         this.sizelist = new JList JavaDoc(SIZES);
116         final JScrollPane JavaDoc sizepane = new JScrollPane JavaDoc(this.sizelist);
117         sizepane.setBorder(BorderFactory.createEtchedBorder());
118         sizePanel.add(sizepane);
119
120         final JPanel JavaDoc attributes = new JPanel JavaDoc(new GridLayout JavaDoc(1, 2));
121         this.bold = new JCheckBox JavaDoc(localizationResources.getString("Bold"));
122         this.italic = new JCheckBox JavaDoc(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 JavaDoc getSelectedFont() {
142         return new Font JavaDoc(getSelectedName(), getSelectedStyle(), getSelectedSize());
143     }
144
145     /**
146      * Returns the selected name.
147      *
148      * @return the name.
149      */

150     public String JavaDoc getSelectedName() {
151         return (String JavaDoc) 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 JavaDoc selected = (String JavaDoc) 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 JavaDoc font) {
196         if (font == null) {
197             throw new NullPointerException JavaDoc();
198         }
199         this.bold.setSelected(font.isBold());
200         this.italic.setSelected(font.isItalic());
201
202         final String JavaDoc fontName = font.getName();
203         ListModel JavaDoc 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 JavaDoc 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 }
223
Popular Tags