/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.applet.components;

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

import org.bodington.net.*;


public class LoginDialog extends javax.swing.JDialog
	implements ActionListener
{
	JTextField ufield;
	JPasswordField pfield;
	JButton okbutton;
	JButton cancelbutton;

	String username = null;
	String password = null;

	boolean ok=false;

	public LoginDialog(Frame parent)
	{
		super(parent);
		
		JLabel label;
		JPanel buttonpanel;
		
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		
		constraints.insets = new Insets( 4, 4, 4, 4 );
		
		getContentPane().setLayout( layout );
		setTitle("Login");


		label = new JLabel( "Login to Bodington System" );
		constraints.gridwidth=2;
		constraints.gridx=0;
		constraints.gridy=0;
		constraints.anchor = GridBagConstraints.CENTER;
		layout.setConstraints( label, constraints );
		getContentPane().add( label );

		
		label = new JLabel( "User Name:" );
		constraints.gridwidth=1;
		constraints.gridx=0;
		constraints.gridy=1;
		constraints.anchor = GridBagConstraints.EAST;
		layout.setConstraints( label, constraints );
		getContentPane().add( label );

		ufield = new JTextField();
		ufield.setColumns( 20 );
		constraints.gridwidth=1;
		constraints.gridx=1;
		constraints.gridy=1;
		constraints.anchor = GridBagConstraints.WEST;
		layout.setConstraints( ufield, constraints );
		getContentPane().add( ufield );

		label = new JLabel( "Password:" );
		constraints.gridwidth=1;
		constraints.gridx=0;
		constraints.gridy=2;
		constraints.anchor = GridBagConstraints.EAST;
		layout.setConstraints( label, constraints );
		getContentPane().add( label );

		
		pfield = new JPasswordField();
		pfield.setColumns( 20 );
		constraints.gridwidth=1;
		constraints.gridx=1;
		constraints.gridy=2;
		constraints.anchor = GridBagConstraints.WEST;
		layout.setConstraints( pfield, constraints );
		getContentPane().add( pfield );

		buttonpanel = new JPanel( new FlowLayout() );
		okbutton = new JButton( "Login" );
		cancelbutton = new JButton( "Cancel" );
		buttonpanel.add( okbutton );
		buttonpanel.add( cancelbutton );
		constraints.gridwidth=2;
		constraints.gridx=0;
		constraints.gridy=3;
		constraints.anchor = GridBagConstraints.CENTER;
		layout.setConstraints( buttonpanel, constraints );
		getContentPane().add( buttonpanel );
		
		okbutton.addActionListener( this );
		cancelbutton.addActionListener( this );

		Dimension d = layout.preferredLayoutSize( this );

		setSize( d.width, d.height );

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		//}}
	}

	public void addNotify()
	{
		// Record the size of the window prior to calling parents addNotify.
		Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		Insets ins = getInsets();
		setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height);
		Component components[] = getContentPane().getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(ins.left, ins.top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

	// Used for addNotify check.
	boolean fComponentsAdjusted = false;

	public LoginDialog(String title)
	{
		this();
		setTitle(title);
	}

	public LoginDialog()
	{
		this((Frame)null);
	}


	public void setVisible(boolean b)
	{
		if(b)
		{
			setLocation(50, 50);
			ok=false;
		}
	super.setVisible(b);
	}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == LoginDialog.this)
				LoginDialog_WindowClosing(event);
		}
	}

	void LoginDialog_WindowClosing(java.awt.event.WindowEvent event)
	{
		dispose();
	}
	//{{DECLARE_CONTROLS
	//}}


public void actionPerformed(ActionEvent e)
	{
	if ( e.getSource() == okbutton )
		{
		System.out.println( "OK button" );
		username = ufield.getText();
		password = new String( pfield.getPassword() );
		ok=true;
		dispose();
		}

	if ( e.getSource() == cancelbutton )
		{
		System.out.println( "Cancel button" );
		username = null;
		password = null;
		dispose();
		}

	}
	
	
public String getUsername()
	{
	return username;
	}
public String getPassword()
	{
	return password;
	}

public boolean isOK()
	{
	return ok;
	}


public boolean login( URLConnection connection )
	{
	/*
	String host = connection.getURL().getHost();
	if ( !BasicAuthenticationHelper.authenticated( host ) )
		{
		show();
		if ( !isOK() )
			return false;
		BasicAuthenticationHelper.login( host, getUsername(), getPassword() );
		}

	BasicAuthenticationHelper.login( connection );
	*/
	return true;
	}
	
}
