initial commit
[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: www.phylosoft.org/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           _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" );
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     public void actionPerformed( final ActionEvent e ) {
130         if ( e.getSource() == _fonts_tf ) {
131             boolean found = false;
132             _type = _fonts_tf.getText();
133             for( int i = 0; i < _font_list.getModel().getSize(); i++ ) {
134                 if ( ( ( String ) _font_list.getModel().getElementAt( i ) ).startsWith( _fonts_tf.getText().trim() ) ) {
135                     _font_list.setSelectedIndex( i );
136                     setScrollPos( _font_jsp, _font_list, i );
137                     found = true;
138                     break;
139                 }
140             }
141             if ( !found ) {
142                 _font_list.clearSelection();
143             }
144             else {
145                 _test_tf.setFont( new Font( _type, _style, _size ) );
146             }
147         }
148         else if ( e.getSource() == _size_tf ) {
149             boolean found = false;
150             parseSize();
151             _test_tf.setFont( new Font( _type, _style, _size ) );
152             for( int i = 0; i < _size_list.getModel().getSize(); i++ ) {
153                 if ( _size_tf.getText().trim().equals( _size_list.getModel().getElementAt( i ) ) ) {
154                     _size_list.setSelectedIndex( i );
155                     setScrollPos( _size_jsp, _size_list, i );
156                     found = true;
157                     break;
158                 }
159             }
160             if ( !found ) {
161                 _size_list.clearSelection();
162             }
163         }
164         else if ( e.getSource() == _style_tf ) {
165             if ( _style_tf.getText().equals( REGULAR ) ) {
166                 _style = Font.PLAIN;
167             }
168             else if ( _style_tf.getText().equals( BOLD ) ) {
169                 _style = Font.BOLD;
170             }
171             else if ( _style_tf.getText().equals( ITALIC ) ) {
172                 _style = Font.ITALIC;
173             }
174             else if ( _style_tf.getText().equals( BOLD_ITALIC ) ) {
175                 _style = Font.BOLD & Font.ITALIC;
176             }
177             _style_list.setSelectedIndex( _style );
178             _test_tf.setFont( new Font( _type, _style, _size ) );
179         }
180         else if ( e.getSource() == _ok_button ) {
181             parseSize();
182             _option = OK_OPTION;
183             _font = new Font( _type, _style, _size );
184             setVisible( false );
185         }
186         else if ( e.getSource() == _cancel_button ) {
187             _option = CANCEL_OPTION;
188             setVisible( false );
189         }
190     }
191
192     @Override
193     public Font getFont() {
194         return _font;
195     }
196
197     public String getFontName() {
198         return _font.getFontName();
199     }
200
201     public int getFontSize() {
202         return _font.getSize();
203     }
204
205     public int getFontStyle() {
206         return _font.getStyle();
207     }
208
209     private void parseSize() {
210         try {
211             _size = ( Integer.parseInt( _size_tf.getText().trim() ) );
212         }
213         catch ( final Exception ex ) {
214             // Ignore.
215         }
216         if ( _size < 1 ) {
217             _size = 1;
218         }
219     }
220
221     @Override
222     public void setFont( final Font font ) {
223         _font = font;
224     }
225
226     private void setScrollPos( final JScrollPane sp, final JList list, final int index ) {
227         final int unit_size = sp.getVerticalScrollBar().getMaximum() / list.getModel().getSize();
228         sp.getVerticalScrollBar().setValue( ( index - 2 ) * unit_size );
229     }
230
231     public int showDialog( final Component parent, final String title ) {
232         boolean found = false;
233         _option = CANCEL_OPTION;
234         setTitle( title );
235         _test_tf.setFont( new Font( _type, _style, _size ) );
236         for( int i = 0; i < _font_list.getModel().getSize(); i++ ) {
237             _font_list.setSelectedIndex( i );
238             if ( _font.getFamily().equals( _font_list.getSelectedValue() ) ) {
239                 found = true;
240                 setScrollPos( _font_jsp, _font_list, i );
241                 break;
242             }
243         }
244         if ( !found ) {
245             _font_list.clearSelection();
246         }
247         _style_list.setSelectedIndex( _font.getStyle() );
248         found = false;
249         for( int i = 0; i < _size_list.getModel().getSize(); i++ ) {
250             _size_list.setSelectedIndex( i );
251             if ( _font.getSize() <= Integer.parseInt( ( String ) _size_list.getSelectedValue() ) ) {
252                 found = true;
253                 setScrollPos( _size_jsp, _size_list, i );
254                 break;
255             }
256         }
257         if ( !found ) {
258             _size_list.clearSelection();
259         }
260         setLocationRelativeTo( parent );
261         setVisible( true );
262         return _option;
263     }
264
265     public void valueChanged( final ListSelectionEvent e ) {
266         if ( e.getSource() == _font_list ) {
267             if ( _font_list.getSelectedValue() != null ) {
268                 _fonts_tf.setText( ( ( String ) ( _font_list.getSelectedValue() ) ) );
269             }
270             _type = _fonts_tf.getText();
271             _test_tf.setFont( new Font( _type, _style, _size ) );
272         }
273         else if ( e.getSource() == _style_list ) {
274             _style_tf.setText( ( ( String ) ( _style_list.getSelectedValue() ) ) );
275             if ( _style_tf.getText().equals( REGULAR ) ) {
276                 _style = 0;
277             }
278             else if ( _style_tf.getText().equals( BOLD ) ) {
279                 _style = 1;
280             }
281             else if ( _style_tf.getText().equals( ITALIC ) ) {
282                 _style = 2;
283             }
284             else if ( _style_tf.getText().equals( BOLD_ITALIC ) ) {
285                 _style = 3;
286             }
287             _test_tf.setFont( new Font( _type, _style, _size ) );
288         }
289         else if ( e.getSource() == _size_list ) {
290             if ( _size_list.getSelectedValue() != null ) {
291                 _size_tf.setText( ( ( String ) ( _size_list.getSelectedValue() ) ) );
292             }
293             _size = ( Integer.parseInt( _size_tf.getText().trim() ) );
294             _test_tf.setFont( new Font( _type, _style, _size ) );
295         }
296     }
297 }