< prev index next >

src/java.desktop/share/classes/com/sun/beans/editors/FontEditor.java

Print this page


   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) {


   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) {


< prev index next >