/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.general.ui.preferences;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.approximatrix.draco.application.preferences.EditorPreferences;

public class EditorPreferencesPanel extends JPanel implements PreferencesPanel, ChangeListener, ActionListener {

	public static final String TAB_TITLE = "Script Editor";
	
	private static final String HELP_KEY = "editorpreferences";
	
	private EditorPreferences prefs = null;
	private JComboBox fontNames = null;
	private JSpinner fontSize = null;
	private SpinnerNumberModel smFontSize = null;
	private JCheckBox antialiasingCheckBox = null;
	
	public EditorPreferencesPanel(EditorPreferences ep) {
		prefs = ep;
		initialize();
	}
	
	private void initialize() {
		this.setLayout(new BorderLayout());
		
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Editor Font"));
		panel.add(getFontNameBox());
		panel.add(getFontSizeSpinner());
		
		this.add(panel,BorderLayout.NORTH);
		
		this.add(getAntiAliasingCheckBox(),BorderLayout.SOUTH);
	}
	
	private JCheckBox getAntiAliasingCheckBox() {
		if(antialiasingCheckBox == null) {
			antialiasingCheckBox = new JCheckBox("Enable Antialiasing");
			antialiasingCheckBox.setSelected(prefs.isAntiAliasing());
			antialiasingCheckBox.addChangeListener(this);
		}
		return antialiasingCheckBox;
	}
	
	private JComboBox getFontNameBox() {
		if(fontNames == null) {
			String[] fontList = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
			fontNames = new JComboBox(fontList);
			fontNames.setSelectedItem(prefs.getFontName());
			fontNames.addActionListener(this);
		}
		return fontNames;
	}
	
	private JSpinner getFontSizeSpinner() {
		if(fontSize == null) {
			smFontSize = new SpinnerNumberModel(prefs.getFontSize(),6,48,1);
			smFontSize.addChangeListener(this);
			fontSize = new JSpinner(smFontSize);
		}
		return fontSize;
	}
	
	public String getHelpKey() {
		return HELP_KEY;
	}

	public void stateChanged(ChangeEvent arg0) {
		if(arg0.getSource() == getAntiAliasingCheckBox())
			prefs.setAntiAliasing(getAntiAliasingCheckBox().isSelected());
		else if(arg0.getSource() == getFontSizeSpinner())
			prefs.setFontSize(smFontSize.getNumber().intValue());
	}

	public void actionPerformed(ActionEvent arg0) {
		prefs.setFontName(getFontNameBox().getSelectedItem().toString());
	}

}
