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.Font; 032import java.util.ResourceBundle; 033import javax.swing.JTextField; 034 035/** 036 * A field for displaying a font selection. The display field itself is 037 * read-only, to the developer must provide another mechanism to allow the 038 * user to change the font. 039 */ 040public class FontDisplayField extends JTextField { 041 042 /** The current font. */ 043 private Font displayFont; 044 045 /** The resourceBundle for the localization. */ 046 protected static final ResourceBundle localizationResources = 047 ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle"); 048 049 /** 050 * Standard constructor - builds a FontDescriptionField initialised with 051 * the specified font. 052 * 053 * @param font the font. 054 */ 055 public FontDisplayField(Font font) { 056 super(""); 057 setDisplayFont(font); 058 setEnabled(false); 059 } 060 061 /** 062 * Returns the current font. 063 * 064 * @return the font. 065 */ 066 public Font getDisplayFont() { 067 return this.displayFont; 068 } 069 070 /** 071 * Sets the font. 072 * 073 * @param font the font. 074 */ 075 public void setDisplayFont(Font font) { 076 this.displayFont = font; 077 setText(fontToString(this.displayFont)); 078 } 079 080 /** 081 * Returns a string representation of the specified font. 082 * 083 * @param font the font. 084 * 085 * @return a string describing the font. 086 */ 087 private String fontToString(Font font) { 088 if (font != null) { 089 return font.getFontName() + ", " + font.getSize(); 090 } 091 else { 092 return localizationResources.getString("No_Font_Selected"); 093 } 094 } 095 096} 097