1 /* 2 * Copyright (c) 1996, 2014, 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 @SuppressWarnings("deprecation") 35 public FontEditor() { 36 setLayout(null); 37 38 toolkit = Toolkit.getDefaultToolkit(); 39 fonts = toolkit.getFontList(); 40 41 familyChoser = new Choice(); 42 for (int i = 0; i < fonts.length; i++) { 43 familyChoser.addItem(fonts[i]); 44 } 45 add(familyChoser); 46 familyChoser.reshape(20, 5, 100, 30); 47 48 styleChoser = new Choice(); 49 for (int i = 0; i < styleNames.length; i++) { 50 styleChoser.addItem(styleNames[i]); 51 } 52 add(styleChoser); 53 styleChoser.reshape(145, 5, 70, 30); 54 55 sizeChoser = new Choice(); 56 for (int i = 0; i < pointSizes.length; i++) { 57 sizeChoser.addItem("" + pointSizes[i]); 58 } 59 add(sizeChoser); 60 sizeChoser.reshape(220, 5, 70, 30); 61 62 resize(300,40); 63 } 64 65 66 @SuppressWarnings("deprecation") 67 public Dimension preferredSize() { 68 return new Dimension(300, 40); 69 } 70 71 public void setValue(Object o) { 72 font = (Font) o; 73 if (this.font == null) 74 return; 75 76 changeFont(font); 77 // Update the current GUI choices. 78 for (int i = 0; i < fonts.length; i++) { 79 if (fonts[i].equals(font.getFamily())) { 80 familyChoser.select(i); 81 break; 82 } 83 } 84 for (int i = 0; i < styleNames.length; i++) { 85 if (font.getStyle() == styles[i]) { 86 styleChoser.select(i); 87 break; 88 } 89 } 90 for (int i = 0; i < pointSizes.length; i++) { 91 if (font.getSize() <= pointSizes[i]) { 92 sizeChoser.select(i); 93 break; 94 } 95 } 96 } 97 98 @SuppressWarnings("deprecation") 99 private void changeFont(Font f) { 100 font = f; 101 if (sample != null) { 102 remove(sample); 103 } 104 sample = new Label(sampleText); 105 sample.setFont(font); 106 add(sample); 107 Component p = getParent(); 108 if (p != null) { 109 p.invalidate(); 110 p.layout(); 111 } 112 invalidate(); 113 layout(); 114 repaint(); 115 support.firePropertyChange("", null, null); 116 } 117 118 public Object getValue() { 119 return (font); 120 } 121 122 public String getJavaInitializationString() { 123 if (this.font == null) 124 return "null"; 125 126 return "new java.awt.Font(\"" + font.getName() + "\", " + 127 font.getStyle() + ", " + font.getSize() + ")"; 128 } 129 130 @SuppressWarnings("deprecation") 131 public boolean action(Event e, Object arg) { 132 String family = familyChoser.getSelectedItem(); 133 int style = styles[styleChoser.getSelectedIndex()]; 134 int size = pointSizes[sizeChoser.getSelectedIndex()]; 135 try { 136 Font f = new Font(family, style, size); 137 changeFont(f); 138 } catch (Exception ex) { 139 System.err.println("Couldn't create font " + family + "-" + 140 styleNames[style] + "-" + size); 141 } 142 return (false); 143 } 144 145 146 public boolean isPaintable() { 147 return true; 148 } 149 150 public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { 151 // Silent noop. 152 Font oldFont = gfx.getFont(); 153 gfx.setFont(font); 154 FontMetrics fm = gfx.getFontMetrics(); 155 int vpad = (box.height - fm.getAscent())/2; 156 gfx.drawString(sampleText, 0, box.height-vpad); 157 gfx.setFont(oldFont); 158 } 159 160 public String getAsText() { 161 if (this.font == null) { 162 return null; 163 } 164 StringBuilder sb = new StringBuilder(); 165 sb.append(this.font.getName()); 166 sb.append(' '); 167 168 boolean b = this.font.isBold(); 169 if (b) { 170 sb.append("BOLD"); 171 } 172 boolean i = this.font.isItalic(); 173 if (i) { 174 sb.append("ITALIC"); 175 } 176 if (b || i) { 177 sb.append(' '); 178 } 179 sb.append(this.font.getSize()); 180 return sb.toString(); 181 } 182 183 public void setAsText(String text) throws IllegalArgumentException { 184 setValue((text == null) ? null : Font.decode(text)); 185 } 186 187 public String[] getTags() { 188 return null; 189 } 190 191 public java.awt.Component getCustomEditor() { 192 return this; 193 } 194 195 public boolean supportsCustomEditor() { 196 return true; 197 } 198 199 public void addPropertyChangeListener(PropertyChangeListener l) { 200 support.addPropertyChangeListener(l); 201 } 202 203 public void removePropertyChangeListener(PropertyChangeListener l) { 204 support.removePropertyChangeListener(l); 205 } 206 207 private Font font; 208 private Toolkit toolkit; 209 private String sampleText = "Abcde..."; 210 211 private Label sample; 212 private Choice familyChoser; 213 private Choice styleChoser; 214 private Choice sizeChoser; 215 216 private String fonts[]; 217 private String[] styleNames = { "plain", "bold", "italic" }; 218 private int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC }; 219 private int[] pointSizes = { 3, 5, 8, 10, 12, 14, 18, 24, 36, 48 }; 220 221 private PropertyChangeSupport support = new PropertyChangeSupport(this); 222 223 }