| JFontChooser |

    1   /*  JFontChooser.java
    2    *
    3    *  Copyright (c) 1998-2001, The University of Sheffield.
    4    *
    5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    6    *  software, licenced under the GNU Library General Public License,
    7    *  Version 2, June 1991 (in the distribution as file licence.html,
    8    *  and also available at http://gate.ac.uk/gate/licence.html).
    9    *
    10   *  Valentin Tablan 06/04/2001
    11   *
    12   *  $Id: JFontChooser.java,v 1.8 2001/11/16 15:15:29 valyt Exp $
    13   *
    14   */
    15
    16  package gate.swing;
    17
    18  import javax.swing.*;
    19  import javax.swing.event.*;
    20  import java.awt.event.*;
    21  import java.awt.Font;
    22  import java.awt.font.TextAttribute;
    23  import java.awt.Frame;
    24  import java.awt.Window;
    25  import java.awt.Dialog;
    26  import java.awt.Component;
    27  import java.awt.Dimension;
    28  import java.awt.GraphicsEnvironment;
    29  import javax.swing.plaf.FontUIResource;
    30  import java.beans.*;
    31  import java.util.*;
    32
    33  import gate.util.*;
    34
    35  public class JFontChooser extends JPanel {
    36
    37    public JFontChooser(){
    38      this(UIManager.getFont("Button.font"));
    39    }
    40
    41    public JFontChooser(Font initialFont){
    42      initLocalData();
    43      initGuiComponents();
    44      initListeners();
    45      setFontValue(initialFont);
    46    }// public JFontChooser(Font initialFont)
    47
    48    public static Font showDialog(Component parent, String title,
    49                                  Font initialfont){
    50
    51      Window windowParent;
    52      if(parent instanceof Window) windowParent = (Window)parent;
    53      else windowParent = SwingUtilities.getWindowAncestor(parent);
    54      if(windowParent == null) throw new IllegalArgumentException(
    55        "The supplied parent component has no window ancestor");
    56      final JDialog dialog;
    57      if(windowParent instanceof Frame) dialog = new JDialog((Frame)windowParent,
    58                                                             title, true);
    59      else dialog = new JDialog((Dialog)windowParent, title, true);
    60
    61      dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(),
    62                                                      BoxLayout.Y_AXIS));
    63
    64      final JFontChooser fontChooser = new JFontChooser(initialfont);
    65      dialog.getContentPane().add(fontChooser);
    66
    67      JButton okBtn = new JButton("OK");
    68      JButton cancelBtn = new JButton("Cancel");
    69      JPanel buttonsBox = new JPanel();
    70      buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS));
    71      buttonsBox.add(Box.createHorizontalGlue());
    72      buttonsBox.add(okBtn);
    73      buttonsBox.add(Box.createHorizontalStrut(30));
    74      buttonsBox.add(cancelBtn);
    75      buttonsBox.add(Box.createHorizontalGlue());
    76      dialog.getContentPane().add(buttonsBox);
    77      dialog.pack();
    78      fontChooser.addComponentListener(new ComponentAdapter() {
    79        public void componentResized(ComponentEvent e) {
    80          dialog.pack();
    81        }
    82      });
    83      okBtn.addActionListener(new ActionListener() {
    84        public void actionPerformed(ActionEvent e) {
    85          dialog.hide();
    86        }
    87      });
    88
    89      cancelBtn.addActionListener(new ActionListener() {
    90        public void actionPerformed(ActionEvent e) {
    91          dialog.hide();
    92          fontChooser.setFontValue(null);
    93        }
    94      });
    95
    96      dialog.show();
    97
    98      return fontChooser.getFontValue();
    99    }// showDialog
    100
    101   protected void initLocalData() {
    102
    103   }
    104
    105   protected void initGuiComponents() {
    106     this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    107     familyCombo = new JComboBox(
    108                         GraphicsEnvironment.getLocalGraphicsEnvironment().
    109                         getAvailableFontFamilyNames()
    110                       );
    111     familyCombo.setSelectedItem(UIManager.getFont("Label.font").getFamily());
    112
    113     sizeCombo = new JComboBox(new String[]{"6", "8", "10", "12", "14", "16",
    114                                               "18", "20", "22", "24", "26"});
    115     sizeCombo.setSelectedItem(new Integer(
    116                         UIManager.getFont("Label.font").getSize()).toString());
    117
    118     italicChk = new JCheckBox("<html><i>Italic</i></html>", false);
    119     boldChk = new JCheckBox("<html><i=b>Bold</b></html>", false);
    120
    121     JPanel fontBox = new JPanel();
    122     fontBox.setLayout(new BoxLayout(fontBox, BoxLayout.X_AXIS));
    123     fontBox.add(familyCombo);
    124     fontBox.add(sizeCombo);
    125     fontBox.setBorder(BorderFactory.createTitledBorder(" Font "));
    126     add(fontBox);
    127     add(Box.createVerticalStrut(10));
    128
    129     JPanel effectsBox = new JPanel();
    130     effectsBox.setLayout(new BoxLayout(effectsBox, BoxLayout.X_AXIS));
    131     effectsBox.add(italicChk);
    132     effectsBox.add(boldChk);
    133     effectsBox.setBorder(BorderFactory.createTitledBorder(" Effects "));
    134     add(effectsBox);
    135     add(Box.createVerticalStrut(10));
    136
    137     sampleTextArea = new JTextArea("Type your sample here...");
    138     JPanel samplePanel = new JPanel();
    139     samplePanel.setLayout(new BoxLayout(samplePanel, BoxLayout.X_AXIS));
    140     //samplePanel.add(new JScrollPane(sampleTextArea));
    141     samplePanel.add(sampleTextArea);
    142     samplePanel.setBorder(BorderFactory.createTitledBorder(" Sample "));
    143     add(samplePanel);
    144     add(Box.createVerticalStrut(10));
    145   }// initGuiComponents()
    146
    147   protected void initListeners(){
    148     familyCombo.addActionListener(new ActionListener() {
    149       public void actionPerformed(ActionEvent e) {
    150         updateFont();
    151       }
    152     });
    153
    154     sizeCombo.addActionListener(new ActionListener() {
    155       public void actionPerformed(ActionEvent e) {
    156         updateFont();
    157       }
    158     });
    159
    160     boldChk.addActionListener(new ActionListener() {
    161       public void actionPerformed(ActionEvent e) {
    162         updateFont();
    163       }
    164     });
    165
    166     italicChk.addActionListener(new ActionListener() {
    167       public void actionPerformed(ActionEvent e) {
    168         updateFont();
    169       }
    170     });
    171   }// initListeners()
    172
    173   protected void updateFont(){
    174     Map fontAttrs = new HashMap();
    175     fontAttrs.put(TextAttribute.FAMILY, (String)familyCombo.getSelectedItem());
    176     fontAttrs.put(TextAttribute.SIZE, new Float((String)sizeCombo.getSelectedItem()));
    177
    178     if(boldChk.isSelected())
    179       fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    180     else fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
    181
    182     if(italicChk.isSelected())
    183       fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
    184     else fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
    185
    186     Font newFont = new Font(fontAttrs);
    187     Font oldFont = fontValue;
    188     fontValue = newFont;
    189     sampleTextArea.setFont(newFont);
    190     String text = sampleTextArea.getText();
    191     sampleTextArea.setText("");
    192     sampleTextArea.setText(text);
    193     sampleTextArea.repaint(100);
    194     firePropertyChange("fontValue", oldFont, newFont);
    195   }//updateFont()
    196
    197   /**
    198    * Test code
    199    */
    200   public static void main(String args[]){
    201     try{
    202       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    203     }catch(Exception e){
    204       e.printStackTrace();
    205     }
    206     final JFrame frame = new JFrame("Foo frame");
    207     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    208     JButton btn = new JButton("Show dialog");
    209     btn.addActionListener(new ActionListener() {
    210       public void actionPerformed(ActionEvent e) {
    211         System.out.println(showDialog(frame, "Fonter",
    212                                       UIManager.getFont("Button.font")));
    213       }
    214     });
    215     frame.getContentPane().add(btn);
    216     frame.setSize(new Dimension(300, 300));
    217     frame.setVisible(true);
    218     System.out.println("Font: " + UIManager.getFont("Button.font"));
    219     showDialog(frame, "Fonter", UIManager.getFont("Button.font"));
    220   }// main
    221
    222   public void setFontValue(java.awt.Font newfontValue) {
    223     boldChk.setSelected(newfontValue.isBold());
    224     italicChk.setSelected(newfontValue.isItalic());
    225     familyCombo.setSelectedItem(newfontValue.getName());
    226     sizeCombo.setSelectedItem(Integer.toString(newfontValue.getSize()));
    227     this.fontValue = newfontValue;
    228   }
    229
    230   public java.awt.Font getFontValue() {
    231     return fontValue;
    232   }
    233
    234   JComboBox familyCombo;
    235   JCheckBox italicChk;
    236   JCheckBox boldChk;
    237   JComboBox sizeCombo;
    238   JTextArea sampleTextArea;
    239   private java.awt.Font fontValue;
    240 }// class JFontChooser extends JPanel

| JFontChooser |