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.4 2005/11/16 15:58:41 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:
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",
070: "16", "18", "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 = ResourceBundle
086: .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
096: .getLocalGraphicsEnvironment();
097: final String[] fonts = g.getAvailableFontFamilyNames();
098:
099: setLayout(new BorderLayout());
100: final JPanel right = new JPanel(new BorderLayout());
101:
102: final JPanel fontPanel = new JPanel(new BorderLayout());
103: fontPanel.setBorder(BorderFactory.createTitledBorder(
104: BorderFactory.createEtchedBorder(),
105: localizationResources.getString("Font")));
106: this .fontlist = new JList(fonts);
107: final JScrollPane fontpane = new JScrollPane(this .fontlist);
108: fontpane.setBorder(BorderFactory.createEtchedBorder());
109: fontPanel.add(fontpane);
110: add(fontPanel);
111:
112: final JPanel sizePanel = new JPanel(new BorderLayout());
113: sizePanel.setBorder(BorderFactory.createTitledBorder(
114: BorderFactory.createEtchedBorder(),
115: localizationResources.getString("Size")));
116: this .sizelist = new JList(SIZES);
117: final JScrollPane sizepane = new JScrollPane(this .sizelist);
118: sizepane.setBorder(BorderFactory.createEtchedBorder());
119: sizePanel.add(sizepane);
120:
121: final JPanel attributes = new JPanel(new GridLayout(1, 2));
122: this .bold = new JCheckBox(localizationResources
123: .getString("Bold"));
124: this .italic = new JCheckBox(localizationResources
125: .getString("Italic"));
126: attributes.add(this .bold);
127: attributes.add(this .italic);
128: attributes.setBorder(BorderFactory.createTitledBorder(
129: BorderFactory.createEtchedBorder(),
130: localizationResources.getString("Attributes")));
131:
132: right.add(sizePanel, BorderLayout.CENTER);
133: right.add(attributes, BorderLayout.SOUTH);
134:
135: add(right, BorderLayout.EAST);
136:
137: setSelectedFont(font);
138: }
139:
140: /**
141: * Returns a Font object representing the selection in the panel.
142: *
143: * @return the font.
144: */
145: public Font getSelectedFont() {
146: return new Font(getSelectedName(), getSelectedStyle(),
147: getSelectedSize());
148: }
149:
150: /**
151: * Returns the selected name.
152: *
153: * @return the name.
154: */
155: public String getSelectedName() {
156: return (String) this .fontlist.getSelectedValue();
157: }
158:
159: /**
160: * Returns the selected style.
161: *
162: * @return the style.
163: */
164: public int getSelectedStyle() {
165: if (this .bold.isSelected() && this .italic.isSelected()) {
166: return Font.BOLD + Font.ITALIC;
167: }
168: if (this .bold.isSelected()) {
169: return Font.BOLD;
170: }
171: if (this .italic.isSelected()) {
172: return Font.ITALIC;
173: } else {
174: return Font.PLAIN;
175: }
176: }
177:
178: /**
179: * Returns the selected size.
180: *
181: * @return the size.
182: */
183: public int getSelectedSize() {
184: final String selected = (String) this .sizelist
185: .getSelectedValue();
186: if (selected != null) {
187: return Integer.parseInt(selected);
188: } else {
189: return 10;
190: }
191: }
192:
193: /**
194: * Initializes the contents of the dialog from the given font
195: * object.
196: *
197: * @param font the font from which to read the properties.
198: */
199: public void setSelectedFont(final Font font) {
200: if (font == null) {
201: throw new NullPointerException();
202: }
203: this .bold.setSelected(font.isBold());
204: this .italic.setSelected(font.isItalic());
205:
206: final String fontName = font.getName();
207: ListModel model = this .fontlist.getModel();
208: this .fontlist.clearSelection();
209: for (int i = 0; i < model.getSize(); i++) {
210: if (fontName.equals(model.getElementAt(i))) {
211: this .fontlist.setSelectedIndex(i);
212: break;
213: }
214: }
215:
216: final String fontSize = String.valueOf(font.getSize());
217: model = this .sizelist.getModel();
218: this .sizelist.clearSelection();
219: for (int i = 0; i < model.getSize(); i++) {
220: if (fontSize.equals(model.getElementAt(i))) {
221: this.sizelist.setSelectedIndex(i);
222: break;
223: }
224: }
225: }
226: }
|