from optparse import Option import sys if sys.platform=="darwin": import pyGlobus.ioc from AccessGrid.SharedAppClient import SharedAppClient from AccessGrid.Platform.Config import UserConfig from AccessGrid.ClientProfile import ClientProfile from AccessGrid.Toolkit import WXGUIApplication from AccessGrid import icons from wxPython.wx import * class GroupChat(wxPanel): ''' wxPython UI panel for sending and receiving text. ''' MESSAGE = "message" def __init__(self, parent, id, appUrl): ''' Initiate the panel. ''' wxPanel.__init__(self, parent, id) self.Populate() self.sharedAppClient = SharedAppClient("Group Chat") self.log = self.sharedAppClient.InitLogging() self.log.debug("GroupChat.__init__: Started Group Chat") # Get client profile try: clientProfileFile = os.path.join(UserConfig.instance().GetConfigDir(), "profile") self.clientProfile = ClientProfile(clientProfileFile) except: self.log.info("SharedQuestionTool.__init__: Could not load client profile, set clientProfile = None") self.clientProfile = None # Join the application session self.sharedAppClient.Join(appUrl, self.clientProfile) self.publicId = self.sharedAppClient.GetPublicId() # Register event callback self.sharedAppClient.RegisterEventCallback(self.MESSAGE, self.ReceiveMessage) def Populate(self): ''' Create all UI components. ''' sizer = wxBoxSizer(wxVERTICAL) self.textOutput = wxTextCtrl(self, -1, style = wxTE_MULTILINE) sizer2 = wxBoxSizer(wxHORIZONTAL) self.textInput = wxTextCtrl(self, -1) sendButton = wxButton(self, wxNewId(), "Send") sizer2.Add(self.textInput, 1, wxRIGHT, 5) sizer2.Add(sendButton, 0) sizer.Add(self.textOutput, 1, wxEXPAND | wxALL, 5) sizer.Add(sizer2, 0, wxEXPAND | wxALL, 5) self.SetSizer(sizer) self.SetAutoLayout(1) self.Layout() EVT_BUTTON(self, sendButton.GetId(), self.OnSend) EVT_WINDOW_DESTROY(self, self.OnExit) def OnSend(self, event): ''' Event handler for the send button. ''' text = self.textInput.GetValue() self.sharedAppClient.SendEvent(self.MESSAGE, text) self.textInput.Clear() def OnExit(self, event): ''' Overridden panel method to shut down client properly. ''' print 'on exit' self.sharedAppClient.Shutdown() def ReceiveMessage(self, message): ''' Event handler for message events ''' text = message.data wxCallAfter(self.textOutput.WriteText, text+"\n") if __name__ == "__main__": # Check arguments if len(sys.argv) < 2: print "Please provide the application URL on the command line --appUrl=" sys.exit() # Create the wxPython application wxapp = wxPySimpleApp() wxInitAllImageHandlers() # Create the AG application app = WXGUIApplication() name = "Group Chat" # Add command line options appurlOption = Option("-a", "--appUrl", dest="appUrl", default=None, help="Specify an application url on the command line") app.AddCmdLineOption(appurlOption) # Initialize the AG application app.Initialize(name) # Create the main frame frame = wxFrame(None, -1, name) frame.SetIcon(icons.getAGIconIcon()) # Create the group chat panel appUrl = app.GetOption("appUrl") chat = GroupChat(frame, -1, appUrl) frame.Show() # Start the UI main thread wxapp.MainLoop()