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