/*
 *  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.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListDataListener;

import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.application.preferences.UIPreferences;

public class UIPreferencesPanel extends JPanel implements PreferencesPanel,ChangeListener,ItemListener {

	public static final String TAB_TITLE = "User Interface";
	
	private static final String HELP_KEY = "uipreferences";
	
	private UIPreferences prefs = null;
	private JSpinner iconSize = null;
	private JComboBox lnfBox = null;
	private LookAndFeelComboModel lnfModel = null;
	
	public UIPreferencesPanel(UIPreferences prefs) {
		this.prefs = prefs;
		initialize();
	}
	
	private void initialize() {
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.add(new JLabel("Toolbar Icon Size: "));
		panel.add(getIconSpinner());
		this.add(panel);
		
		JPanel lnfAndMessagePanel = new JPanel();
		lnfAndMessagePanel.setLayout(new BoxLayout(lnfAndMessagePanel,BoxLayout.Y_AXIS));
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(new FlowLayout());
		panel2.add(new JLabel("Look and Feel: "));
		panel2.add(getLNFComboBox());
		
		lnfAndMessagePanel.add(panel2);
		
		JLabel label = new JLabel("<html><body><p><b>Note</b>: The change to the look and feel</p>"+
                "<p>of the program will not take effect</p>"+
                "<p>until "+ApplicationManager.APP_NAME+" is restarted.</p></body></html>");
		JPanel lpanel = new JPanel();
		lpanel.setLayout(new FlowLayout());
		lpanel.add(label);
		
		lnfAndMessagePanel.add(lpanel);
		
		panel.add(lnfAndMessagePanel);
	}
	
	private JSpinner getIconSpinner() {
		if(iconSize == null) {
			iconSize = new JSpinner(new SpinnerNumberModel(prefs.getIconSize(),1,prefs.getMaxIconSize(),1));
			iconSize.addChangeListener(this);
		}
		return iconSize;
	}
	
	private JComboBox getLNFComboBox() {
		if(lnfBox == null) {
			lnfModel = new LookAndFeelComboModel(prefs.getLookAndFeel());
			lnfBox = new JComboBox(lnfModel);
			lnfBox.setSelectedItem(prefs.getLookAndFeel());
			lnfBox.addItemListener(this);
		}
		return lnfBox;
	}

	public void stateChanged(ChangeEvent e) {
		if(e.getSource() == getIconSpinner())
			prefs.setIconSize(((SpinnerNumberModel)getIconSpinner().getModel()).getNumber().intValue());
		
	}

	public void itemStateChanged(ItemEvent e) {
		if(e.getSource() == getLNFComboBox())
			prefs.setLookAndFeel(lnfModel.getSelectedClass());
	}

	public String getHelpKey() {
		return HELP_KEY;
	}
	
	private class LookAndFeelComboModel implements ComboBoxModel {
		
		private String selected = null;
		
		public LookAndFeelComboModel(String currentClass) {
			selected = currentClass;
		}

		public String getSelectedClass() {
			return selected;
		}
		
		public Object getSelectedItem() {
			for(int i=0;i<UIManager.getInstalledLookAndFeels().length;i++) {
				LookAndFeelInfo lnf = UIManager.getInstalledLookAndFeels()[i];
				if(lnf.getClassName().equals(selected)) {
					return lnf.getName();
				}
			}
			return null;
		}

		public void setSelectedItem(Object arg0) {
			for(int i=0;i<UIManager.getInstalledLookAndFeels().length;i++) {
				LookAndFeelInfo lnf = UIManager.getInstalledLookAndFeels()[i];
				if(lnf.getName().equals(arg0)) {
					selected = lnf.getClassName();
					break;
				}
			}
		}

		public Object getElementAt(int arg0) {
			try {
				LookAndFeelInfo lnf = UIManager.getInstalledLookAndFeels()[arg0];
				return lnf.getName();
			} catch(Exception e) {
				return "None";
			}
		}

		public int getSize() {
			return UIManager.getInstalledLookAndFeels().length;
		}

		public void removeListDataListener(ListDataListener arg0) {}
		public void addListDataListener(ListDataListener arg0) {}
		
	}
}
