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

package salsa.font;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;
import salsa.*;

public class FontChooser extends JDialog
{

   JCheckBox _boldCheck;

   JComboBox _colorCombo;
   OpenList _fontNameList;
   OpenList _fontSizeList;
   JCheckBox _italicCheck;
   int _option = JOptionPane.CLOSED_OPTION;
   JLabel _preview;
   JCheckBox _strikethroughCheck;

   MutableAttributeSet _styleAttributes;
   JCheckBox _subscriptCheck;
   JCheckBox _superscriptCheck;
   JCheckBox _underlineCheck;

   public FontChooser( JFrame parent, String fontNames[], String fontSizes[] )
   {
      super( parent, "Font", true );

      getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) );

      JPanel p = new JPanel( new GridLayout( 1, 2, 10, 2 ) );
      p.setBorder( new TitledBorder( new EtchedBorder(), "Font" ) );

      _fontNameList = new OpenList( fontNames, "Name:" );
      p.add( _fontNameList );

      _fontSizeList = new OpenList( fontSizes, "Size:" );
      p.add( _fontSizeList );

      getContentPane().add( p );

      p = new JPanel( new GridLayout( 2, 3, 10, 5 ) );
      p.setBorder( new TitledBorder( new EtchedBorder(), "Style" ) );

      _boldCheck = new JCheckBox( "Bold" );
      p.add( _boldCheck );

      _italicCheck = new JCheckBox( "Italic" );
      p.add( _italicCheck );

      _underlineCheck = new JCheckBox( "Underline" );
      p.add( _underlineCheck );

      _strikethroughCheck = new JCheckBox( "Strikethrough" );
      p.add( _strikethroughCheck );

      _subscriptCheck = new JCheckBox( "Subscript" );
      p.add( _subscriptCheck );

      _superscriptCheck = new JCheckBox( "Superscript" );
      p.add( _superscriptCheck );

      getContentPane().add( p );

      getContentPane().add( Box.createVerticalStrut( 5 ) );
      p = new JPanel();
      p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) );
      p.add( Box.createHorizontalStrut( 10 ) );
      p.add( new JLabel( "Color:" ) );
      p.add( Box.createHorizontalStrut( 20 ) );

      _colorCombo = new JComboBox();
      int values[] = new int[]{0, 128, 192, 255};
      for( int r = 0; r < values.length; r++ )
         for( int g = 0; g < values.length; g++ )
            for( int b = 0; b < values.length; b++ )
            {
               Color color = new Color( values[r], values[g], values[b] );
               _colorCombo.addItem( color );
            }

      _colorCombo.setRenderer( new ColorListCellRenderer() );
      p.add( _colorCombo );
      p.add( Box.createHorizontalStrut( 10 ) );
      getContentPane().add( p );

      p = new JPanel( new BorderLayout() );
      p.setBorder( new TitledBorder( new EtchedBorder(), "Preview" ) );
      _preview = new JLabel( "The quick brown fox jumps over the lazy dog.", JLabel.CENTER );
      _preview.setBackground( Color.WHITE );
      _preview.setForeground( Color.BLACK );
      _preview.setOpaque( true );
      _preview.setBorder( new LineBorder( Color.BLACK ) );
      _preview.setPreferredSize( new Dimension( 120, 40 ) );
      p.add( _preview, BorderLayout.CENTER );
      getContentPane().add( p );

      JButton okButton = new JButton( "Ok" );
      okButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent ev )
            {
               _option = JOptionPane.OK_OPTION;
               setVisible( false );
            }
         } );

      JButton cancelButton = new JButton( "Cancel" );
      cancelButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent ev )
            {
               _option = JOptionPane.CANCEL_OPTION;
               setVisible( false );
            }
         } );

      p = new JPanel( new FlowLayout() );
      JPanel p1 = new JPanel( new GridLayout( 1, 2, 10, 2 ) );
      p1.add( okButton );
      p1.add( cancelButton );
      p.add( p1 );
      getContentPane().add( p );

      pack();
      setResizable( false );

      ListSelectionListener listSelListener =
         new ListSelectionListener()
         {
            public void valueChanged( ListSelectionEvent ev )
            {
               updatePreview();
            }
         };

      _fontNameList.addListSelectionListener( listSelListener );
      _fontSizeList.addListSelectionListener( listSelListener );

      ActionListener actionListener =
         new ActionListener()
         {
            public void actionPerformed( ActionEvent ev )
            {
               updatePreview();
            }
         };

      _boldCheck.addActionListener( actionListener );
      _italicCheck.addActionListener( actionListener );

      _colorCombo.addActionListener( actionListener );
   }

   public void setAttributes( AttributeSet attr )
   {
      _styleAttributes = new SimpleAttributeSet( attr );

      String fontName = StyleConstants.getFontFamily( attr );
      _fontNameList.setSelected( fontName );

      int fontSize = StyleConstants.getFontSize( attr );
      _fontSizeList.setSelectedInt( fontSize );

      _boldCheck.setSelected( StyleConstants.isBold( attr ) );
      _italicCheck.setSelected( StyleConstants.isItalic( attr ) );
      _underlineCheck.setSelected( StyleConstants.isUnderline( attr ) );
      _strikethroughCheck.setSelected( StyleConstants.isStrikeThrough( attr ) );
      _subscriptCheck.setSelected( StyleConstants.isSubscript( attr ) );
      _superscriptCheck.setSelected( StyleConstants.isSuperscript( attr ) );

      _colorCombo.setSelectedItem( StyleConstants.getForeground( attr ) );
      updatePreview();
   }

   public AttributeSet getAttributes()
   {
      if( _styleAttributes == null )
         return null;

      StyleConstants.setFontFamily( _styleAttributes,
            _fontNameList.getSelected() );

      StyleConstants.setFontSize( _styleAttributes,
            _fontSizeList.getSelectedInt() );

      StyleConstants.setBold( _styleAttributes, _boldCheck.isSelected() );
      StyleConstants.setItalic( _styleAttributes, _italicCheck.isSelected() );
      StyleConstants.setUnderline( _styleAttributes, _underlineCheck.isSelected() );
      StyleConstants.setStrikeThrough( _styleAttributes, _strikethroughCheck.isSelected() );
      StyleConstants.setSubscript( _styleAttributes, _subscriptCheck.isSelected() );
      StyleConstants.setSuperscript( _styleAttributes, _superscriptCheck.isSelected() );
      StyleConstants.setForeground( _styleAttributes, ( Color ) _colorCombo.getSelectedItem() );

      return _styleAttributes;
   }

   public int getOption()
   {
      return _option;
   }

   private void updatePreview()
   {
      String fontName = _fontNameList.getSelected();
      int fontSize = _fontSizeList.getSelectedInt();
      if( fontSize <= 0 )
         return;

      int fontStyle = Font.PLAIN;
      if( _boldCheck.isSelected() )
         fontStyle |= Font.BOLD;
      if( _italicCheck.isSelected() )
         fontStyle |= Font.ITALIC;

      Font font = new Font( fontName, fontStyle, fontSize );
      _preview.setFont( font );

      Color color = ( Color ) _colorCombo.getSelectedItem();
      _preview.setForeground( color );
      _preview.repaint();
   }

}