1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 06/03/2001
10   *
11   *  $Id: XJTextPane.java,v 1.6 2005/01/11 13:51:37 ian Exp $
12   *
13   */
14  
15  package gate.swing;
16  
17  import java.awt.Font;
18  import java.beans.PropertyChangeEvent;
19  import java.beans.PropertyChangeListener;
20  
21  import javax.swing.JTextPane;
22  import javax.swing.UIManager;
23  import javax.swing.text.*;
24  
25  /**
26   * A custom JTextPane that reinitialises the default font style when th UI
27   * changes. This is needed by applications that want to be able to change the
28   * font in the entire application by changing the UI defaults table.
29   */
30  public class XJTextPane extends JTextPane {
31  
32    public XJTextPane() {
33      super();
34      initListeners();
35      updateStyle();
36    }
37  
38    public XJTextPane(StyledDocument doc) {
39      super(doc);
40      initListeners();
41      updateStyle();
42    }
43  
44    protected void initListeners(){
45      addPropertyChangeListener(new PropertyChangeListener() {
46        public void propertyChange(PropertyChangeEvent e) {
47          if(e.getPropertyName().equals("UI")){
48            updateStyle();
49          }else if(e.getPropertyName().equals("document")){
50            updateStyle();
51          }
52        }
53      });
54    }
55  
56    protected void updateStyle(){
57      Font newFont = UIManager.getFont("TextPane.font");
58      Style defaultStyle = getStyle("default");
59      StyleConstants.setFontFamily(defaultStyle, newFont.getFamily());
60      StyleConstants.setFontSize(defaultStyle, newFont.getSize());
61      StyleConstants.setItalic(defaultStyle, newFont.isItalic());
62      StyleConstants.setBold(defaultStyle, newFont.isBold());
63      repaint();
64    }
65  }