/*
 * @(#)LoginDialog.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2001 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 *
 * Jörg Haeger, 14.05.2001
 */

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

public class LoginDialog extends Dialog
{
	public LoginDialog()
	{
		super("LDTitle");

		JPanel panel = new JPanel()
		{
			public Insets getInsets() {
				return new Insets(10, 10, 10, 10);
			}
		};
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		userPanel = createInputGroup();
		panel.add(userPanel);

		getContentPane().add(panel, BorderLayout.CENTER);
		getContentPane().add(createControlPanel(),
			BorderLayout.SOUTH);
		pack();
	}

	protected boolean apply()
	{
		String userID = userIDField.getText();
		String password = passwordField.getText();
		if (userID.length() == 0)
		{
			JOptionPane.showMessageDialog(
				CDcreator.mainWin,
				i18n("LDNoUserName"),
				i18n("error"),
				JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else
		if (password.length() == 0)
		{
			JOptionPane.showMessageDialog(
				CDcreator.mainWin,
				i18n("LDNoPassword"),
				i18n("error"),
				JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else
		{
			try {
				new Login(userID, password).exec();
				return true;
			} catch (CommandError e)
			{
				JOptionPane.showMessageDialog(
					CDcreator.mainWin,
					i18n("LDFailed"),
					i18n("error"),
					JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
	}

	protected JPanel createControlPanel()
	{
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout());

		JButton cancel = new JButton(i18n("cancel"));
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cancelPressed();
			}
		});
		p.add(cancel);

		JButton newAccount = new JButton(i18n("LDNewAccount"));
		newAccount.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				newAccountPressed();
			}
		});
		p.add(newAccount);

		OKButton = new JButton(i18n("OK"));
		OKButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OKPressed();
			}
		});
		p.add(OKButton);

		return p;
	}

	protected JPanel createInputGroup()
	{
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setBorder(
			BorderFactory.createEmptyBorder(3, 40, 5, 5));
		p.setAlignmentX(LEFT_ALIGNMENT);

		userIDField = new JTextField(State.userID);
		userIDField.setColumns(15);
		tab = 120;
		p.add(createLabelItemBox(
			new JLabel(i18n("LDUserName")),
			userIDField));
		p.add(Box.createVerticalStrut(10));

		passwordField = new JPasswordField("");
		passwordField.setColumns(15);
		p.add(createLabelItemBox(
			new JLabel(i18n("LDPassword")),
			passwordField));

		return p;
	}

	protected void newAccountPressed()
	{
		NewAccountDialog d = new NewAccountDialog();
		CDcreator.mainWin.center(d);
		d.show();
	}

	JPanel userPanel;
	JTextField userIDField, passwordField;
}
