
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*; 
   


class login extends Properties {

    private String file;
    private String URL;

    public login(String file)
    {
	super();
	this.file = file;
	load();
    }

    public void load() 
    {
	try {
	    FileInputStream fin = new FileInputStream(file);
	    load(fin);
	    fin.close();
	}
        catch (Exception exception) {
	    put("URL_",      "jdbc:oracle:thin:@ccdev9i.in2p3.fr:1521:dev9i");
	    put("user_",     "antares");
	    put("password_", "towbtyur");
	    put("URL",       "jdbc:oracle:thin:@antdb01.in2p3.fr:1521:antares");
	    put("user",      "antares");
	    put("password",  "oracle8i");
        }
    }

    public void store()
    {
	try {
	    FileOutputStream fout = new FileOutputStream(file);
	    store(fout, "Database login parameters");
	    fout.close();
	}
        catch (Exception exception) {
	}
    }

    public String getURL()
    {
	return getProperty("URL");
    }

    public void getUserInput()
    {
        // startup Dialog

        final JTextField     url    = new JTextField(getURL(), 30);
        final JTextField     usr    = new JTextField(getProperty("user"), 20);
        final JPasswordField pwd    = new JPasswordField(20);

        GridLayout singleColumn = new GridLayout(0, 1);

        JPanel labelPanel = new JPanel();

        labelPanel.setLayout(singleColumn);
        labelPanel.add(new JLabel("URL: "));
        labelPanel.add(new JLabel("user: "));
        labelPanel.add(new JLabel("password: "));

        JPanel inputPanel = new JPanel();

        inputPanel.setLayout(singleColumn);
        inputPanel.add(url);
        inputPanel.add(usr);
        inputPanel.add(pwd);

        JPanel dialogPanel = new JPanel();

        dialogPanel.setLayout(new BorderLayout());
        dialogPanel.add(labelPanel, BorderLayout.WEST);
        dialogPanel.add(inputPanel, BorderLayout.EAST);

        JOptionPane optionPane = new JOptionPane(dialogPanel,
                                                 JOptionPane.PLAIN_MESSAGE,
                                                 JOptionPane.OK_CANCEL_OPTION);

        JDialog dialog = optionPane.createDialog(null, "");

        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.show();

        Object answer = optionPane.getValue();

        if (answer == null ||
            answer == JOptionPane.UNINITIALIZED_VALUE ||
            !((Integer) answer).equals(new Integer(JOptionPane.OK_OPTION)))
            System.exit(0);

        put("URL",      url.getText());
        put("user",     usr.getText());
        put("password", String.valueOf(pwd.getPassword()));
    }

}

