001 /* JFontChooser.java
002 *
003 * Copyright (c) 1995-2010, The University of Sheffield. See the file
004 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005 *
006 * This file is part of GATE (see http://gate.ac.uk/), and is free
007 * software, licenced under the GNU Library General Public License,
008 * Version 2, June 1991 (in the distribution as file licence.html,
009 * and also available at http://gate.ac.uk/gate/licence.html).
010 *
011 * Valentin Tablan 06/04/2001
012 *
013 * $Id: JFontChooser.java 12006 2009-12-01 17:24:28Z thomas_heitz $
014 *
015 */
016
017 package gate.swing;
018
019 import java.awt.*;
020 import java.awt.event.*;
021 import java.awt.font.TextAttribute;
022 import java.util.HashMap;
023 import java.util.Map;
024
025 import javax.swing.*;
026
027 public class JFontChooser extends JPanel {
028
029 public JFontChooser(){
030 this(UIManager.getFont("Button.font"));
031 }
032
033 public JFontChooser(Font initialFont){
034 initLocalData();
035 initGuiComponents();
036 initListeners();
037 setFontValue(initialFont);
038 }// public JFontChooser(Font initialFont)
039
040 public static Font showDialog(Component parent, String title,
041 Font initialfont){
042
043 Window windowParent;
044 if(parent instanceof Window) windowParent = (Window)parent;
045 else windowParent = SwingUtilities.getWindowAncestor(parent);
046 if(windowParent == null) throw new IllegalArgumentException(
047 "The supplied parent component has no window ancestor");
048 final JDialog dialog;
049 if(windowParent instanceof Frame) dialog = new JDialog((Frame)windowParent,
050 title, true);
051 else dialog = new JDialog((Dialog)windowParent, title, true);
052
053 dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(),
054 BoxLayout.Y_AXIS));
055
056 final JFontChooser fontChooser = new JFontChooser(initialfont);
057 dialog.getContentPane().add(fontChooser);
058
059 JButton okBtn = new JButton("OK");
060 JButton cancelBtn = new JButton("Cancel");
061 JPanel buttonsBox = new JPanel();
062 buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS));
063 buttonsBox.add(Box.createHorizontalGlue());
064 buttonsBox.add(okBtn);
065 buttonsBox.add(Box.createHorizontalStrut(30));
066 buttonsBox.add(cancelBtn);
067 buttonsBox.add(Box.createHorizontalGlue());
068 dialog.getContentPane().add(buttonsBox);
069 dialog.pack();
070 fontChooser.addComponentListener(new ComponentAdapter() {
071 public void componentResized(ComponentEvent e) {
072 dialog.pack();
073 }
074 });
075 okBtn.addActionListener(new ActionListener() {
076 public void actionPerformed(ActionEvent e) {
077 dialog.setVisible(false);
078 }
079 });
080
081 cancelBtn.addActionListener(new ActionListener() {
082 public void actionPerformed(ActionEvent e) {
083 dialog.setVisible(false);
084 fontChooser.setFontValue(null);
085 }
086 });
087
088 dialog.setVisible(true);
089
090 return fontChooser.getFontValue();
091 }// showDialog
092
093 protected void initLocalData() {
094
095 }
096
097 protected void initGuiComponents() {
098 this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
099 familyCombo = new JComboBox(
100 GraphicsEnvironment.getLocalGraphicsEnvironment().
101 getAvailableFontFamilyNames()
102 );
103 familyCombo.setSelectedItem(UIManager.getFont("Label.font").getFamily());
104
105 sizeCombo = new JComboBox(new String[]{"6", "8", "10", "12", "14", "16",
106 "18", "20", "22", "24", "26"});
107 sizeCombo.setSelectedItem(new Integer(
108 UIManager.getFont("Label.font").getSize()).toString());
109
110 italicChk = new JCheckBox("<html><i>Italic</i></html>", false);
111 boldChk = new JCheckBox("<html><i=b>Bold</b></html>", false);
112
113 JPanel fontBox = new JPanel();
114 fontBox.setLayout(new BoxLayout(fontBox, BoxLayout.X_AXIS));
115 fontBox.add(familyCombo);
116 fontBox.add(sizeCombo);
117 fontBox.setBorder(BorderFactory.createTitledBorder(" Font "));
118 add(fontBox);
119 add(Box.createVerticalStrut(10));
120
121 JPanel effectsBox = new JPanel();
122 effectsBox.setLayout(new BoxLayout(effectsBox, BoxLayout.X_AXIS));
123 effectsBox.add(italicChk);
124 effectsBox.add(boldChk);
125 effectsBox.setBorder(BorderFactory.createTitledBorder(" Effects "));
126 add(effectsBox);
127 add(Box.createVerticalStrut(10));
128
129 sampleTextArea = new JTextArea("Type your sample here...");
130 JPanel samplePanel = new JPanel();
131 samplePanel.setLayout(new BoxLayout(samplePanel, BoxLayout.X_AXIS));
132 //samplePanel.add(new JScrollPane(sampleTextArea));
133 samplePanel.add(sampleTextArea);
134 samplePanel.setBorder(BorderFactory.createTitledBorder(" Sample "));
135 add(samplePanel);
136 add(Box.createVerticalStrut(10));
137 }// initGuiComponents()
138
139 protected void initListeners(){
140 familyCombo.addActionListener(new ActionListener() {
141 public void actionPerformed(ActionEvent e) {
142 updateFont();
143 }
144 });
145
146 sizeCombo.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 updateFont();
149 }
150 });
151
152 boldChk.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
154 updateFont();
155 }
156 });
157
158 italicChk.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
160 updateFont();
161 }
162 });
163 }// initListeners()
164
165 protected void updateFont(){
166 Map fontAttrs = new HashMap();
167 fontAttrs.put(TextAttribute.FAMILY, (String)familyCombo.getSelectedItem());
168 fontAttrs.put(TextAttribute.SIZE, new Float((String)sizeCombo.getSelectedItem()));
169
170 if(boldChk.isSelected())
171 fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
172 else fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
173
174 if(italicChk.isSelected())
175 fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
176 else fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
177
178 Font newFont = new Font(fontAttrs);
179 Font oldFont = fontValue;
180 fontValue = newFont;
181 sampleTextArea.setFont(newFont);
182 String text = sampleTextArea.getText();
183 sampleTextArea.setText("");
184 sampleTextArea.setText(text);
185 sampleTextArea.repaint(100);
186 firePropertyChange("fontValue", oldFont, newFont);
187 }//updateFont()
188
189 /**
190 * Test code
191 */
192 public static void main(String args[]){
193 try{
194 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
195 }catch(Exception e){
196 e.printStackTrace();
197 }
198 final JFrame frame = new JFrame("Foo frame");
199 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
200 JButton btn = new JButton("Show dialog");
201 btn.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent e) {
203 System.out.println(showDialog(frame, "Fonter",
204 UIManager.getFont("Button.font")));
205 }
206 });
207 frame.getContentPane().add(btn);
208 frame.setSize(new Dimension(300, 300));
209 frame.setVisible(true);
210 System.out.println("Font: " + UIManager.getFont("Button.font"));
211 showDialog(frame, "Fonter", UIManager.getFont("Button.font"));
212 }// main
213
214 public void setFontValue(java.awt.Font newfontValue) {
215 boldChk.setSelected(newfontValue.isBold());
216 italicChk.setSelected(newfontValue.isItalic());
217 familyCombo.setSelectedItem(newfontValue.getName());
218 sizeCombo.setSelectedItem(Integer.toString(newfontValue.getSize()));
219 this.fontValue = newfontValue;
220 }
221
222 public java.awt.Font getFontValue() {
223 return fontValue;
224 }
225
226 JComboBox familyCombo;
227 JCheckBox italicChk;
228 JCheckBox boldChk;
229 JComboBox sizeCombo;
230 JTextArea sampleTextArea;
231 private java.awt.Font fontValue;
232 }// class JFontChooser extends JPanel
|