5253N/A/*
5253N/A * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
5253N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5253N/A *
5253N/A * This code is free software; you can redistribute it and/or modify it
5253N/A * under the terms of the GNU General Public License version 2 only, as
5253N/A * published by the Free Software Foundation. Oracle designates this
5253N/A * particular file as subject to the "Classpath" exception as provided
5253N/A * by Oracle in the LICENSE file that accompanied this code.
5253N/A *
5253N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5253N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5253N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5253N/A * version 2 for more details (a copy is included in the LICENSE file that
5253N/A * accompanied this code).
5253N/A *
5253N/A * You should have received a copy of the GNU General Public License version
5253N/A * 2 along with this work; if not, write to the Free Software Foundation,
5253N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5253N/A *
5253N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5253N/A * or visit www.oracle.com if you need additional information or have any
5253N/A * questions.
5253N/A */
5253N/A
5253N/Apackage com.sun.beans.editors;
5253N/A
5253N/Aimport java.awt.*;
5253N/Aimport java.beans.*;
5253N/A
5253N/Apublic class FontEditor extends Panel implements java.beans.PropertyEditor {
5253N/A private static final long serialVersionUID = 6732704486002715933L;
5253N/A
5253N/A public FontEditor() {
5253N/A setLayout(null);
5253N/A
5253N/A toolkit = Toolkit.getDefaultToolkit();
5253N/A fonts = toolkit.getFontList();
5253N/A
5253N/A familyChoser = new Choice();
5253N/A for (int i = 0; i < fonts.length; i++) {
5253N/A familyChoser.addItem(fonts[i]);
5253N/A }
5253N/A add(familyChoser);
5253N/A familyChoser.reshape(20, 5, 100, 30);
5253N/A
5253N/A styleChoser = new Choice();
5253N/A for (int i = 0; i < styleNames.length; i++) {
5253N/A styleChoser.addItem(styleNames[i]);
5253N/A }
5253N/A add(styleChoser);
5253N/A styleChoser.reshape(145, 5, 70, 30);
5253N/A
5253N/A sizeChoser = new Choice();
5253N/A for (int i = 0; i < pointSizes.length; i++) {
5253N/A sizeChoser.addItem("" + pointSizes[i]);
5253N/A }
5253N/A add(sizeChoser);
5253N/A sizeChoser.reshape(220, 5, 70, 30);
5253N/A
5253N/A resize(300,40);
5253N/A }
5253N/A
5253N/A
5253N/A public Dimension preferredSize() {
5253N/A return new Dimension(300, 40);
5253N/A }
5253N/A
5253N/A public void setValue(Object o) {
5253N/A font = (Font) o;
5253N/A if (this.font == null)
5253N/A return;
5253N/A
5253N/A changeFont(font);
5253N/A // Update the current GUI choices.
5253N/A for (int i = 0; i < fonts.length; i++) {
5253N/A if (fonts[i].equals(font.getFamily())) {
5253N/A familyChoser.select(i);
5253N/A break;
5253N/A }
5253N/A }
5253N/A for (int i = 0; i < styleNames.length; i++) {
5253N/A if (font.getStyle() == styles[i]) {
5253N/A styleChoser.select(i);
5253N/A break;
5253N/A }
5253N/A }
5253N/A for (int i = 0; i < pointSizes.length; i++) {
5253N/A if (font.getSize() <= pointSizes[i]) {
5253N/A sizeChoser.select(i);
5253N/A break;
5253N/A }
5253N/A }
5253N/A }
5253N/A
5253N/A private void changeFont(Font f) {
5253N/A font = f;
5253N/A if (sample != null) {
5253N/A remove(sample);
5253N/A }
5253N/A sample = new Label(sampleText);
5253N/A sample.setFont(font);
5253N/A add(sample);
5253N/A Component p = getParent();
5253N/A if (p != null) {
5253N/A p.invalidate();
5253N/A p.layout();
5253N/A }
5253N/A invalidate();
5253N/A layout();
5253N/A repaint();
5253N/A support.firePropertyChange("", null, null);
5253N/A }
5253N/A
5253N/A public Object getValue() {
5253N/A return (font);
5253N/A }
5253N/A
5253N/A public String getJavaInitializationString() {
5253N/A if (this.font == null)
5253N/A return "null";
5253N/A
5253N/A return "new java.awt.Font(\"" + font.getName() + "\", " +
5253N/A font.getStyle() + ", " + font.getSize() + ")";
5253N/A }
5253N/A
5253N/A public boolean action(Event e, Object arg) {
5253N/A String family = familyChoser.getSelectedItem();
5253N/A int style = styles[styleChoser.getSelectedIndex()];
5253N/A int size = pointSizes[sizeChoser.getSelectedIndex()];
5253N/A try {
5253N/A Font f = new Font(family, style, size);
5253N/A changeFont(f);
5253N/A } catch (Exception ex) {
5253N/A System.err.println("Couldn't create font " + family + "-" +
5253N/A styleNames[style] + "-" + size);
5253N/A }
5253N/A return (false);
5253N/A }
5253N/A
5253N/A
5253N/A public boolean isPaintable() {
5253N/A return true;
5253N/A }
5253N/A
5253N/A public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
5253N/A // Silent noop.
5253N/A Font oldFont = gfx.getFont();
5253N/A gfx.setFont(font);
5253N/A FontMetrics fm = gfx.getFontMetrics();
5253N/A int vpad = (box.height - fm.getAscent())/2;
5253N/A gfx.drawString(sampleText, 0, box.height-vpad);
5253N/A gfx.setFont(oldFont);
5253N/A }
5253N/A
5253N/A public String getAsText() {
5253N/A if (this.font == null) {
5253N/A return null;
5253N/A }
5253N/A StringBuilder sb = new StringBuilder();
5253N/A sb.append(this.font.getName());
5253N/A sb.append(' ');
5253N/A
5253N/A boolean b = this.font.isBold();
5253N/A if (b) {
5253N/A sb.append("BOLD");
5253N/A }
5253N/A boolean i = this.font.isItalic();
5253N/A if (i) {
5253N/A sb.append("ITALIC");
5253N/A }
5253N/A if (b || i) {
5253N/A sb.append(' ');
5253N/A }
5253N/A sb.append(this.font.getSize());
5253N/A return sb.toString();
5253N/A }
5253N/A
5253N/A public void setAsText(String text) throws IllegalArgumentException {
5253N/A setValue((text == null) ? null : Font.decode(text));
5253N/A }
5253N/A
5253N/A public String[] getTags() {
5253N/A return null;
5253N/A }
5253N/A
5253N/A public java.awt.Component getCustomEditor() {
5253N/A return this;
5253N/A }
5253N/A
5253N/A public boolean supportsCustomEditor() {
5253N/A return true;
5253N/A }
5253N/A
5253N/A public void addPropertyChangeListener(PropertyChangeListener l) {
5253N/A support.addPropertyChangeListener(l);
5253N/A }
5253N/A
5253N/A public void removePropertyChangeListener(PropertyChangeListener l) {
5253N/A support.removePropertyChangeListener(l);
5253N/A }
5253N/A
5253N/A private Font font;
5253N/A private Toolkit toolkit;
5253N/A private String sampleText = "Abcde...";
5253N/A
5253N/A private Label sample;
5253N/A private Choice familyChoser;
5253N/A private Choice styleChoser;
5253N/A private Choice sizeChoser;
5253N/A
5253N/A private String fonts[];
5253N/A private String[] styleNames = { "plain", "bold", "italic" };
5253N/A private int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC };
5253N/A private int[] pointSizes = { 3, 5, 8, 10, 12, 14, 18, 24, 36, 48 };
5253N/A
5253N/A private PropertyChangeSupport support = new PropertyChangeSupport(this);
5253N/A
5253N/A}