001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 */ 028 029package org.jfree.chart.ui; 030 031import java.awt.BorderLayout; 032import java.awt.Font; 033import java.awt.GraphicsEnvironment; 034import java.awt.GridLayout; 035import java.util.ResourceBundle; 036import javax.swing.BorderFactory; 037import javax.swing.JCheckBox; 038import javax.swing.JList; 039import javax.swing.JPanel; 040import javax.swing.JScrollPane; 041import javax.swing.ListModel; 042import org.jfree.chart.util.ResourceBundleWrapper; 043 044/** 045 * A panel for choosing a font from the available system fonts - still a bit of 046 * a hack at the moment, but good enough for demonstration applications. 047 */ 048public class FontChooserPanel extends JPanel { 049 050 /** The font sizes that can be selected. */ 051 public static final String[] SIZES = {"9", "10", "11", "12", "14", "16", 052 "18", "20", "22", "24", "28", "36", "48", "72"}; 053 054 /** The list of fonts. */ 055 private JList fontlist; 056 057 /** The list of sizes. */ 058 private JList sizelist; 059 060 /** The checkbox that indicates whether the font is bold. */ 061 private JCheckBox bold; 062 063 /** The checkbox that indicates whether or not the font is italic. */ 064 private JCheckBox italic; 065 066 /** The resourceBundle for the localization. */ 067 protected static ResourceBundle localizationResources = 068 ResourceBundleWrapper.getBundle("org.jfree.chart.ui.LocalizationBundle"); 069 070 /** 071 * Standard constructor - builds a FontChooserPanel initialised with the 072 * specified font. 073 * 074 * @param font the initial font to display. 075 */ 076 public FontChooserPanel(Font font) { 077 078 final GraphicsEnvironment g 079 = GraphicsEnvironment.getLocalGraphicsEnvironment(); 080 final String[] fonts = g.getAvailableFontFamilyNames(); 081 082 setLayout(new BorderLayout()); 083 final JPanel right = new JPanel(new BorderLayout()); 084 085 final JPanel fontPanel = new JPanel(new BorderLayout()); 086 fontPanel.setBorder(BorderFactory.createTitledBorder( 087 BorderFactory.createEtchedBorder(), 088 localizationResources.getString("Font"))); 089 this.fontlist = new JList(fonts); 090 final JScrollPane fontpane = new JScrollPane(this.fontlist); 091 fontpane.setBorder(BorderFactory.createEtchedBorder()); 092 fontPanel.add(fontpane); 093 add(fontPanel); 094 095 final JPanel sizePanel = new JPanel(new BorderLayout()); 096 sizePanel.setBorder(BorderFactory.createTitledBorder( 097 BorderFactory.createEtchedBorder(), 098 localizationResources.getString("Size"))); 099 this.sizelist = new JList(SIZES); 100 final JScrollPane sizepane = new JScrollPane(this.sizelist); 101 sizepane.setBorder(BorderFactory.createEtchedBorder()); 102 sizePanel.add(sizepane); 103 104 final JPanel attributes = new JPanel(new GridLayout(1, 2)); 105 this.bold = new JCheckBox(localizationResources.getString("Bold")); 106 this.italic = new JCheckBox(localizationResources.getString("Italic")); 107 attributes.add(this.bold); 108 attributes.add(this.italic); 109 attributes.setBorder(BorderFactory.createTitledBorder( 110 BorderFactory.createEtchedBorder(), 111 localizationResources.getString("Attributes"))); 112 113 right.add(sizePanel, BorderLayout.CENTER); 114 right.add(attributes, BorderLayout.SOUTH); 115 116 add(right, BorderLayout.EAST); 117 118 setSelectedFont(font); 119 } 120 121 /** 122 * Returns a Font object representing the selection in the panel. 123 * 124 * @return the font. 125 */ 126 public Font getSelectedFont() { 127 return new Font(getSelectedName(), getSelectedStyle(), 128 getSelectedSize()); 129 } 130 131 /** 132 * Returns the selected name. 133 * 134 * @return the name. 135 */ 136 public String getSelectedName() { 137 return (String) this.fontlist.getSelectedValue(); 138 } 139 140 /** 141 * Returns the selected style. 142 * 143 * @return the style. 144 */ 145 public int getSelectedStyle() { 146 if (this.bold.isSelected() && this.italic.isSelected()) { 147 return Font.BOLD + Font.ITALIC; 148 } 149 if (this.bold.isSelected()) { 150 return Font.BOLD; 151 } 152 if (this.italic.isSelected()) { 153 return Font.ITALIC; 154 } 155 else { 156 return Font.PLAIN; 157 } 158 } 159 160 /** 161 * Returns the selected size. 162 * 163 * @return the size. 164 */ 165 public int getSelectedSize() { 166 final String selected = (String) this.sizelist.getSelectedValue(); 167 if (selected != null) { 168 return Integer.parseInt(selected); 169 } 170 else { 171 return 10; 172 } 173 } 174 175 /** 176 * Initializes the contents of the dialog from the given font 177 * object. 178 * 179 * @param font the font from which to read the properties. 180 */ 181 public void setSelectedFont (Font font) { 182 if (font == null) { 183 throw new NullPointerException(); 184 } 185 this.bold.setSelected(font.isBold()); 186 this.italic.setSelected(font.isItalic()); 187 188 final String fontName = font.getName(); 189 ListModel model = this.fontlist.getModel(); 190 this.fontlist.clearSelection(); 191 for (int i = 0; i < model.getSize(); i++) { 192 if (fontName.equals(model.getElementAt(i))) { 193 this.fontlist.setSelectedIndex(i); 194 break; 195 } 196 } 197 198 final String fontSize = String.valueOf(font.getSize()); 199 model = this.sizelist.getModel(); 200 this.sizelist.clearSelection(); 201 for (int i = 0; i < model.getSize(); i++) { 202 if (fontSize.equals(model.getElementAt(i))) { 203 this.sizelist.setSelectedIndex(i); 204 break; 205 } 206 } 207 } 208} 209