//==============================================================================
// The Society of Actuaries (SOA) makes this computer model and its computer
// code available to its members for educational purposes. The model and its
// code may be modified and redistributed without prior authorization from the
// SOA. The SOA distributes this computer model and its computer code 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.
//==============================================================================

package implementation;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class UserPanelTab_PersonGoals extends JPanel
{
	private static final long serialVersionUID = 3L;

	JLabel 	label_Title, label_A1;
	JTable component_A1;
	JTableHeader component_A1Header;
	TableModel goalDistributionTableModel;
	
	public UserPanelTab_PersonGoals()
	{
		// Set the layout manager for the pane to be a "grid bag".
		// Grid bag is a flexible and easily modifiable layout format.		
		// (see: www.ugrad.cs.ubc.ca/~cs219/CourseNotes/Swing/swing-LayoutManagers-GridBag.html)
		// The Person Goals panel has one column.
		GridBagLayout gridBagLayout = new GridBagLayout();
		setLayout(gridBagLayout);
		
		// Title label
		label_Title = new JLabel("<html><b><i>Person goal parameters</i></b><br>" +
                "<font color=\"blue\">(After making a change, be sure to press ENTER.)</font></html>");
		GridBagConstraints c = new GridBagConstraints();
		c.gridx 		= 0;
		c.gridy 		= 0;
		c.gridwidth 	= 1;
		c.gridheight 	= 1;
		c.weightx 		= 1.0;
		c.weighty 		= 1.0;
		c.anchor 		= GridBagConstraints.WEST;
		c.fill 			= GridBagConstraints.NONE;
		c.insets 		= new Insets(0,10,10,0);
		c.ipadx			= 0;
		c.ipady			= 0;
		gridBagLayout.setConstraints(label_Title, c);
		add(label_Title);	

		// A1 label
		JLabel label_A1 = new JLabel("A1. Person goal priority distribution:");
		label_A1.setToolTipText("<html>Set the distribution of Person agent goal priorities.<br>" +
											"Be sure that each row totals 1.00.</html>");
		c.gridx 		= 0;
		c.gridy 		= 1;
		c.gridwidth 	= 1;
		c.gridheight 	= 1;
		c.weightx 		= 1.0;
		c.weighty 		= 1.0;
		c.anchor 		= GridBagConstraints.WEST;
		c.fill 			= GridBagConstraints.NONE;
		c.insets 		= new Insets(0,10,0,0);
		c.ipadx			= 0;
		c.ipady			= 0;
		gridBagLayout.setConstraints(label_A1, c);
		add(label_A1);

		// A1 component header
		Object[][] goalPriorityTableData = {
				{ " Maximize income", 	SimulationUserPanel.getPersonGoalPriorities()[0][0],
										SimulationUserPanel.getPersonGoalPriorities()[0][1],
										SimulationUserPanel.getPersonGoalPriorities()[0][2],
										SimulationUserPanel.getPersonGoalPriorities()[0][3],
										SimulationUserPanel.getPersonGoalPriorities()[0][4]},
				{ " Increase health", 	SimulationUserPanel.getPersonGoalPriorities()[1][0],
										SimulationUserPanel.getPersonGoalPriorities()[1][1],
										SimulationUserPanel.getPersonGoalPriorities()[1][2],
										SimulationUserPanel.getPersonGoalPriorities()[1][3],
										SimulationUserPanel.getPersonGoalPriorities()[1][4]},
				{ " Conform", 			SimulationUserPanel.getPersonGoalPriorities()[2][0],
										SimulationUserPanel.getPersonGoalPriorities()[2][1],
										SimulationUserPanel.getPersonGoalPriorities()[2][2],
										SimulationUserPanel.getPersonGoalPriorities()[2][3],
										SimulationUserPanel.getPersonGoalPriorities()[2][4]},
				{ " Follow policy", 	SimulationUserPanel.getPersonGoalPriorities()[3][0],
										SimulationUserPanel.getPersonGoalPriorities()[3][1],
										SimulationUserPanel.getPersonGoalPriorities()[3][2],
										SimulationUserPanel.getPersonGoalPriorities()[3][3],
										SimulationUserPanel.getPersonGoalPriorities()[3][4]},
				{ " Follow advertising",SimulationUserPanel.getPersonGoalPriorities()[4][0],
										SimulationUserPanel.getPersonGoalPriorities()[4][1],
										SimulationUserPanel.getPersonGoalPriorities()[4][2],
										SimulationUserPanel.getPersonGoalPriorities()[4][3],
										SimulationUserPanel.getPersonGoalPriorities()[4][4]}
											};
		
		String[] goalPriorityTableHeader = {"Goal", "1st Priority", "2nd Priority", "3rd Priority", "4th Priority", "5th Priority"};

		component_A1 = new JTable(new DefaultTableModel(goalPriorityTableData, goalPriorityTableHeader))
		{
			private static final long serialVersionUID = 7L;
			// The purpose of this override is to make the first column not editable.
			@Override
			public boolean isCellEditable(int rowIndex, int colIndex)
			{
				if (colIndex == 0) return false;
				else return true;
			}
		};
		component_A1.getColumnModel().getColumn(0).setPreferredWidth(150);
		component_A1.setGridColor(Color.LIGHT_GRAY);
		component_A1.setCellSelectionEnabled(true);
		component_A1.setToolTipText("<html>Set the distribution of Person agent goal priorities.<br>" +
											"Be sure that each row totals 1.00.</html>");
		component_A1Header = component_A1.getTableHeader();
		component_A1Header.setToolTipText("<html>Set the distribution of Person agent goal priorities.<br>" +
											"Be sure that each row totals 1.00.</html>");
		c.gridx 		= 0;
		c.gridy 		= 2;
		c.gridwidth 	= 1;
		c.gridheight 	= 1;
		c.weightx 		= 1.0;
		c.weighty 		= 1.0;
		c.anchor 		= GridBagConstraints.SOUTH;
		c.fill 			= GridBagConstraints.NONE;
		c.insets 		= new Insets(0,0,0,0);
		c.ipadx			= 0;
		c.ipady			= 0;
		gridBagLayout.setConstraints(component_A1Header, c);

		add(component_A1Header);

		// A1 component	
		c.gridx 		= 0;
		c.gridy 		= 3;
		c.gridwidth 	= 1;
		c.gridheight 	= 1;
		c.weightx 		= 1.0;
		c.weighty 		= 1.0;
		c.anchor 		= GridBagConstraints.NORTH;
		c.fill 			= GridBagConstraints.NONE;
		c.insets 		= new Insets(0,0,550,0);
		c.ipadx			= 0;
		c.ipady			= 0;
		gridBagLayout.setConstraints(component_A1, c);

		goalDistributionTableModel = component_A1.getModel();
		goalDistributionTableModel.addTableModelListener(new TableModelListener()
		{
			public void tableChanged(TableModelEvent event)
			{
				if (event.getType() == TableModelEvent.UPDATE)
				{
					double[][] personGoalPriorities = SimulationUserPanel.getPersonGoalPriorities();
					personGoalPriorities[event.getFirstRow()][event.getColumn()-1] =
							(Double) Double.valueOf((String) goalDistributionTableModel
									.getValueAt(event.getFirstRow(), event.getColumn()));
					SimulationUserPanel.setPersonGoalPriorities(personGoalPriorities);
					
					//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
					//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//					System.out.println("Person Goals A1. personGoalPriorities:  ");
//					for (int i = 0; i < 5; i++)
//					{
//						for (int j = 0; j < 5; j++)
//						{
//							System.out.print(SimulationUserPanel.getPersonGoalPriorities()[i][j] + " "); //Person Goals:  A1
//						}
//						System.out.println();
//					}
				    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  				    
				    
				}
			}//end tableChanged()
		}
		);

		add(component_A1);
		
	}//end UserPanelTab_PersonGoals()
	
}//end UserPanelTab_PersonGoals class
