[Bio] / ModelEditor / MainMenuBar.py Repository:
ViewVC logotype

View of /ModelEditor/MainMenuBar.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (download) (as text) (annotate)
Tue Jan 17 17:12:47 2006 UTC (12 years, 10 months ago) by efrank
Branch: MAIN
CVS Tags: lwc, HEAD
Changes since 1.11: +2 -2 lines
persistence for dynamics stuff
bunch of flux modelinig stuff including flux model building in modeleditor

#########################################################################
#
#
#########################################################################

from wxPython.wx import *
from ModelEditor.FontManager           import FontManager
from ModelEditor.DoubleTextEntryDialog import DoubleTextEntryDialog
from KahDataServices.KahDbDataFactory  import KahDbDataFactory
from KahChemDb.KahChemDbDataFactory    import KahChemDbDataFactory
from FaST.BssFactory                   import BssFactory
from FaST.BssFactoryProxy              import BssFactoryProxy
from KahDataServices.SeedDataFactory   import SeedDataFactory
from CatalogServices.Catalog           import Catalog

class MainMenuBar:

    #------------------------------------------------------------------------
    def __init__(self, parent):
    #------------------------------------------------------------------------
        self.parent = parent

        self.setupMenuBar()
        self.initEvents()
       
        # below used in selectFont.  Why member data here???
        
        self.fontData = None
        self.newFont  = None 
        self.curFont  = None
        self.isConnected = 0
        """ Flags we are connected to AG Kah shared app """

        self.cbStarted =   0
        """ Flags clibboard sharing has been started """

        return
    
    #------------------------------------------------------------------------
    def setupMenuBar(self):
    #------------------------------------------------------------------------
    
        ##
        # "File" dropdown
        ##
        self.exitID       = wxNewId()
        self.fontID       = wxNewId()
        self.addPlugInId  = wxNewId()

        self.optionsMenu = wxMenu()
        #self.optionsMenu.Append(self.fontID, "Change  Font",  "changing font")
        self.optionsMenu.Append(self.addPlugInId, "Add Plug-in", "Add Plug-in")
        self.optionsMenu.Append(self.exitID, "Exit    ", "quitting the program")
        self.parent.theMainMenuBar.Append(self.optionsMenu, "&File")
    

        ##
        # "Data" drop down
        ##

        self.dataDropDownMenu = wxMenu()

        self.addLocalFactoryId = wxNewId()
        self.dataDropDownMenu.Append(self.addLocalFactoryId,
                                     "Connect Directly to a KahDb",
                                     "Start local data factory")
        self.addNewLocalFactoryId = wxNewId()
        self.dataDropDownMenu.Append(self.addNewLocalFactoryId,
                                     "Connect Directly to a NEW STYLE KahDb",
                                     "Start local data factory")

        self.addLocalChemDbId = wxNewId()
        self.dataDropDownMenu.Append(self.addLocalChemDbId,
                                     "Connect Directly to KahChemDb",
                                     "Connect Directly to KahChemDb" )

        self.dataDropDownMenu.AppendSeparator()

        self.addSeedServerId = wxNewId()
        self.dataDropDownMenu.Append(self.addSeedServerId,
                                     "Connect to Seed Instance",
                                     "Connect to another seed server")

        self.addKahDBServerId = wxNewId()
        self.dataDropDownMenu.Append(self.addKahDBServerId,
                                     "Connect to KahDB Server",
                                     "Connect to another KahDB server")

        self.dataDropDownMenu.AppendSeparator()
       
        self.getPubAGId = wxNewId()
        self.dataDropDownMenu.Append(self.getPubAGId,
                                   "Find KahDb Servers via AG",
                                   "Connect to currently published dbs on AG")

        #self.advertiseAGId = wxNewId()
        #self.dataDropDownMenu.Append(self.advertiseAGId,
        #                           "Advertise KahDb Server on AG",
        #                           "Advertise a Server on Kah venue")

        self.parent.theMainMenuBar.Append(self.dataDropDownMenu, "&Data")

        ##
        # "AccessGrid" drop down
        ##
        self.AGDropDownMenu = wxMenu()

        self.connectAGId = wxNewId()
        self.AGDropDownMenu.Append(self.connectAGId, "Connect to AG", "Connect to the AccessGrid")


        self.startAGClipboardId = wxNewId()
        self.AGDropDownMenu.Append(self.startAGClipboardId,
                                   "Use AG Clipboards", "Allows collaborative editing")

        self.stopAGClipboardId = wxNewId()
        self.AGDropDownMenu.Append(self.stopAGClipboardId,
                                   "Stop Clipboards", "Stop collaborative editing")

        self.disconnectAGId = wxNewId()
        self.AGDropDownMenu.Append(self.disconnectAGId, "Disconnect from AG", "Leave the AccessGrid")
        
        self.parent.theMainMenuBar.Append(self.AGDropDownMenu, "&AccessGrid")
        
        return
    
    #------------------------------------------------------------------------
    def initEvents(self):
    #------------------------------------------------------------------------
        EVT_MENU(self.parent, self.exitID, self.exitApp)
        #EVT_MENU(self.parent, self.fontID, self.selectFont)
        EVT_MENU(self.parent, self.addLocalFactoryId, self.doAddLocalFactory)
        EVT_MENU(self.parent, self.addNewLocalFactoryId, self.doAddNewLocalFactory)
        EVT_MENU(self.parent, self.addLocalChemDbId, self.doAddLocalChemDbFactory)
        EVT_MENU(self.parent, self.addSeedServerId, self.doAddSeedServer)
        EVT_MENU(self.parent, self.addKahDBServerId, self.doAddKahDBServer)
        EVT_MENU(self.parent, self.addPlugInId, self.doAddPlugInEvent)
        EVT_MENU(self.parent, self.connectAGId, self.doAGConnect)
        EVT_MENU(self.parent, self.startAGClipboardId, self.doStartAGClipboard)
        EVT_MENU(self.parent, self.stopAGClipboardId, self.doStopAGClipboard)
        EVT_MENU(self.parent, self.disconnectAGId, self.doAGDisconnect)
        #EVT_MENU(self.parent, self.advertiseAGId, self.doAGAdvertise)
        EVT_MENU(self.parent, self.getPubAGId, self.doAGgetPub)

        return
        
    #------------------------------------------------------------------------
    def exitApp(self, event):
    #------------------------------------------------------------------------
        print "in exitApp"

        if self.isConnected:
            if self.cbStarted:
                self.parent.cat.stopAGClipboard()
                

            self.parent.meAGMgr.__del__()
            del self.parent.meAGMgr
            self.parent.meAGMgr = None

        
        self.parent.Destroy()
        
	return

    #------------------------------------------------------------------------
    def doAddPlugInEvent(self, event):
    #------------------------------------------------------------------------
        prompt = "Name of source file holding plug-in:"
        dlg = wxTextEntryDialog(self.parent, message=prompt, style=wxOK)

        if ( dlg.ShowModal() == wxID_OK):
            theName = dlg.GetValue()
            if ( theName=="" ): return
            dlg.Destroy()
        else:
            #didn't get a kid name
            dlg.Destroy()
            return

        print "trying to add ", theName

        try:
            self.doAddPlugIn( theName)
        except ImportError, e:
            self.parent.error( "Could not import %s: %s" % (theName, e) )
            return
        except LookupError:
            self.parent.error( "No register hook called 'register' found in plugin")
            return

        return


    #------------------------------------------------------------------------
    def doAddPlugIn(self, theName):
    #------------------------------------------------------------------------
        """
        Given theName, import the module and call its registerHook.  NOTE: we
        let exceptions raise up to caller since in some cases wxPy is ready
        for us to do GUI and in others its not (we load default plugins early,
        before the GUI is ready.

        Use . notation, e.g., KahModules.Printer, not KahModule/Printer
        
        ImportError- could not find module
        LookupError- could not find registerHook
        
        @type theName: string
        @rtype: None
        """

        mod = __import__( theName, globals(), locals(), "registerHook" )
        mod.registerHook( self.parent.re.extendRecoElemEditMenu)


        return

    #------------------------------------------------------------------------
    def doAddLocalChemDbFactory( self, event):
    #------------------------------------------------------------------------
        dialog = DoubleTextEntryDialog(self.parent, -1,
                                       "Connect to Kah Chem Database",
                                       prompt = ("Connect string",
                                                 "'oracle' or 'postgres'"),
                                       pos =(300,300),
                                       size=wxSize(300,90),
                                       style=wxDEFAULT_DIALOG_STYLE)
                                       
                                                      
        showDialogResult = dialog.ShowModal()

        if (showDialogResult == wxID_CANCEL):
            return
        elif (showDialogResult == wxID_OK):
            (connectString, dbType) = dialog.GetValue()
        else:
            self.parent.error("Program error in doAddChemDbFactory")

        if (connectString == "" or connectString == None or
            dbType == "" or dbType == None):
            self.parent.error("Invalid information.")
            return
        
        try:
            b = KahChemDbDataFactory( connectString, dbType )
        except Exception, e:
            self.parent.error( "MainMenuBar:doAddChemDbFactory could not create factory for %s (type= %s): %s" % (connectString, dbType, e ) )
                               
        return

    #------------------------------------------------------------------------
    def doAddNewLocalFactory( self, event):
    #------------------------------------------------------------------------
        dialog = DoubleTextEntryDialog(self.parent, -1,
                                       "Connect to NEW Database",
                                       prompt = ("Connect string",
                                                 "'oracle' or 'postgres'"),
                                       pos =(300,300),
                                       size=wxSize(300,90),
                                       style=wxDEFAULT_DIALOG_STYLE)
                                       
                                                      
        showDialogResult = dialog.ShowModal()

        if (showDialogResult == wxID_CANCEL):
            return
        elif (showDialogResult == wxID_OK):
            (connectString, dbType) = dialog.GetValue()
        else:
            self.parent.error("Program error in doAddNewLocalFactory")

        if (connectString == "" or connectString == None or
            dbType == "" or dbType == None):
            self.parent.error("Invalid information.")
            return
        
        try:
            b = KahDbDataFactory( connectString, dbType )
        except Exception, e:
            self.parent.error( "MainMenuBar:doAddNewLocalFactory could not create factory for %s (type= %s): %s" % (connectString, dbType, e ) )
                               
        return

    #------------------------------------------------------------------------
    def doAddLocalFactory( self, event):
    #------------------------------------------------------------------------
        dialog = DoubleTextEntryDialog(self.parent, -1,
                                       "Connect to Database",
                                       prompt = ("Connect string",
                                                 "'oracle' or 'postgres'"),
                                       pos =(300,300),
                                       size=wxSize(300,90),
                                       style=wxDEFAULT_DIALOG_STYLE)
                                       
                                                      
        showDialogResult = dialog.ShowModal()

        if (showDialogResult == wxID_CANCEL):
            return
        elif (showDialogResult == wxID_OK):
            (connectString, dbType) = dialog.GetValue()
        else:
            self.parent.error("Program error in doAddLocalFactory")

        if (connectString == "" or connectString == None or
            dbType == "" or dbType == None):
            self.parent.error("Invalid information.")
            return
        
        try:
            b = BssFactory( connectString, dbType )
        except:
            self.parent.error( "MainMenuBar:doAddLocalFactory could not create factory for %s (type= %s)" % (connectString, dbType ) )
                               

        #try:
        #Catalog.instance().addDataFactory(b)
        #except:
        #    self.parent.error("MainMenuBar:doAddLocalFactory could not load %s." % (b.getFoundryName()))
        #    return
        
            
        return
    

    # Begin AG Stuff
    #------------------------------------------------------------------------
    def doAGConnect(self, event):
    #------------------------------------------------------------------------
        from KahDataServices.KahCatAGCfgMgr  import KahCatAGCfgMgr
        from ModelEditor.MeAGMgr             import MeAGMgr
        
        if  self.isConnected:
            self.parent.error("Connect failed: already connected. Disconnect first.")
            return
        else:
            prompt = "URL of AG Venue"
            dlg = wxTextEntryDialog(self.parent, message=prompt, style=wxOK)
            
            dlg.SetValue(KahCatAGCfgMgr.defaultVenueURI())

            if ( dlg.ShowModal() == wxID_OK):
                url = dlg.GetValue()
                if ( url=="" ): return
                dlg.Destroy()
                wxBeginBusyCursor()
                self.parent.meAGMgr = MeAGMgr(url)
                self.isConnected = 1
                wxEndBusyCursor()
            else:
                #didn't get a kid name
                dlg.Destroy()
                return

        return

    #------------------------------------------------------------------------
    def doAGDisconnect(self, event):
    #------------------------------------------------------------------------
        if self.isConnected:
            wxBeginBusyCursor()

            if self.cbStarted: self.doStopAGClipboard(event)

            self.parent.meAGMgr.__del__()
            del self.parent.meAGMgr
            self.parent.meAGMgr = None
            self.isConnected = 0
            wxEndBusyCursor()
        else:
            self.parent.error("Disconnect failed:  No connection found.")

        return

    #------------------------------------------------------------------------
    def doStartAGClipboard(self, event):
    #------------------------------------------------------------------------
        if not self.isConnected:
            self.parent.error( "Connect to the AG before sharing clipboards.")
            return

        if self.cbStarted:
            self.parent.error( "Clipboard sharing has already been started")
            return

        prompt = "Public name for your clipboard? (e.g., MyClipboard)"
        dlg = wxTextEntryDialog(self.parent, message=prompt, style=wxOK)
        dlg.SetValue("")
        if ( dlg.ShowModal() == wxID_OK):
            cbName = dlg.GetValue()
            if ( cbName=="" ): return
            dlg.Destroy()
        else:
            #didn't get a kid name
            dlg.Destroy()
            return

        wxBeginBusyCursor()
        try:
            self.parent.cat.startAGClipboard( cbName, self.parent.meAGMgr)
            self.cbStarted = 1
        except KeyError:
            self.parent.error( "A clipboard named %s is already in use" % (cbName) )
            
        wxEndBusyCursor()

        return

    #------------------------------------------------------------------------
    def doStopAGClipboard(self, event):
    #------------------------------------------------------------------------
        # warning: we are also called by doAGDisconnect which passes its
        # event to us.  that may not make sense if we actually start
        # using he event.

        if not self.isConnected:
            return

        if not self.cbStarted:
            return


        self.parent.cat.stopAGClipboard()
        self.cbStarted = 0

        return
    
    #------------------------------------------------------------------------
    #def doAGAdvertise(self, event):
    #
    # if you turn this back on, remember to handle the unPublish() 
    #
    #------------------------------------------------------------------------
    #    if self.isConnected:
    #        advertiseAGEntryDialog = DoubleTextEntryDialog(self.parent, -1,
    #                                                  "Advertise on AG",
    #                                                  prompt = ("server:",
    #                                                           "port:   "),
    #                                                  pos =(300,300),
    #                                                  size=wxSize(300,90),
    #                                                  style=wxDEFAULT_DIALOG_STYLE)
    #        
    #        showAGAdvertise = advertiseAGEntryDialog.ShowModal()
    #
    #        if (showAGAdvertise == wxID_CANCEL):
    #            return
    #        elif (showAGAdvertise == wxID_OK):
    #            (hostName, portNum) = advertiseAGEntryDialog.GetValue()
    #        else:
    #            self.parent.error("Program error in doAGAdvertise")
    #
    #        if (hostName == "" or hostName == None or
    #            portNum == "" or portNum == None):
    #            self.parent.error("Invalid information")
    #            return
    #
    #        f = self.parent.getCurrentDataFactory().getFoundryId()
    #        
    #        self.parent.meAGMgr.advertise(f, [hostName, int(portNum)])
    #        
    #    else:
    #        self.parent.error("Advertise failed:  No connection found.")
    #        
    #    return
    
    #------------------------------------------------------------------------
    def doAGgetPub(self, event):
    #------------------------------------------------------------------------
        # place import here so that we can run on system that does not have
        # AG installed...error will only happen if they try to use ag stuff

        from ModelEditor.AGGetPubDialog        import AGGetPubDialog
        
        if ( not self.isConnected ):
            self.parent.error("getPublished failed:  No connection found.")
            return
    
        wxBeginBusyCursor()
        dlg = AGGetPubDialog(self.parent, -1,
                             "AG Server", pos=(300,300),
                             size=(500,400),
                             style=wxDEFAULT_FRAME_STYLE)
        if ( dlg.ShowModal() == wxID_OK):
            dlg.Destroy()
        else:
            #didn't get a kid name
            dlg.Destroy()
            wxEndBusyCursor()
            return

        servers  = dlg.GetValue()
        self.parent.meAGMgr.addServers(servers)
        wxEndBusyCursor()
        
        return
            
    #------------------------------------------------------------------------
    def selectFont(self, event):
    #------------------------------------------------------------------------
        
        #All fonts will be change, but need to know what is the font is for
        #at least one
        self.curFont = self.parent.GetFont()
        print "self.curFont", self.curFont
        
        self.fontData = wxFontData()
        self.fontData.EnableEffects(True)
        self.fontData.SetInitialFont(self.curFont)
        print "main point size", self.curFont.GetPointSize()

        dlg = wxFontDialog(self.parent, self.fontData)
        if dlg.ShowModal() == wxID_OK:
            
            self.fontData = dlg.GetFontData()
            self.newFont = self.fontData.GetChosenFont()
            print "still keeps?", self.newFont.GetPointSize()
            self.parent.fontManager.updateFont(self.newFont)
        dlg.Destroy()
        
        return
	    

    #------------------------------------------------------------------------
    def doAddKahDBServer(self, event):
    #------------------------------------------------------------------------
        addKahDBTextEntryDialog = DoubleTextEntryDialog(self.parent, -1,
                                                      "Add KahDB Server",
                                                      prompt = ("server",
                                                               "port          "),
                                                      pos =(300,300),
                                                      size=(250,90),
                                                      style=wxDEFAULT_DIALOG_STYLE)
                                                      
        showAddKahDBServer = addKahDBTextEntryDialog.ShowModal()

        if (showAddKahDBServer == wxID_CANCEL):
            return
        elif (showAddKahDBServer == wxID_OK):
            (theServer, thePort) = addKahDBTextEntryDialog.GetValue()
        else:
            self.parent.error("Program error in doAddKahDBServer")

        if (theServer == "" or theServer == None or
            thePort == "" or thePort == None):
            self.parent.error("Invalid information")
            return
        
        theConnectionInfo = [theServer, int(thePort)]
        b = BssFactoryProxy(theConnectionInfo)

        try:
            Catalog.instance().addDataFactory(b)
        except:
            self.parent.error("MainMenuBar:doAddKahDBServer could not load %s." % (b.getFoundryName()))
            return
        
            
        #self.parent.error("Not implemented yet")
        return

    #------------------------------------------------------------------------
    def doAddSeedServer(self, event):
    #------------------------------------------------------------------------
        wxBeginBusyCursor()

        default = self.parent.getConfigMgr().getSection( "ModelEditor" ).getString("DefaultSeedInstance")


        prompt = "Seed server, (e.g. 'http://theSeed.uchicago.edu/FIG)"
        dlg = wxTextEntryDialog(self.parent, message=prompt, style=wxOK)
        dlg.SetValue( default )

        if ( dlg.ShowModal() == wxID_OK):
            seedServer = dlg.GetValue()
            if ( seedServer=="" ): return
            dlg.Destroy()
        else:
            #didn't get a kid name
            dlg.Destroy()
            return


	# The SeedDataFactory ctor is slow and actually reads a bunch of
	# data from the server

        seedFactory   = SeedDataFactory( seedServer )

        # add it to the list in the catalog
        try:
            Catalog.instance().addDataFactory( seedFactory )
        except:
            self.parent.error("loadSeedCatalog: %s already in foundry list." % seedServer)
            wxEndBusyCursor()
            return

        wxEndBusyCursor()

        return 


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3