"""mems.instrument.client.tk.login_dialog

Presents the user with a login form and lets them select which microscope
instrument to use.
"""

# created 2002/01/23, AMK

__revision__ = "$Id: login_dialog.py,v 1.3 2002/02/22 22:05:48 akuchlin Exp $"

import Tkinter, Pmw
from tkSimpleDialog import Dialog

class LoginDialog (Dialog):

    devices = [('ucb', 'Berkeley'),
               ('mx', 'MEMS Exchange'),
               ('cnf', 'Cornell'),
               ('snf', 'Stanford'),
               ('umich', 'U. of Michigan')]


    def __init__ (self, master, client,
                 device = None, userid=None, password=None, message=None):
        self.client = client
        self.device = device
        self.userid = userid
        self.password = password
        self.message = message or ""
        
        Dialog.__init__(self, master)


    def body (self, master):
        self.title("Microscope Login")

        Tkinter.Label(master, text="User ID:").grid(row=0, sticky=Tkinter.W)
        Tkinter.Label(master, text="Password:").grid(row=1, sticky=Tkinter.W)
        Tkinter.Label(master, text="Device:").grid(row=2, sticky=Tkinter.W)

        self.userid_field = Tkinter.Entry(master, width=10)
        if self.userid is not None:
            self.userid_field.insert('end', self.userid)
            
        self.password_field = Tkinter.Entry(master, width=10, show='*')
        if self.password is not None:
            self.password_field.insert('end', self.password)


        device_names = []
        selected = None
        for dev_id, dev_name in self.devices:
            device_names.append(dev_name)
            if self.device == dev_id:
                selected = dev_name
                
        self.device_field = Pmw.OptionMenu(master,
                                           items = device_names,
                                           initialitem = selected
                                          )
        Tkinter.Label(master, text=self.message).grid(row=3, column=1, sticky=Tkinter.W)
            
        self.userid_field.grid(row=0, column=1, sticky=Tkinter.W)
        self.password_field.grid(row=1, column=1, sticky=Tkinter.W)
        self.device_field.grid(row=2, column=1, sticky=Tkinter.W)



    def apply (self):
        self.client.userid = self.userid_field.get()
        self.client.password = self.password_field.get()

        index = self.device_field.index(self.device_field.getcurselection())
        dev_id, dev_name = self.devices[index]
        self.client.device = dev_id

        self.client.get_authtoken((self.master,))

        
