# 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.8.2.1 2002/02/26 19:56:56 akuchlin Exp $"

from java import awt

class LoginDialog(awt.Dialog):
    # 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)

        p = awt.Panel() ; p.setLayout( awt.BorderLayout() )
        p.add("North", awt.Label("Microscope Server") )
        p.add("Center", awt.Label("User ID") )
        p.add("South", awt.Label("Password") )
        self.add("West", p)

        p = awt.Panel() ; p.setLayout( awt.BorderLayout() )
        self.device_list = awt.List()
        for id, desc in self.MICROSCOPE_LIST:
            self.device_list.add( desc )
            if device is not None and id == device:
                self.device_list.select( self.device_list.getItemCount()-1 )
            
        self.user_field = awt.TextField(20)
        if user_id is not None: self.user_field.setText( user_id )

        self.passwd_field = awt.TextField(20)
        self.passwd_field.setEchoCharacter('*')
        if password is not None: self.passwd_field.setText( password )
        
        p.add("North", self.device_list )
        p.add("Center", self.user_field )
        p.add("South", self.passwd_field )

        self.add("East", p)

        self.add("South", awt.Button("Connect") )

        self.device = self.user_id = self.password = None

    def handleEvent (self, e):
        if e.id == awt.Event.WINDOW_DESTROY:
            self.dispose()
            self.device = self.user_id = self.password = None
            return 1
        elif (e.id == awt.Event.ACTION_EVENT and
              isinstance(e.target, awt.Button) ):
            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()
            return 1
