KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > font > FontChooser


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package salsa.font;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import javax.swing.*;
28 import javax.swing.border.*;
29 import javax.swing.event.*;
30 import javax.swing.text.*;
31 import salsa.*;
32
33 public class FontChooser extends JDialog
34 {
35
36    JCheckBox _boldCheck;
37
38    JComboBox _colorCombo;
39    OpenList _fontNameList;
40    OpenList _fontSizeList;
41    JCheckBox _italicCheck;
42    int _option = JOptionPane.CLOSED_OPTION;
43    JLabel _preview;
44    JCheckBox _strikethroughCheck;
45
46    MutableAttributeSet _styleAttributes;
47    JCheckBox _subscriptCheck;
48    JCheckBox _superscriptCheck;
49    JCheckBox _underlineCheck;
50
51    public FontChooser( JFrame parent, String JavaDoc fontNames[], String JavaDoc fontSizes[] )
52    {
53       super( parent, "Font", true );
54
55       getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) );
56
57       JPanel p = new JPanel( new GridLayout( 1, 2, 10, 2 ) );
58       p.setBorder( new TitledBorder( new EtchedBorder(), "Font" ) );
59
60       _fontNameList = new OpenList( fontNames, "Name:" );
61       p.add( _fontNameList );
62
63       _fontSizeList = new OpenList( fontSizes, "Size:" );
64       p.add( _fontSizeList );
65
66       getContentPane().add( p );
67
68       p = new JPanel( new GridLayout( 2, 3, 10, 5 ) );
69       p.setBorder( new TitledBorder( new EtchedBorder(), "Style" ) );
70
71       _boldCheck = new JCheckBox( "Bold" );
72       p.add( _boldCheck );
73
74       _italicCheck = new JCheckBox( "Italic" );
75       p.add( _italicCheck );
76
77       _underlineCheck = new JCheckBox( "Underline" );
78       p.add( _underlineCheck );
79
80       _strikethroughCheck = new JCheckBox( "Strikethrough" );
81       p.add( _strikethroughCheck );
82
83       _subscriptCheck = new JCheckBox( "Subscript" );
84       p.add( _subscriptCheck );
85
86       _superscriptCheck = new JCheckBox( "Superscript" );
87       p.add( _superscriptCheck );
88
89       getContentPane().add( p );
90
91       getContentPane().add( Box.createVerticalStrut( 5 ) );
92       p = new JPanel();
93       p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) );
94       p.add( Box.createHorizontalStrut( 10 ) );
95       p.add( new JLabel( "Color:" ) );
96       p.add( Box.createHorizontalStrut( 20 ) );
97
98       _colorCombo = new JComboBox();
99       int values[] = new int[]{0, 128, 192, 255};
100       for( int r = 0; r < values.length; r++ )
101          for( int g = 0; g < values.length; g++ )
102             for( int b = 0; b < values.length; b++ )
103             {
104                Color color = new Color( values[r], values[g], values[b] );
105                _colorCombo.addItem( color );
106             }
107
108       _colorCombo.setRenderer( new ColorListCellRenderer() );
109       p.add( _colorCombo );
110       p.add( Box.createHorizontalStrut( 10 ) );
111       getContentPane().add( p );
112
113       p = new JPanel( new BorderLayout() );
114       p.setBorder( new TitledBorder( new EtchedBorder(), "Preview" ) );
115       _preview = new JLabel( "The quick brown fox jumps over the lazy dog.", JLabel.CENTER );
116       _preview.setBackground( Color.WHITE );
117       _preview.setForeground( Color.BLACK );
118       _preview.setOpaque( true );
119       _preview.setBorder( new LineBorder( Color.BLACK ) );
120       _preview.setPreferredSize( new Dimension( 120, 40 ) );
121       p.add( _preview, BorderLayout.CENTER );
122       getContentPane().add( p );
123
124       JButton okButton = new JButton( "Ok" );
125       okButton.addActionListener(
126          new ActionListener()
127          {
128             public void actionPerformed( ActionEvent ev )
129             {
130                _option = JOptionPane.OK_OPTION;
131                setVisible( false );
132             }
133          } );
134
135       JButton cancelButton = new JButton( "Cancel" );
136       cancelButton.addActionListener(
137          new ActionListener()
138          {
139             public void actionPerformed( ActionEvent ev )
140             {
141                _option = JOptionPane.CANCEL_OPTION;
142                setVisible( false );
143             }
144          } );
145
146       p = new JPanel( new FlowLayout() );
147       JPanel p1 = new JPanel( new GridLayout( 1, 2, 10, 2 ) );
148       p1.add( okButton );
149       p1.add( cancelButton );
150       p.add( p1 );
151       getContentPane().add( p );
152
153       pack();
154       setResizable( false );
155
156       ListSelectionListener listSelListener =
157          new ListSelectionListener()
158          {
159             public void valueChanged( ListSelectionEvent ev )
160             {
161                updatePreview();
162             }
163          };
164
165       _fontNameList.addListSelectionListener( listSelListener );
166       _fontSizeList.addListSelectionListener( listSelListener );
167
168       ActionListener actionListener =
169          new ActionListener()
170          {
171             public void actionPerformed( ActionEvent ev )
172             {
173                updatePreview();
174             }
175          };
176
177       _boldCheck.addActionListener( actionListener );
178       _italicCheck.addActionListener( actionListener );
179
180       _colorCombo.addActionListener( actionListener );
181    }
182
183    public void setAttributes( AttributeSet attr )
184    {
185       _styleAttributes = new SimpleAttributeSet( attr );
186
187       String JavaDoc fontName = StyleConstants.getFontFamily( attr );
188       _fontNameList.setSelected( fontName );
189
190       int fontSize = StyleConstants.getFontSize( attr );
191       _fontSizeList.setSelectedInt( fontSize );
192
193       _boldCheck.setSelected( StyleConstants.isBold( attr ) );
194       _italicCheck.setSelected( StyleConstants.isItalic( attr ) );
195       _underlineCheck.setSelected( StyleConstants.isUnderline( attr ) );
196       _strikethroughCheck.setSelected( StyleConstants.isStrikeThrough( attr ) );
197       _subscriptCheck.setSelected( StyleConstants.isSubscript( attr ) );
198       _superscriptCheck.setSelected( StyleConstants.isSuperscript( attr ) );
199
200       _colorCombo.setSelectedItem( StyleConstants.getForeground( attr ) );
201       updatePreview();
202    }
203
204    public AttributeSet getAttributes()
205    {
206       if( _styleAttributes == null )
207          return null;
208
209       StyleConstants.setFontFamily( _styleAttributes,
210             _fontNameList.getSelected() );
211
212       StyleConstants.setFontSize( _styleAttributes,
213             _fontSizeList.getSelectedInt() );
214
215       StyleConstants.setBold( _styleAttributes, _boldCheck.isSelected() );
216       StyleConstants.setItalic( _styleAttributes, _italicCheck.isSelected() );
217       StyleConstants.setUnderline( _styleAttributes, _underlineCheck.isSelected() );
218       StyleConstants.setStrikeThrough( _styleAttributes, _strikethroughCheck.isSelected() );
219       StyleConstants.setSubscript( _styleAttributes, _subscriptCheck.isSelected() );
220       StyleConstants.setSuperscript( _styleAttributes, _superscriptCheck.isSelected() );
221       StyleConstants.setForeground( _styleAttributes, ( Color ) _colorCombo.getSelectedItem() );
222
223       return _styleAttributes;
224    }
225
226    public int getOption()
227    {
228       return _option;
229    }
230
231    private void updatePreview()
232    {
233       String JavaDoc fontName = _fontNameList.getSelected();
234       int fontSize = _fontSizeList.getSelectedInt();
235       if( fontSize <= 0 )
236          return;
237
238       int fontStyle = Font.PLAIN;
239       if( _boldCheck.isSelected() )
240          fontStyle |= Font.BOLD;
241       if( _italicCheck.isSelected() )
242          fontStyle |= Font.ITALIC;
243
244       Font font = new Font( fontName, fontStyle, fontSize );
245       _preview.setFont( font );
246
247       Color color = ( Color ) _colorCombo.getSelectedItem();
248       _preview.setForeground( color );
249       _preview.repaint();
250    }
251
252 }
253
Popular Tags