# LoginDialog.py : Login dialog box, used when running as an application
#
# Distributed under the terms described in the LICENCE file.
#
# Developed by akuchlin@mems-exchange.org
#
# Presents the user with a login dialog box, listing the available microscope
# devices, and text fields for the user's ID and password.

__revision__ = "$Id: LoginDialog.py,v 1.11 2002/01/15 20:02:29 akuchlin Exp $"

from java import awt
from javax import swing

import gui_util

class LoginWindowListener(awt.event.WindowAdapter):
    def __init__ (self):
	self.dialog = dialog

    def windowClosing (self, e):
        import sys
        sys.exit(0)


class LoginDialog(awt.Dialog, awt.event.ActionListener):
    # XXX should get this list through XML-RPC!
    MICROSCOPE_LIST = [('cnf', 'Cornell'),
                       ('mx', 'MEMS Exchange'),
                       ('snf', 'Stanford'),
                       ('ucb', 'Berkeley'),
                       ('umich', 'U. of Michigan'),
                        ]
    
    def __init__ (self, parent, device, user_id, password):
        # Initialize as a modal dialog
        awt.Dialog.__init__(self, parent, "Microscope Login", 1)
        self.addWindowListener(LoginWindowListener())

        p = swing.JPanel() ; p.setLayout(awt.GridBagLayout())
        self.add(p)
        gui_util.constrain(p, swing.JLabel("Microscope Server"), 0,0, 1,1)
        gui_util.constrain(p, swing.JLabel("User ID"), 0,1, 1,1)
        gui_util.constrain(p, swing.JLabel("Password"), 0,2, 1,1)

        L = map(lambda tuple: tuple[1], self.MICROSCOPE_LIST)
        self.device_list = swing.JList(L)
        self.user_field = swing.JTextField(20)
        if user_id is not None: self.user_field.setText(user_id)

        self.passwd_field = swing.JPasswordField(20)
        if password is not None: self.passwd_field.setText(password)
        
        gui_util.constrain(p, self.device_list, 1,0, 1,1)
        gui_util.constrain(p, self.user_field, 1,1, 1,1)
        gui_util.constrain(p, self.passwd_field, 1,2, 1,1)

        button = swing.JButton("Connect")
        gui_util.constrain(p, button, 0,3, 2,1)
        button.addActionListener(self)

        self.device = self.user_id = self.password = None
        
    def actionPerformed (self, e):
        if (e.getActionCommand() == "Connect"):
            index = self.device_list.getSelectedIndex()
            self.device, desc = self.MICROSCOPE_LIST[index]
            self.user_id = self.user_field.getText()
            self.password = self.passwd_field.getText()

            self.dispose()
