View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.component;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import java.awt.Font;
25  import java.awt.Insets;
26  import java.awt.event.ActionEvent;
27  
28  import javax.swing.AbstractAction;
29  import javax.swing.Action;
30  import javax.swing.JButton;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.border.BevelBorder;
34  
35  import org.xnap.gui.util.IconHelper;
36  
37  /***
38   * Provides a panel that displays a font name and a button that can launch
39   * a {@link FontChooserDialog}.
40   */
41  public class FontPanel extends JPanel
42  {
43  
44      //--- Constant(s) ---
45  
46      //--- Data field(s) ---
47  
48      private Component parent;
49      private Font font;
50      private JLabel jlFont;
51  
52      //--- Constructor(s) ---
53  
54      public FontPanel(Component parent, Font newFont)
55      {
56  		this.parent = parent;
57  
58  		setLayout(new BorderLayout());
59  	
60  		jlFont = new JLabel();
61  		jlFont.setBorder(new BevelBorder(BevelBorder.LOWERED));
62  		add(jlFont, BorderLayout.CENTER);
63  
64  		JButton jb = new XNapButton(new FontAction());
65  		jb.setMargin(new Insets(1, 1, 1, 1));
66  		add(jb, BorderLayout.EAST);
67  
68  		setSelectedFont(newFont);
69      }
70  
71      //--- Method(s) ---
72  
73      public Font getSelectedFont()
74      {
75  		return font;
76      }
77  
78      /***
79       * Makes sure that font is always != null.
80       */
81      public void setSelectedFont(Font newValue)
82      {
83  		font = newValue;
84  		jlFont.setFont(font);
85  		jlFont.setText(" " + font.getName() + ", " + font.getSize());
86      }
87  
88      /***
89       * 
90       */
91      private class FontAction extends AbstractAction {
92  
93          public FontAction() 
94  		{
95              putValue(Action.SHORT_DESCRIPTION, "Opens font selection dialog.");
96  			putValue(IconHelper.XNAP_ICON, "charset.png");
97          }
98  
99          public void actionPerformed(ActionEvent event) 
100 		{
101 			Font f = FontChooserDialog.showDialog(parent, getSelectedFont());
102 			if (f != null) {
103 				setSelectedFont(f);
104 			}
105 		}
106 
107     }
108 
109 }
110