/*
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin@tvbrowser.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2012-08-28 10:16:12 +0200 (Di, 28 Aug 2012) $
 *   $Author: ds10 $
 * $Revision: 7508 $
 */

package util.ui;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;

public class FontChooserPanel extends JPanel {

  /** The localizer for this class. */
  private static final util.ui.Localizer mLocalizer = util.ui.Localizer.getLocalizerFor(FontChooserPanel.class);

  private JComboBox mFontCB, mStyleCB;

  private JSpinner mSizeSpinner;

  private JLabel mTitle;

  private static java.awt.GraphicsEnvironment ge = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();

  private static final String[] FONTNAMES = ge.getAvailableFontFamilyNames();

  private static final String[] FONTSTYLES = { mLocalizer.msg("plain", "plain"), mLocalizer.msg("bold", "bold"),
      mLocalizer.msg("italic", "italic") };

  private static final int FONTSIZE_MIN = 8;

  private static final int FONTSIZE_MAX = 40;

  /**
   * create a new font chooser with optional style selection
   * @param title title or <code>null</code>
   * @param font selected font
   * @param style enable style selection
   */
  public FontChooserPanel(String title, Font font, boolean style) {
    setLayout(new BorderLayout());
    if (title != null) {
      mTitle = new JLabel(title);
      add(mTitle, BorderLayout.NORTH);
    }
    
    FormLayout layout = new FormLayout("min:grow,2dlu,min","default");
    
    JPanel innerPanel = new JPanel(layout);
    innerPanel.setBorder(BorderFactory.createEmptyBorder());

    CellConstraints cc = new CellConstraints();
    
    mFontCB = new JComboBox(FONTNAMES);
    mStyleCB = new JComboBox(FONTSTYLES);
    mSizeSpinner = new JSpinner(new SpinnerNumberModel(FONTSIZE_MIN, FONTSIZE_MIN, FONTSIZE_MAX, 1));

    int column = 1;
    
    innerPanel.add(mFontCB, cc.xy(column, 1));

    if (style) {
      column += 2;
      
      layout.appendColumn(ColumnSpec.decode("2dlu"));
      layout.appendColumn(ColumnSpec.decode("min"));
      
      innerPanel.add(mStyleCB, cc.xy(column, 1));
    }
    
    column += 2;

    innerPanel.add(mSizeSpinner, cc.xy(column, 1));

    add(innerPanel, BorderLayout.CENTER);

    if (font != null) {
      selectFont(font);
    }
  }

  /**
   * create a font chooser with style selection
   * @param title title or <code>null</code>
   * @param font
   */
  public FontChooserPanel(String title, Font font) {
    this(title, font, true);
  }

  /**
   * Creates a Font-Chooser without Title-Bar, but with style selection
   *
   * @param font
   * @since 2.2
   */
  public FontChooserPanel(Font font) {
    this(null, font, true);
  }

  /**
   * create a font chooser without title bar
   * @param font
   * @param enableStyleSelection show style selection or not
   * @since 3.0
   */
  public FontChooserPanel(final Font font, boolean enableStyleSelection) {
    this(null, font, enableStyleSelection);
  }

  public FontChooserPanel(String title) {
    this(title, null);
  }

  public void selectFont(Font font) {
    for (int i = 0; i < mFontCB.getItemCount(); i++) {
      String item = (String) mFontCB.getItemAt(i);
      if (item.equals(font.getName())) {
        mFontCB.setSelectedIndex(i);
        break;
      }
    }

    mSizeSpinner.setValue(font.getSize());

    if (font.getStyle() == Font.BOLD) {
      mStyleCB.setSelectedIndex(1);
    } else if (font.getStyle() == Font.ITALIC) {
      mStyleCB.setSelectedIndex(2);
    }

  }

  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    mFontCB.setEnabled(enabled);
    mStyleCB.setEnabled(enabled);
    mSizeSpinner.setEnabled(enabled);
    if (mTitle != null) {
      mTitle.setEnabled(enabled);
    }
  }

  public Font getChosenFont() {
    Font result;
    int style;
    int inx = mStyleCB.getSelectedIndex();
    if (inx == 0) {
      style = Font.PLAIN;
    } else if (inx == 1) {
      style = Font.BOLD;
    } else {
      style = Font.ITALIC;
    }
    result = new Font((String) mFontCB.getSelectedItem(), style, ((Integer) mSizeSpinner.getValue()).intValue());

    return result;
  }
}
