/**
 * GetTime
 * A small Java application that sends a request to an Internet Time Server
 * and display the fields of the returned NTP packet. This can be used to 
 * make sure all fields generated by a time server are correct.
 * <p>
 * It may also be used as a learning tool to better understand 
 * "Network Time Protocol" (NTP) and "User Datagram Protocol" (UDP) is Java.
 * <p>
 * My source code is given freely and WITHOUT copyright.  You may copy, modify
 * and distribute it as if it were your own code.  However, the NtpMessage.java
 * source code file is copyrighted by Adam Buckley 2004.
 * <p>
 * @author Anthony Mack
 */
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import java.awt.Color;

public abstract class LablePanel extends JScrollPane
	{
	//
	// Types
	//
	private class LabelValue
		{
		JLabel label;
		JTextField dataField;
		}
	
	//
	// Constants
	//
	public static final Font 	LABEL_FONT 	= new Font( "Serif", Font.PLAIN, 14 );
	public static final Font 	DATA_FONT 	= new Font( "SansSerif", Font.BOLD, 16 );


	//
	// Member Data
	//
	JPanel	view;
	private LabelValue[] 			labelValues = null;
	private int								gridy 			= 0;
	Dimension 								preferredSize;

	//
	// Constructors
	//
	public LablePanel()
		{
		super( new JPanel(new GridBagLayout()) );

		view = (JPanel) getViewport().getView();
		setBackground( Color.WHITE );
		view.setBackground( Color.WHITE );
		JTextField dummy = new JTextField();
		dummy.setFont( DATA_FONT );
		dummy.setColumns( 10 );
		preferredSize = dummy.getPreferredSize();
		}

	//
	// Methods
	//
	
	/**
	 * Add components above and below the NTP Packet. 
	 */
	protected void  AddComponent( Component component )
		{
		// Build some constraints.
		GridBagConstraints centerAligned = new GridBagConstraints();
		centerAligned.anchor = GridBagConstraints.CENTER;
		centerAligned.gridx = 0;
		centerAligned.gridy = gridy;
		centerAligned.gridwidth = 2;
		gridy++;
		
		view.add( component, centerAligned );
		}

	/**
	 * Build the panel of NTP labels.
	 */
	protected void setupNTPLabels( String[] names )
		{
		labelValues = new LabelValue[names.length];

		// Build some constraints.
		GridBagConstraints leftSideRightAligned = new GridBagConstraints();
		leftSideRightAligned.anchor = GridBagConstraints.EAST;
		leftSideRightAligned.gridx = 0;

		GridBagConstraints rightSideLeftAligned = new GridBagConstraints();
		rightSideLeftAligned.anchor = GridBagConstraints.WEST;
		rightSideLeftAligned.gridx = 1;

		// Build the "NTP Packet" panel with a title
		JPanel  NTPPacketPanel = new JPanel( new GridBagLayout() );
		NTPPacketPanel.setBorder( BorderFactory.createEmptyBorder(10, 0, 10, 0) );
		NTPPacketPanel.setLayout( new BoxLayout( NTPPacketPanel, BoxLayout.Y_AXIS ) );
		NTPPacketPanel.setBackground( Color.WHITE );
		TitledBorder tlTitled = BorderFactory.createTitledBorder( "NTP Packet Fields" );
		tlTitled.setTitleJustification( TitledBorder.LEFT );
		tlTitled.setTitlePosition( TitledBorder.DEFAULT_POSITION );
		JPanel  innerPane = new JPanel( new GridBagLayout() );
		innerPane.setBackground( Color.WHITE );
		
		// Loop thru all the NTP fields.
		for( int i = 0; i < names.length; i++ )
			{
			labelValues[i] = new LabelValue();
			labelValues[i].label = new JLabel( names[i] );
			labelValues[i].label.setFont( LABEL_FONT );
			labelValues[i].label.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
			leftSideRightAligned.gridy = i;
			innerPane.add( labelValues[i].label, leftSideRightAligned );

			labelValues[i].dataField = new JTextField( "    " );
			labelValues[i].dataField.setFont( DATA_FONT );
			labelValues[i].dataField.setEditable( false );
			labelValues[i].dataField.setBackground( new Color(240, 240, 250) );
			labelValues[i].dataField.setPreferredSize( preferredSize );
			rightSideLeftAligned.gridy = i;
			innerPane.add( labelValues[i].dataField, rightSideLeftAligned );
			}
		innerPane.setBorder( tlTitled );
		NTPPacketPanel.add( Box.createRigidArea(new Dimension(0,10)) );
		NTPPacketPanel.add( innerPane );
		
		GridBagConstraints centerAligned = new GridBagConstraints();
		centerAligned.anchor = GridBagConstraints.CENTER;
		centerAligned.gridx = 0;
		centerAligned.gridy = gridy;
		centerAligned.gridwidth = 2;
		
		view.add( NTPPacketPanel, centerAligned );
		gridy++;
		}

	/**
	 * Set the NTP field values.
	 */
	protected void setValues( String[] values )
		{
		// Loop thru all the labelValue pairs in the panel.
		int i = 0;
		for( LabelValue labelValue : labelValues )
			{
			if( i >= values.length )
				break; // not enough values passed in.
			labelValue.dataField.setText( " " + values[i] + " " );
			labelValue.dataField.setPreferredSize( null ); // Try auto sizing.
			if( labelValue.dataField.getPreferredSize().width < preferredSize.width )
				labelValue.dataField.setPreferredSize( preferredSize ); // Nope, too small.
			i++;
			}
		}

	}

