JAL-2797 added the frame and listener interfaces
[jalview.git] / forester / java / src / org / forester / archaeopteryx / FontChooser.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
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.
7 //
8 // The FontChooser class is based on the JFontChooser class written
9 // by: James Bardsley (torasin@torasin.com)
10 //
11 // Contact: phylosoft @ gmail . com
12 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
13
14 package org.forester.archaeopteryx;
15
16 import java.awt.Component;
17 import java.awt.Container;
18 import java.awt.Font;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21
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;
33
34 public class FontChooser extends JDialog implements ActionListener, ListSelectionListener {
35
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;
47     private Font                  _font;
48     private int                   _option;
49     private String                _type;
50     private int                   _style;
51     private int                   _size;
52     private final JList<String>   _font_list        = new JList<String>( AptxUtil.getAvailableFontFamiliesSorted() );
53     private final JList<String>   _style_list       = new JList<String>( STYLE );
54     private final JList<String>   _size_list        = new JList<String>( 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" );
67
68     public FontChooser() {
69         this( new Font( DEFAULT_FONT_NAME, Font.PLAIN, 12 ) );
70     }
71
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" );
76         _font = font;
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 );
112         setSize( 424, 177 );
113         setResizable( false );
114         setModal( true );
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 );
123     }
124
125     public FontChooser( final String font_name, final int font_style, final int size ) {
126         this( new Font( font_name, font_style, size ) );
127     }
128
129     @Override
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 ( _font_list.getModel().getElementAt( i ).startsWith( _fonts_tf.getText().trim() ) ) {
136                     _font_list.setSelectedIndex( i );
137                     setScrollPos( _font_jsp, _font_list, i );
138                     found = true;
139                     break;
140                 }
141             }
142             if ( !found ) {
143                 _font_list.clearSelection();
144             }
145             else {
146                 _test_tf.setFont( new Font( _type, _style, _size ) );
147             }
148         }
149         else if ( e.getSource() == _size_tf ) {
150             boolean found = false;
151             parseSize();
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 );
157                     found = true;
158                     break;
159                 }
160             }
161             if ( !found ) {
162                 _size_list.clearSelection();
163             }
164         }
165         else if ( e.getSource() == _style_tf ) {
166             if ( _style_tf.getText().equals( REGULAR ) ) {
167                 _style = Font.PLAIN;
168             }
169             else if ( _style_tf.getText().equals( BOLD ) ) {
170                 _style = Font.BOLD;
171             }
172             else if ( _style_tf.getText().equals( ITALIC ) ) {
173                 _style = Font.ITALIC;
174             }
175             else if ( _style_tf.getText().equals( BOLD_ITALIC ) ) {
176                 _style = Font.BOLD & Font.ITALIC;
177             }
178             _style_list.setSelectedIndex( _style );
179             _test_tf.setFont( new Font( _type, _style, _size ) );
180         }
181         else if ( e.getSource() == _ok_button ) {
182             parseSize();
183             _option = OK_OPTION;
184             _font = new Font( _type, _style, _size );
185             setVisible( false );
186         }
187         else if ( e.getSource() == _cancel_button ) {
188             _option = CANCEL_OPTION;
189             setVisible( false );
190         }
191     }
192
193     @Override
194     public Font getFont() {
195         return _font;
196     }
197
198     public String getFontName() {
199         return _font.getFontName();
200     }
201
202     public int getFontSize() {
203         return _font.getSize();
204     }
205
206     public int getFontStyle() {
207         return _font.getStyle();
208     }
209
210     @Override
211     public void setFont( final Font font ) {
212         _font = font;
213     }
214
215     public int showDialog( final Component parent, final String title ) {
216         boolean found = false;
217         _option = CANCEL_OPTION;
218         setTitle( title );
219         _test_tf.setFont( new Font( _type, _style, _size ) );
220         for( int i = 0; i < _font_list.getModel().getSize(); i++ ) {
221             _font_list.setSelectedIndex( i );
222             if ( _font.getFamily().equals( _font_list.getSelectedValue() ) ) {
223                 found = true;
224                 setScrollPos( _font_jsp, _font_list, i );
225                 break;
226             }
227         }
228         if ( !found ) {
229             _font_list.clearSelection();
230         }
231         _style_list.setSelectedIndex( _font.getStyle() );
232         found = false;
233         for( int i = 0; i < _size_list.getModel().getSize(); i++ ) {
234             _size_list.setSelectedIndex( i );
235             if ( _font.getSize() <= Integer.parseInt( _size_list.getSelectedValue() ) ) {
236                 found = true;
237                 setScrollPos( _size_jsp, _size_list, i );
238                 break;
239             }
240         }
241         if ( !found ) {
242             _size_list.clearSelection();
243         }
244         setLocationRelativeTo( parent );
245         setVisible( true );
246         return _option;
247     }
248
249     @Override
250     public void valueChanged( final ListSelectionEvent e ) {
251         if ( e.getSource() == _font_list ) {
252             if ( _font_list.getSelectedValue() != null ) {
253                 _fonts_tf.setText( ( ( _font_list.getSelectedValue() ) ) );
254             }
255             _type = _fonts_tf.getText();
256             _test_tf.setFont( new Font( _type, _style, _size ) );
257         }
258         else if ( e.getSource() == _style_list ) {
259             _style_tf.setText( ( ( _style_list.getSelectedValue() ) ) );
260             if ( _style_tf.getText().equals( REGULAR ) ) {
261                 _style = 0;
262             }
263             else if ( _style_tf.getText().equals( BOLD ) ) {
264                 _style = 1;
265             }
266             else if ( _style_tf.getText().equals( ITALIC ) ) {
267                 _style = 2;
268             }
269             else if ( _style_tf.getText().equals( BOLD_ITALIC ) ) {
270                 _style = 3;
271             }
272             _test_tf.setFont( new Font( _type, _style, _size ) );
273         }
274         else if ( e.getSource() == _size_list ) {
275             if ( _size_list.getSelectedValue() != null ) {
276                 _size_tf.setText( ( ( _size_list.getSelectedValue() ) ) );
277             }
278             _size = ( Integer.parseInt( _size_tf.getText().trim() ) );
279             _test_tf.setFont( new Font( _type, _style, _size ) );
280         }
281     }
282
283     private void parseSize() {
284         try {
285             _size = ( Integer.parseInt( _size_tf.getText().trim() ) );
286         }
287         catch ( final Exception ex ) {
288             // Ignore.
289         }
290         if ( _size < 1 ) {
291             _size = 1;
292         }
293     }
294
295     private void setScrollPos( final JScrollPane sp, final JList<String> list, final int index ) {
296         final int unit_size = sp.getVerticalScrollBar().getMaximum() / list.getModel().getSize();
297         sp.getVerticalScrollBar().setValue( ( index - 2 ) * unit_size );
298     }
299 }