2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // The FontChooser class is in the Public Domain, the code may be used
6 // for any purpose. It is provided as is with no warranty.
8 // The FontChooser class is based on the JFontChooser class written
9 // by: James Bardsley (torasin@torasin.com)
11 // Contact: phylosoft @ gmail . com
12 // WWW: www.phylosoft.org/forester
14 package org.forester.archaeopteryx;
16 import java.awt.Component;
17 import java.awt.Container;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
22 import javax.swing.JButton;
23 import javax.swing.JDialog;
24 import javax.swing.JLabel;
25 import javax.swing.JList;
26 import javax.swing.JPanel;
27 import javax.swing.JScrollPane;
28 import javax.swing.JTextField;
29 import javax.swing.ScrollPaneConstants;
30 import javax.swing.border.TitledBorder;
31 import javax.swing.event.ListSelectionEvent;
32 import javax.swing.event.ListSelectionListener;
34 public class FontChooser extends JDialog implements ActionListener, ListSelectionListener {
36 private static final String BOLD_ITALIC = "Bold Italic";
37 private static final String ITALIC = "Italic";
38 private static final String BOLD = "Bold";
39 private static final String REGULAR = "Regular";
40 private static final String DEFAULT_FONT_NAME = "Sans";
41 public static final long serialVersionUID = 62256323L;
42 private static final String[] STYLE = { REGULAR, BOLD, ITALIC, BOLD_ITALIC };
43 private static final String[] SIZE = { "3", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22",
44 "24", "26", "28", "36", "72" };
45 private static final int OK_OPTION = 1;
46 private static final int CANCEL_OPTION = 2;
52 private final JList _font_list = new JList( Util.getAvailableFontFamiliesSorted() );
53 private final JList _style_list = new JList( STYLE );
54 private final JList _size_list = new JList( SIZE );
55 private final JTextField _fonts_tf = new JTextField();
56 private final JTextField _style_tf = new JTextField();
57 private final JTextField _size_tf = new JTextField();
58 private final JLabel _fonts_label = new JLabel( "Font:" );
59 private final JLabel _style_label = new JLabel( "Style:" );
60 private final JLabel _size_label = new JLabel( "Size:" );
61 private final JScrollPane _font_jsp = new JScrollPane( _font_list );
62 private final JScrollPane _style_jsp = new JScrollPane( _style_list );
63 private final JScrollPane _size_jsp = new JScrollPane( _size_list );
64 private final JButton _ok_button = new JButton( "OK" );
65 private final JButton _cancel_button = new JButton( "Cancel" );
66 private final JTextField _test_tf = new JTextField( "AaBbZz012" );
68 public FontChooser() {
69 this( new Font( DEFAULT_FONT_NAME, Font.PLAIN, 12 ) );
72 public FontChooser( final Font font ) {
73 final Container container = getContentPane();
74 final JPanel panel = new JPanel();
75 final TitledBorder panel_border = new TitledBorder( "Demo" );
77 _type = _font.getFontName();
78 _style = _font.getStyle();
79 _size = _font.getSize();
80 _font_list.setSelectionMode( 0 );
81 _style_list.setSelectionMode( 0 );
82 _size_list.setSelectionMode( 0 );
83 _font_jsp.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
84 _style_jsp.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
85 _size_jsp.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
86 panel.setBorder( panel_border );
87 _fonts_tf.setBounds( 8, 32, 121, 20 );
88 _font_jsp.setBounds( 8, 56, 121, 82 );
89 _style_tf.setBounds( 136, 32, 121, 20 );
90 _style_jsp.setBounds( 136, 56, 121, 82 );
91 _size_tf.setBounds( 264, 32, 41, 20 );
92 _size_jsp.setBounds( 264, 56, 41, 82 );
93 _ok_button.setBounds( 320, 8, 89, 17 );
94 _cancel_button.setBounds( 320, 32, 89, 17 );
95 panel.setBounds( 320, 64, 89, 73 );
96 container.add( _fonts_label );
97 container.add( _fonts_tf );
98 container.add( _font_jsp );
99 container.add( _style_label );
100 container.add( _style_tf );
101 container.add( _style_jsp );
102 container.add( _size_label );
103 container.add( _size_tf );
104 container.add( _size_jsp );
105 container.add( _ok_button );
106 container.add( _cancel_button );
107 container.add( panel );
108 _test_tf.setBounds( 8, 25, 73, 30 );
109 panel.add( _test_tf );
110 container.setLayout( null );
111 panel.setLayout( null );
113 setResizable( false );
115 _fonts_tf.addActionListener( this );
116 _size_tf.addActionListener( this );
117 _style_tf.addActionListener( this );
118 _cancel_button.addActionListener( this );
119 _ok_button.addActionListener( this );
120 _font_list.addListSelectionListener( this );
121 _style_list.addListSelectionListener( this );
122 _size_list.addListSelectionListener( this );
125 public FontChooser( final String font_name, final int font_style, final int size ) {
126 this( new Font( font_name, font_style, size ) );
130 public void actionPerformed( final ActionEvent e ) {
131 if ( e.getSource() == _fonts_tf ) {
132 boolean found = false;
133 _type = _fonts_tf.getText();
134 for( int i = 0; i < _font_list.getModel().getSize(); i++ ) {
135 if ( ( ( String ) _font_list.getModel().getElementAt( i ) ).startsWith( _fonts_tf.getText().trim() ) ) {
136 _font_list.setSelectedIndex( i );
137 setScrollPos( _font_jsp, _font_list, i );
143 _font_list.clearSelection();
146 _test_tf.setFont( new Font( _type, _style, _size ) );
149 else if ( e.getSource() == _size_tf ) {
150 boolean found = false;
152 _test_tf.setFont( new Font( _type, _style, _size ) );
153 for( int i = 0; i < _size_list.getModel().getSize(); i++ ) {
154 if ( _size_tf.getText().trim().equals( _size_list.getModel().getElementAt( i ) ) ) {
155 _size_list.setSelectedIndex( i );
156 setScrollPos( _size_jsp, _size_list, i );
162 _size_list.clearSelection();
165 else if ( e.getSource() == _style_tf ) {
166 if ( _style_tf.getText().equals( REGULAR ) ) {
169 else if ( _style_tf.getText().equals( BOLD ) ) {
172 else if ( _style_tf.getText().equals( ITALIC ) ) {
173 _style = Font.ITALIC;
175 else if ( _style_tf.getText().equals( BOLD_ITALIC ) ) {
176 _style = Font.BOLD & Font.ITALIC;
178 _style_list.setSelectedIndex( _style );
179 _test_tf.setFont( new Font( _type, _style, _size ) );
181 else if ( e.getSource() == _ok_button ) {
184 _font = new Font( _type, _style, _size );
187 else if ( e.getSource() == _cancel_button ) {
188 _option = CANCEL_OPTION;
194 public Font getFont() {
198 public String getFontName() {
199 return _font.getFontName();
202 public int getFontSize() {
203 return _font.getSize();
206 public int getFontStyle() {
207 return _font.getStyle();
210 private void parseSize() {
212 _size = ( Integer.parseInt( _size_tf.getText().trim() ) );
214 catch ( final Exception ex ) {
223 public void setFont( final Font font ) {
227 private void setScrollPos( final JScrollPane sp, final JList list, final int index ) {
228 final int unit_size = sp.getVerticalScrollBar().getMaximum() / list.getModel().getSize();
229 sp.getVerticalScrollBar().setValue( ( index - 2 ) * unit_size );
232 public int showDialog( final Component parent, final String title ) {
233 boolean found = false;
234 _option = CANCEL_OPTION;
236 _test_tf.setFont( new Font( _type, _style, _size ) );
237 for( int i = 0; i < _font_list.getModel().getSize(); i++ ) {
238 _font_list.setSelectedIndex( i );
239 if ( _font.getFamily().equals( _font_list.getSelectedValue() ) ) {
241 setScrollPos( _font_jsp, _font_list, i );
246 _font_list.clearSelection();
248 _style_list.setSelectedIndex( _font.getStyle() );
250 for( int i = 0; i < _size_list.getModel().getSize(); i++ ) {
251 _size_list.setSelectedIndex( i );
252 if ( _font.getSize() <= Integer.parseInt( ( String ) _size_list.getSelectedValue() ) ) {
254 setScrollPos( _size_jsp, _size_list, i );
259 _size_list.clearSelection();
261 setLocationRelativeTo( parent );
267 public void valueChanged( final ListSelectionEvent e ) {
268 if ( e.getSource() == _font_list ) {
269 if ( _font_list.getSelectedValue() != null ) {
270 _fonts_tf.setText( ( ( String ) ( _font_list.getSelectedValue() ) ) );
272 _type = _fonts_tf.getText();
273 _test_tf.setFont( new Font( _type, _style, _size ) );
275 else if ( e.getSource() == _style_list ) {
276 _style_tf.setText( ( ( String ) ( _style_list.getSelectedValue() ) ) );
277 if ( _style_tf.getText().equals( REGULAR ) ) {
280 else if ( _style_tf.getText().equals( BOLD ) ) {
283 else if ( _style_tf.getText().equals( ITALIC ) ) {
286 else if ( _style_tf.getText().equals( BOLD_ITALIC ) ) {
289 _test_tf.setFont( new Font( _type, _style, _size ) );
291 else if ( e.getSource() == _size_list ) {
292 if ( _size_list.getSelectedValue() != null ) {
293 _size_tf.setText( ( ( String ) ( _size_list.getSelectedValue() ) ) );
295 _size = ( Integer.parseInt( _size_tf.getText().trim() ) );
296 _test_tf.setFont( new Font( _type, _style, _size ) );