From fe448ef049e439ea6674dbcd23321dc27eca3bf7 Mon Sep 17 00:00:00 2001
From: Luke Leighton <lkcl@lkcl.net>
Date: Sat, 11 Sep 2010 13:25:54 +0100
Subject: [PATCH 06/34] use pyjamas.Builder to create loginpanel

---
 gnumed/gnumed/client/ProxiedWeb/gui/GNUmedWeb.py  |   12 +--
 gnumed/gnumed/client/ProxiedWeb/gui/LoginPanel.py |   85 +++++++--------------
 2 files changed, 33 insertions(+), 64 deletions(-)

diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/GNUmedWeb.py b/gnumed/gnumed/client/ProxiedWeb/gui/GNUmedWeb.py
index d3fef7f..073b2b8 100644
--- a/gnumed/gnumed/client/ProxiedWeb/gui/GNUmedWeb.py
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/GNUmedWeb.py
@@ -16,7 +16,7 @@ class gmTopLevelLayer:
     def onModuleLoad(self):
         self.TEXT_ERROR = "Server Error"
         self.remote_py = EchoServicePython()
-        self.loginpanel = cLoginPanel(self, Spacing=8)
+        self.loginpanel = cLoginPanel(self) # fires logged out on UI load
         self.afterloginpanel = cMainPanel(self, Spacing=8, Width="100%")
 
         self.maindisplay = DockPanel(HorizontalAlignment=HasAlignment.ALIGN_CENTER,
@@ -24,19 +24,17 @@ class gmTopLevelLayer:
 
         RootPanel().add(self.maindisplay)
 
-        self.logged_out() # start at logged out
-
     def logged_out(self):
         self.login_username = None
-        self.loginpanel.status.setText("") # reset status display
+        self.loginpanel.setStatus("") # reset status display
         if self.maindisplay.center is not None:
             self.maindisplay.remove(self.afterloginpanel)
-        self.maindisplay.add(self.loginpanel, DockPanel.CENTER)
+        self.maindisplay.add(self.loginpanel.getPanel(), DockPanel.CENTER)
 
     def logged_in(self, username):
         self.login_username = username
         if self.maindisplay.center is not None:
-            self.maindisplay.remove(self.loginpanel)
+            self.maindisplay.remove(self.loginpanel.getPanel())
         self.maindisplay.add(self.afterloginpanel, DockPanel.CENTER)
 
 #======================================================
@@ -55,7 +53,7 @@ if __name__ == '__main__':
     # for pyjd, set up a web server and load the HTML from there:
     # this convinces the browser engine that the AJAX will be loaded
     # from the same URI base as the URL, it's all a bit messy...
-    pyjd.setup("http://127.0.0.1:8080/ProxiedWeb/gui/public/GNUmedWeb.html")
+    pyjd.setup("http://127.0.0.1:8080/ProxiedWeb/gui/public/GNUmedWebPyJD.html")
     app = gmTopLevelLayer()
     app.onModuleLoad()
     pyjd.run()
diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/LoginPanel.py b/gnumed/gnumed/client/ProxiedWeb/gui/LoginPanel.py
index b8aa5c8..b8e3378 100644
--- a/gnumed/gnumed/client/ProxiedWeb/gui/LoginPanel.py
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/LoginPanel.py
@@ -1,78 +1,49 @@
-from pyjamas.ui.TextBox import TextBox
-from pyjamas.ui.PasswordTextBox import PasswordTextBox
-from pyjamas.ui.Label import Label
-from pyjamas.ui.Button import Button
-from pyjamas.ui.Image import Image
-from pyjamas.ui.HTML import HTML
-from pyjamas.ui.VerticalPanel import VerticalPanel
-from pyjamas.ui.HorizontalPanel import HorizontalPanel
-from pyjamas.ui.CaptionPanel import CaptionPanel
-from pyjamas.ui import HasAlignment
+from pyjamas.builder.Builder import Builder, HTTPUILoader
 
 #======================================================
-class cLoginPanel(VerticalPanel): 
-    def __init__(self, app, **kwargs):
+class cLoginPanel: 
+    def __init__(self, app):
         self.app = app
-        VerticalPanel.__init__(self, **kwargs)
-
-        self.status=Label()
+        self.lp = None
         self.TEXT_WAITING = "Waiting for response..."
-        
-        self.lblusername = Label()
-        self.lblusername.setText("username")
-        self.username = TextBox(Text="any-doc")
-        self.lblpassword = Label()
-        self.lblpassword.setText("password")
-        self.password = PasswordTextBox(Text="")
-        self.button_login = Button("Login", self)
-        self.gnumedlogo = Image("images/gnumedlogo.png")
-
-        usernamepanel = HorizontalPanel(VerticalAlignment=HasAlignment.ALIGN_MIDDLE)
-        usernamepanel.add(self.lblusername)
-        usernamepanel.add(self.username)
-
-        passwdpanel = HorizontalPanel(VerticalAlignment=HasAlignment.ALIGN_MIDDLE)
-        passwdpanel.add(self.lblpassword)
-        passwdpanel.add(self.password)
+        HTTPUILoader(self).load("gnumedweb.xml") # calls onUILoaded when done
 
-        credentialpanel = VerticalPanel(HorizontalAlignment=HasAlignment.ALIGN_CENTER)
-        credentialpanel.add(usernamepanel)
-        credentialpanel.add(passwdpanel)
+    def onUILoaded(self, text):
+        self.b = Builder(text)
+        self.lp = self.b.createInstance("LoginPanel", self)
+        self.app.logged_out() # start at logged out
 
-        captionpanel = CaptionPanel("GNUmed Default 0.7.7", credentialpanel)
+    def setStatus(self, txt):
+        if self.lp is not None:
+            self.lp.status.setText(txt)
 
-        panel = VerticalPanel()
-        panel.add(self.gnumedlogo)
-        panel.add(captionpanel)
-        panel.add(self.button_login)
-        panel.add(self.status)
-        self.add(panel)
+    def getPanel(self):
+        return self.lp
 
     #--------------------------------------------------
-    def onClick(self, sender):
-        self.status.setText(self.TEXT_WAITING)
+    def onLoginClicked(self, sender):
+        self.setStatus(self.TEXT_WAITING)
 
-        if sender == self.button_login:
-            self.button_login.setEnabled(False) # disable whilst checking
-            self.try_user = self.username.getText()
-            passwd = self.password.getText() 
-            self.password.setText("") # reset to blank
-            self.app.remote_py.login(self.try_user, passwd, self)
+        self.lp.button_login.setEnabled(False) # disable whilst checking
+        self.try_user = self.lp.CaptionPanel1.Grid1.username.getText()
+        passwd = self.lp.CaptionPanel1.Grid1.password.getText() 
+        self.lp.CaptionPanel1.Grid1.password.setText("") # reset to blank
+        self.app.remote_py.login(self.try_user, passwd, self)
 
     #--------------------------------------------------
     def onRemoteResponse(self, response, request_info):
         method = request_info.method
 
         if method == 'login':
-            self.button_login.setEnabled(True) # re-enable after response
+            self.lp.button_login.setEnabled(True) # re-enable after response
             if response:
                 # XXX response should really contain the username
-                self.status.setText("Login successful: %s" %str(response))
+                self.setStatus("Login successful: %s" %str(response))
                 user = self.try_user
                 self.app.logged_in(user)
-                self.status.setText("Connected as: %s" % user)
+                self.setStatus("Connected as: %s" % user)
             else:
-                self.status.setText("Login failed: %s" %str(response))
+                self.setStatus("Login failed: %s" %str(response))
 
     #--------------------------------------------------
     def onRemoteError(self, code, errobj, request_info):
@@ -85,13 +56,13 @@ class cLoginPanel(VerticalPanel):
         #     }
         message = errobj['message']
         if code != 0:
-            self.status.setText("HTTP error %d: %s" % 
+            self.setStatus("HTTP error %d: %s" % 
                                 (code, message))
         else:
             code = errobj['code']
             if message['message'] == 'Cannot request login parameters.':
-                self.status.setText("You need to log in first")
+                self.setStatus("You need to log in first")
             else:
-                self.status.setText("JSONRPC Error %s: %s" %
+                self.setStatus("JSONRPC Error %s: %s" %
                                 (code, message))
 
-- 
1.7.1

