1 /*
   2  * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.beans.editors;
  27 
  28 import java.awt.*;
  29 import java.beans.*;
  30 
  31 public class FontEditor extends Panel implements java.beans.PropertyEditor {
  32     private static final long serialVersionUID = 6732704486002715933L;
  33 
  34     public FontEditor() {
  35         setLayout(null);
  36 
  37         toolkit = Toolkit.getDefaultToolkit();
  38         fonts = toolkit.getFontList();
  39 
  40         familyChoser = new Choice();
  41         for (int i = 0; i < fonts.length; i++) {
  42             familyChoser.addItem(fonts[i]);
  43         }
  44         add(familyChoser);
  45         familyChoser.reshape(20, 5, 100, 30);
  46 
  47         styleChoser = new Choice();
  48         for (int i = 0; i < styleNames.length; i++) {
  49             styleChoser.addItem(styleNames[i]);
  50         }
  51         add(styleChoser);
  52         styleChoser.reshape(145, 5, 70, 30);
  53 
  54         sizeChoser = new Choice();
  55         for (int i = 0; i < pointSizes.length; i++) {
  56             sizeChoser.addItem("" + pointSizes[i]);
  57         }
  58         add(sizeChoser);
  59         sizeChoser.reshape(220, 5, 70, 30);
  60 
  61         resize(300,40);
  62     }
  63 
  64 
  65     public Dimension preferredSize() {
  66         return new Dimension(300, 40);
  67     }
  68 
  69     public void setValue(Object o) {
  70         font = (Font) o;
  71         if (this.font == null)
  72             return;
  73 
  74         changeFont(font);
  75         // Update the current GUI choices.
  76         for (int i = 0; i < fonts.length; i++) {
  77             if (fonts[i].equals(font.getFamily())) {
  78                 familyChoser.select(i);
  79                 break;
  80             }
  81         }
  82         for (int i = 0; i < styleNames.length; i++) {
  83             if (font.getStyle() == styles[i]) {
  84                 styleChoser.select(i);
  85                 break;
  86             }
  87         }
  88         for (int i = 0; i < pointSizes.length; i++) {
  89             if (font.getSize() <= pointSizes[i]) {
  90                 sizeChoser.select(i);
  91                 break;
  92             }
  93         }
  94     }
  95 
  96     private void changeFont(Font f) {
  97         font = f;
  98         if (sample != null) {
  99             remove(sample);
 100         }
 101         sample = new Label(sampleText);
 102         sample.setFont(font);
 103         add(sample);
 104         Component p = getParent();
 105         if (p != null) {
 106             p.invalidate();
 107             p.layout();
 108         }
 109         invalidate();
 110         layout();
 111         repaint();
 112         support.firePropertyChange("", null, null);
 113     }
 114 
 115     public Object getValue() {
 116         return (font);
 117     }
 118 
 119     public String getJavaInitializationString() {
 120         if (this.font == null)
 121             return "null";
 122 
 123         return "new java.awt.Font(\"" + font.getName() + "\", " +
 124                    font.getStyle() + ", " + font.getSize() + ")";
 125     }
 126 
 127     public boolean action(Event e, Object arg) {
 128         String family = familyChoser.getSelectedItem();
 129         int style = styles[styleChoser.getSelectedIndex()];
 130         int size = pointSizes[sizeChoser.getSelectedIndex()];
 131         try {
 132             Font f = new Font(family, style, size);
 133             changeFont(f);
 134         } catch (Exception ex) {
 135             System.err.println("Couldn't create font " + family + "-" +
 136                         styleNames[style] + "-" + size);
 137         }
 138         return (false);
 139     }
 140 
 141 
 142     public boolean isPaintable() {
 143         return true;
 144     }
 145 
 146     public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
 147         // Silent noop.
 148         Font oldFont = gfx.getFont();
 149         gfx.setFont(font);
 150         FontMetrics fm = gfx.getFontMetrics();
 151         int vpad = (box.height - fm.getAscent())/2;
 152         gfx.drawString(sampleText, 0, box.height-vpad);
 153         gfx.setFont(oldFont);
 154     }
 155 
 156     public String getAsText() {
 157         if (this.font == null) {
 158             return null;
 159         }
 160         StringBuilder sb = new StringBuilder();
 161         sb.append(this.font.getName());
 162         sb.append(' ');
 163 
 164         boolean b = this.font.isBold();
 165         if (b) {
 166             sb.append("BOLD");
 167         }
 168         boolean i = this.font.isItalic();
 169         if (i) {
 170             sb.append("ITALIC");
 171         }
 172         if (b || i) {
 173             sb.append(' ');
 174         }
 175         sb.append(this.font.getSize());
 176         return sb.toString();
 177     }
 178 
 179     public void setAsText(String text) throws IllegalArgumentException {
 180         setValue((text == null) ? null : Font.decode(text));
 181     }
 182 
 183     public String[] getTags() {
 184         return null;
 185     }
 186 
 187     public java.awt.Component getCustomEditor() {
 188         return this;
 189     }
 190 
 191     public boolean supportsCustomEditor() {
 192         return true;
 193     }
 194 
 195     public void addPropertyChangeListener(PropertyChangeListener l) {
 196         support.addPropertyChangeListener(l);
 197     }
 198 
 199     public void removePropertyChangeListener(PropertyChangeListener l) {
 200         support.removePropertyChangeListener(l);
 201     }
 202 
 203     private Font font;
 204     private Toolkit toolkit;
 205     private String sampleText = "Abcde...";
 206 
 207     private Label sample;
 208     private Choice familyChoser;
 209     private Choice styleChoser;
 210     private Choice sizeChoser;
 211 
 212     private String fonts[];
 213     private String[] styleNames = { "plain", "bold", "italic" };
 214     private int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC };
 215     private int[] pointSizes = { 3, 5, 8, 10, 12, 14, 18, 24, 36, 48 };
 216 
 217     private PropertyChangeSupport support = new PropertyChangeSupport(this);
 218 
 219 }