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.util.*;
23  import java.awt.Component;
24  import java.awt.Font;
25  
26  import org.apache.log4j.Logger;
27  
28  /***
29   * This class provides a font chooser dialog. The user can choose from a list
30   * of fonts and attributes that are available on the system.
31   */
32  public class FontChooserDialog extends DefaultDialog 
33  {
34  
35      //--- Constant(s) ---
36  
37      //--- Data field(s) ---
38  
39      protected static Logger logger = Logger.getLogger(FontChooserDialog.class);
40  
41      protected FontSelectionPanel fsp;
42      protected Font font = null;
43  
44      //--- Constructor(s) ---
45  
46      public FontChooserDialog(Font font)
47      {
48  		fsp = new FontSelectionPanel(font);
49  		setMainComponent(fsp);
50  
51  		pack();
52  
53  		try {
54  			fsp.setSelectedFont(font);
55  		}
56  		catch (Exception e) {
57  			logger.debug("font selection failed", e);
58  		}
59      }
60  
61      //--- Method(s) ---
62  
63      public boolean apply() 
64      {
65  		try {
66  			font = fsp.getSelectedFont();
67  		}
68  		catch (FontSelectionPanel.InvalidFontException e) {
69  		}
70  		return true;
71      }
72  
73      /***
74       * Returns the selected font.
75       *
76       * @return null, if dialog was canceled; the font, otherwise
77       */
78      public Font getSelectedFont()
79      {
80  		return font;
81      }
82  
83      /***
84       * Displays a font dialog. The dialog is placed relative to the position
85       * of <code>c</code>.
86       *
87       * @param c the component; if null, the dialog will be placed at the center
88       *          of the screen
89       * @param f the font that is to be selected, if available
90       * @return null, if dialog was canceled; the font, otherwise
91       */
92      public static Font showDialog(Component c, Font f)
93      {
94  		FontChooserDialog d = new FontChooserDialog(f);
95  		d.setModal(true);
96  		d.show(c);
97  		return d.getSelectedFont();
98      }
99  
100 }
101 
102