/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 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.plot.ui.proppanels;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.approximatrix.charting.swing.BufferedChartPanel;
import com.approximatrix.charting.model.MultiScatterDataModel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;

public class SeriesPropPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel1 = null;

	private BufferedChartPanel panel = null;
	private MultiScatterDataModel model = null;
	private JComboBox jComboBox = null;
	private JPanel jPanel2 = null;
	private JCheckBox marker_checkbox = null;
	private JCheckBox line_checkbox = null;
	
	/**
	 * This is the default constructor
	 */
	public SeriesPropPanel(BufferedChartPanel incoming_panel) {
		super();
		if(incoming_panel == null) {
			System.err.println("SeriesPropPanel: Panel is null!");
		}
		
		panel = incoming_panel;
		model = (MultiScatterDataModel)incoming_panel.getChartDataModel();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		
		this.setName("Series Properties");
		this.add(getJPanel1(), BorderLayout.NORTH);
		this.add(getJPanel2(), BorderLayout.CENTER);
		update_fields_from_combo();
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			JLabel jLabel = new JLabel();
			jLabel.setText("Available Series: ");
			jPanel1.add(jLabel, null);
			//jPanel1.add(getAvail_series_combo(), null);
			jPanel1.add(getJComboBox(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setPreferredSize(new java.awt.Dimension(180,21));
			jComboBox.addItemListener(new java.awt.event.ItemListener() { 
				public void itemStateChanged(java.awt.event.ItemEvent e) {    
					update_fields_from_combo();
					panel.revalidate();
				}
			});
			for(int i=0;i<model.getDataSetNumber();i++) {
				jComboBox.addItem((Object)model.getDataSetName(i));
			}
			jComboBox.setSelectedIndex(0);
		}
		return jComboBox;
	}
	
	private void update_fields_from_combo() {
		// Based on name, get plot properties
		String selected_name = getJComboBox().getSelectedItem().toString();
		
		// Determine if there is a line
		getMarker_checkbox().setSelected(model.getSeriesMarker(selected_name));
		getLine_checkbox().setSelected(model.getSeriesLine(selected_name));	
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(getMarker_checkbox(), gridBagConstraints);
			jPanel2.add(getLine_checkbox(), gridBagConstraints1);
		}
		return jPanel2;
	}

	/**
	 * This method initializes marker_checkbox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getMarker_checkbox() {
		if (marker_checkbox == null) {
			marker_checkbox = new JCheckBox();
			marker_checkbox.setText("Show Markers");
			marker_checkbox.addItemListener(new java.awt.event.ItemListener() { 
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					String selected_name = getJComboBox().getSelectedItem().toString();
					model.setSeriesMarker(selected_name,marker_checkbox.isSelected());
					model.fireChartDataModelChangedEvent(SeriesPropPanel.this);
					//panel.repaint();
				}
			});
		}
		return marker_checkbox;
	}

	/**
	 * This method initializes line_checkbox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getLine_checkbox() {
		if (line_checkbox == null) {
			line_checkbox = new JCheckBox();
			line_checkbox.setText("Show Line");
			line_checkbox.addItemListener(new java.awt.event.ItemListener() { 
				public void itemStateChanged(java.awt.event.ItemEvent e) {    
					String selected_name = getJComboBox().getSelectedItem().toString();
					model.setSeriesLine(selected_name,line_checkbox.isSelected());
					model.fireChartDataModelChangedEvent(SeriesPropPanel.this);
					//panel.repaint();
				}
			});
		}
		return line_checkbox;
	}

}
