#----------------------------------------------------------------------------- # Name: SharedStopLight.py # Purpose: Example shared application. # # Author: Susanne Lefvert # # Thanks to Christoph Willing for providing a nicer solution for the Mac. # # Created: $Date: 2005/04/12 20:45:58 $ # Copyright: (c) 2002 # Licence: See COPYING.TXT #----------------------------------------------------------------------------- # Standard imports import os from optparse import Option import sys if sys.platform=="darwin": import pyGlobus.ioc # wxPython imports from wxPython.wx import * # AGTk imports from AccessGrid.Toolkit import WXGUIApplication from AccessGrid.SharedAppClient import SharedAppClient from AccessGrid.Platform.Config import UserConfig from AccessGrid.ClientProfile import ClientProfile from AccessGrid import icons class StopLightPanel(wxPanel): def __init__(self, parent, name, appUrl): wxPanel.__init__(self, parent, -1) # Do UI layout self.parent = parent self.__Layout() # Create UI events EVT_BUTTON(self, self.redButton.GetId(), self.OnRedButton) EVT_BUTTON(self, self.greenButton.GetId(), self.OnGreenButton) EVT_WINDOW_DESTROY(self, self.OnExit) # Create shared application client self.sharedAppClient = SharedAppClient(name) self.log = self.sharedAppClient.InitLogging() self.log.debug("StopLightPanel.__init__: Start stop light, appUrl: %s"%(appUrl)) # Get client profile clientProfileFile = os.path.join(UserConfig.instance().GetConfigDir(), "profile") clientProfile = ClientProfile(clientProfileFile) # Join the application session. self.sharedAppClient.Join(appUrl, clientProfile) self.id = self.sharedAppClient.GetPublicId() # Register callbacks for external events self.sharedAppClient.RegisterEventCallback("ChangeColor", self.ChangeColorCallback) # Get current state self.color = None self.color = self.sharedAppClient.GetData("color") if self.color: self.SetBackgroundColour(self.color) # # Callbacks for local UI events. # def OnRedButton(self, event): ''' Invoked when user hits the red button. ''' self.SetBackgroundColour("red") self.Refresh() self.sharedAppClient.SendEvent("ChangeColor", (self.id, "red")) self.sharedAppClient.SetData("color", "red") def OnGreenButton(self, event): ''' Invoked when user hits the green button. ''' self.SetBackgroundColour("green") self.Refresh() self.sharedAppClient.SendEvent("ChangeColor", (self.id, "green")) self.sharedAppClient.SetData("color", "green") def OnExit(self, event): ''' Shut down shared stoplight. ''' self.sharedAppClient.Shutdown() # # Callbacks for external AG events # def ChangeColorCallback(self, event): ''' Invoked when a changeColor event is received. ''' id, color = event.data # Ignore my own events if self.id != id: wxCallAfter(self.SetBackgroundColour, color) wxCallAfter(self.Refresh) # # Layout user interface # def __Layout(self): ''' Layout of ui components. ''' self.parent.SetTitle("Shared Stoplight") self.redButton = wxButton(self, wxNewId(), "RED", size = wxSize(100, 30)) #self.yellowButton = wxButton(self, wxNewId(), "YELLOW", size = wxSize(100, 30)) self.greenButton = wxButton(self, wxNewId(), "GREEN", size = wxSize(100, 30)) #self.titleCtrl = wxTextCtrl(self, wxNewId(), "", style = wxTE_PROCESS_ENTER) sizer = wxBoxSizer(wxVERTICAL) btnSizer = wxBoxSizer(wxHORIZONTAL) btnSizer.Add(self.redButton, 0, wxEXPAND|wxALL, 10) #btnSizer.Add(self.yellowButton, 0, wxALL, 10) btnSizer.Add(self.greenButton, 0, wxEXPAND|wxALL, 10) sizer.Add(btnSizer, 0, wxEXPAND) #sizer.Add(self.titleCtrl, 0, wxEXPAND | wxALL, 20) self.SetSizer(sizer) self.SetAutoLayout(True) #---------------------------------------------------------------------- def Usage(): """ How to use the program. """ print "%s:" % sys.argv[0] print " -a|--applicationURL : " print " -h|--help : print usage" print " -d|--debug : print debugging output" print " -t|--test : run in test mode" if __name__ == '__main__': # Create the wx python application wxapp = wxPySimpleApp() wxBeginBusyCursor() # Inizialize AG application app = WXGUIApplication() urlOption = Option("-a", "--applicationURL", dest="appUrl", default=None, help="Specify an application url on the command line") testOption = Option("-t", "--testMode", dest="test", action="store_true", default=None, help="Automatically create application session in venue") app.AddCmdLineOption(urlOption) app.AddCmdLineOption(testOption) name = "SharedStopLight" app.Initialize(name) appUrl = app.GetOption("appUrl") test = app.GetOption("test") if test: from AccessGrid.Venue import VenueIW # Create an application session in the venue # for testing. This should normally be done via # the Venue Client menu. venue = VenueIW("https://localhost:8000/Venues/default") appDesc = venue.CreateApplication("Shared Stop Light", "A demo application", "application/x-ag-shared-stoplight") appUrl = appDesc.uri if not appUrl: Usage() sys.exit(0) # Create the UI mainFrame = wxFrame(None, -1, name, size = wxSize(360, 150)) mainFrame.SetIcon(icons.getAGIconIcon()) viewer = StopLightPanel(mainFrame, name, appUrl) # Start the UI main loop mainFrame.Show() wxEndBusyCursor() wxapp.MainLoop() if test: venue.DestroyApplication(appDesc.id)