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