/* $Id: SettingsPromptDialog.java,v 1.1 2004/01/09 16:54:18 burkhard Exp $
 * Created on 21.05.2003 by sell
 *
 */
package de.skyrix.zsp.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import de.skyrix.zsp.ServerInfo;

/**
 * 
 * @author sell
 * @version 
 */
public class SettingsPromptDialog extends JDialog {
  private JTextField urlField = null;
  private JTextField nameField = null;
  private JTextField pwField = null;
  private boolean confirmed = false;

  public SettingsPromptDialog() {
    super(new JDialog(), "Server settings...", true);

    JPanel dataPanel = new JPanel(new GridLayout(0, 2, 5, 5));
    dataPanel.add(new JLabel("Server url:"));
    dataPanel.add(urlField = new JTextField(10));

    dataPanel.add(new JLabel("Username:"));
    dataPanel.add(nameField = new JTextField(10));

    dataPanel.add(new JLabel("Password:"));
    dataPanel.add(pwField = new JPasswordField(10));

    JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
    JButton btn = new JButton("Start");
    btn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        confirmed = true;
        setVisible(false);
      }
    });
    buttonPanel.add(btn);

    btn = new JButton("Cancel");
    btn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        confirmed = false;
        setVisible(false);
      }
    });
    buttonPanel.add(btn);

    JPanel btnPanel = new JPanel(new FlowLayout());
    btnPanel.add(buttonPanel);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(dataPanel, BorderLayout.CENTER);
    getContentPane().add(btnPanel, BorderLayout.SOUTH);
  }

  public boolean isConfirmed() {
    return confirmed;
  }
  
  public String getServerURL() {
    return urlField.getText();
  }
  
  public String getUserName() {
    return nameField.getText();
  }
  
  public String getPassword() {
    return pwField.getText();
  }

  public static ServerInfo showSettingsDialog() {
    SettingsPromptDialog spd = new SettingsPromptDialog();
    spd.pack();
    spd.setResizable(false);
    spd.show();

    if (spd.isConfirmed())
    return new ServerInfo(spd.getServerURL(), spd.getUserName(), spd.getPassword());
    
    return null;
  }

  public static void main(String[] args) {
    System.out.println(SettingsPromptDialog.showSettingsDialog());
    System.exit(0);
  }
}
