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

View of /ModelEditor/RecoElempanel.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (download) (as text) (annotate)
Fri Jan 7 14:40:33 2005 UTC (15 years, 1 month ago) by efrank
Branch: MAIN
CVS Tags: post-st-migration2, post-st-migration
Changes since 1.8: +79 -59 lines
migration to new schematools generated classes.
new chemdb.
factor chem out of seed data factory
imporved configuration of modeleditor

from ModelEditor.DoubleTextEntryDialog import DoubleTextEntryDialog
import copy
from wxPython.wx         import *
from KahApp.KahConfigMgr import KahConfigMgr
from FaST.BssFactory     import BssFactory
from KahOM.RecoElem      import RecoElem
import os
from tempfile            import mktemp 
from tempfile            import TemporaryFile

from MeSharedEditServer  import putOnClipboard
from MeSharedEditServer  import getCbDescriptor
from KahDataServices.ModelFromText  import modelFromText

#------------------------------------------------------------------------
class TreeData:
#------------------------------------------------------------------------
    """
    TreeData instances are associated with wxTree nodes via
    SetPyData(). TreeData is just a struct to keep us sane
    when fetching things back. It holds a RecoElem and the
    CatalogEntry that owns it.
    """

    def __init__(self, itemId, aRecoElem, aCatalogEntry):
        self.re       = aRecoElem
        self.ce       = aCatalogEntry
        self.itemId   = itemId
        return

        


#------------------------------------------------------------------------
class RecoElempanel:
#------------------------------------------------------------------------
#    def __init__(self, parent):
#    def initEvents(self):
#    def ID_NUM(self,event):
#    def AddRecoElemTree( self, treeItemId, aRecoElem, aCatalogEntry ):
#    def onSelectReaction(self,event):
#    def onSelect(self, treeEvent):
#    def redrawRecoElemRxList(self, re):
#    def onRightListClick( self, event):
#    def setupRecoElemRxListEditMenu(self):
#    def rightTreeClick( self, event):
#    def setupRecoElemEditMenu(self):
#    def updateTreeBranchText(self, sourceRXNum):
#    def recoElemPasteRx(self, event):
#    def recoElemPasteFromSelectedRxGrid(self, event):
#    def recoElemAddKid(self, event):
#    def recoElemAddRoot(self, event):
#    def recoElemRxListCut(self, event):
#    def recoElemRxListCopy(self, event):
#    def recoElemRxListPaste(self, event):

    #------------------------------------------------------------------
    def __init__(self, parent):
    #------------------------------------------------------------------
        self.selectedItem    = None      # last thing selected. Instance of TreeItem
        self.cutOrCopiedRx   = None
        self.cutOrCopiedRE   = None
        self._pluginDict      = {}        # used to dispatch calls to plugins.
        self.parent = parent

        self.parent.rootId = self.parent.tree_ctr.AddRoot("top item")

 
        #set up menu for editing recoelem
        self.recoElemEditMenu = wxMenu()
        self.recoElemNewKidId = wxNewId()
        self.recoElemCopyREBranchId = wxNewId()
        self.recoElemCutREBranchId = wxNewId()
        self.recoElemPasteREBranchId = wxNewId()
        self.recoElemMergeREBranchId = wxNewId()

        self.recoElemPasteRxId = wxNewId()
        self.recoElemPasteFromSelectedRxGridId = wxNewId()
        self.recoElemAppendFromTextFileId = wxNewId()
        self.recoElemVizWithCytoScapeId = wxNewId()
        self.recoElemWriteToBssId = wxNewId()

        self.recoElemWriteToCbId= wxNewId()
        self.setupRecoElemEditMenu()

        #set up menu for list
        self.recoElemRxListEditMenu = wxMenu()
        self.recoElemRxListCutId = wxNewId()
        self.recoElemRxListCopyId = wxNewId()
        self.recoElemRxListPasteId = wxNewId()
        self.setupRecoElemRxListEditMenu()

        self.initEvents()
        return

    #------------------------------------------------------------------
    def initEvents(self):
    #------------------------------------------------------------------

        #button1= self.parent.OK_button
        # select in list of reactions
        EVT_LISTBOX(self.parent, self.parent.list_box.GetId(),
                    self.onSelectReaction)

        # hit the button that loads a RecoElem id
        #EVT_BUTTON(self.parent, button1.GetId(), self.ID_NUM)

        # select in tree
        EVT_TREE_SEL_CHANGED(self.parent.tree_ctr, 
                             self.parent.tree_ctr.GetId(), self.onSelect)

        # Right click on tree (presumably after selection)
        EVT_RIGHT_DOWN(self.parent.tree_ctr, self.rightTreeClick)

        # Right click on List (presumably after selection)
        EVT_RIGHT_DOWN(self.parent.list_box, self.onRightListClick)

        # this does not work for some reason.
        #EVT_LISTBOX(self.parent.list_box, self.parent.list_box.GetId(),
        #  self.onRightListClick)
        
        
        return


    #------------------------------------------------------------------
    def addRecoElemTree( self, treeItemId, aRecoElem, aCatalogEntry ):
    #------------------------------------------------------------------
        """
        @type treeItemId:  wx tree item id
        @type aRecoElem:   KahOM.RecoElem
        @type aCatalogEntry: CatalogServices.CatalogEntry

        Attach aRecoElem at node treeItemId, recording the catalogEntry.
        Also, recursively attach all childern AND force all children to
        have the same catalogEntry.
        """
        
        
        numrx= aRecoElem.nReactions()
        if numrx > 0:
            rxitem = "(" + str(numrx) + "rx"+ ") " + aRecoElem.getName()
        else:
            rxitem = aRecoElem.getName()

        itemId=self.parent.tree_ctr.AppendItem(treeItemId, rxitem )
        self.parent.tree_ctr.SetPyData(itemId,
                                       TreeData( itemId, aRecoElem, aCatalogEntry))
        
        for kid in aRecoElem.getRawSubElems():
            self.addRecoElemTree( itemId, kid, aCatalogEntry )
            
        return

    #------------------------------------------------------------------
    def onSelectReaction(self,event):
    #------------------------------------------------------------------
        return

        pt=self.parent.list_box.GetPosition()
        item =self.parent.list_box.HitTest(pt)

        selectedlist=self.parent.list_box.GetSelections()

        for count in selectedlist:
            rx = self.parent.list_boxData[count]

        return

    #------------------------------------------------------------------
    def onSelect(self, treeEvent):
    #------------------------------------------------------------------
        item = treeEvent.GetItem()
        re = self.parent.tree_ctr.GetPyData(item).re
        self.selectedItem=item
      
        self.redrawRecoElemRxList( re )
        return

    #------------------------------------------------------------------
    def redrawRecoElemRxList(self, re):
    #------------------------------------------------------------------
        """
        @type re: KahOM.RecoElem
        @rtype: none
        """
        wxBeginBusyCursor()
        self.parent.list_box.Clear()
        offset = 0
        self.parent.list_boxData.clear()
        for rx in re.getRawReactions():
            item = rx.asString()
            listnum = self.parent.list_box.Append(item)
            itemNo =  self.parent.list_box.GetCount()-1
            # we store the recoElem ptr too in case we have to rm the rx
            self.parent.list_boxData[itemNo] = (rx, offset, re)
            offset += 1
           
        wxEndBusyCursor()

        return

    #------------------------------------------------------------------
    def onRightListClick( self, event):
    #------------------------------------------------------------------
                
        listBoxObject = event.GetEventObject()
        listBoxObject.PopupMenu(self.recoElemRxListEditMenu, event.GetPosition())
        
        return
    
    #------------------------------------------------------------------
    def setupRecoElemRxListEditMenu(self):
    #------------------------------------------------------------------
        self.recoElemRxListEditMenu.Append( self.recoElemRxListCutId, 
                                          "Cut", "Cut" )
        EVT_MENU(self.parent, self.recoElemRxListCutId, self.recoElemRxListCut)
        

        self.recoElemRxListEditMenu.Append( self.recoElemRxListCopyId,
                                          "Copy","Copy")
        EVT_MENU(self.parent, self.recoElemRxListCopyId,self.recoElemRxListCopy)
        self.recoElemRxListEditMenu.Append( self.recoElemRxListPasteId,
                                          "Paste", "Paste")
        EVT_MENU(self.parent, self.recoElemRxListPasteId,
                 self.recoElemRxListPaste)

        self.recoElemRxListEditMenu.Append( self.recoElemPasteFromSelectedRxGridId,
                                            "Paste From Selected Rx Table", 
                                            "Paste From Selected Rx Table")
        EVT_MENU(self.parent, self.recoElemPasteFromSelectedRxGridId,
                 self.recoElemPasteFromSelectedRxGrid)


        return

    #------------------------------------------------------------------
    def rightTreeClick( self, event):
    #------------------------------------------------------------------
        #pt=event.GetPosition()
        #if self.selectedItem != None:
        if 1:
            treeObject = event.GetEventObject()
            treeObject.PopupMenu(self.recoElemEditMenu, event.GetPosition() )

        return
    
    #------------------------------------------------------------------
    def setupRecoElemEditMenu(self):
    #------------------------------------------------------------------
        self.recoElemEditMenu.Append( self.recoElemNewKidId,
                                      "New Sub Element", "New kid arg3" )
        EVT_MENU(self.parent, self.recoElemNewKidId, self.recoElemNewKid)
        #
        self.recoElemEditMenu.Append( self.recoElemCopyREBranchId,
                                      "Copy Tree Branch", "Copy Tree Branch" )
        EVT_MENU(self.parent, self.recoElemCopyREBranchId, self.recoElemCopyREBranch)
        #
        self.recoElemEditMenu.Append( self.recoElemCutREBranchId,
                                      "Cut Tree Branch", "Cut Tree Branch" )
        EVT_MENU(self.parent, self.recoElemCutREBranchId, self.recoElemCutREBranch)
        #
        self.recoElemEditMenu.Append( self.recoElemPasteREBranchId,
                                      "Paste Tree Branch", "Paste Tree Branch" )
        EVT_MENU(self.parent, self.recoElemPasteREBranchId, self.recoElemPasteREBranch)

        self.recoElemEditMenu.Append( self.recoElemMergeREBranchId,
                                      "Merge into Tree Branch",
                                      "Merge into Tree Branch" )
        EVT_MENU(self.parent, self.recoElemMergeREBranchId, self.recoElemMergeREBranch)
        #
        # Rx related
        #

        #
        self.recoElemEditMenu.AppendSeparator()
        #

        self.recoElemEditMenu.Append( self.recoElemPasteRxId,
                                      "Paste Rx", "Add kid arg3" )
        EVT_MENU(self.parent, self.recoElemPasteRxId, self.recoElemPasteRx)
        #
        self.recoElemEditMenu.Append( self.recoElemPasteFromSelectedRxGridId,
                                      "Paste From Selected Rx Table", 
                                      "Paste From Selected Rx Table")
        EVT_MENU(self.parent, self.recoElemPasteFromSelectedRxGridId,
                 self.recoElemPasteFromSelectedRxGrid)
        #
        self.recoElemEditMenu.AppendSeparator()
        #

        self.recoElemEditMenu.Append( self.recoElemAppendFromTextFileId,
                                      "Append from text file",
                                      "Append from text file")
        EVT_MENU(self.parent, self.recoElemAppendFromTextFileId,
                 self.recoElemAppendFromTextFile)

        self.recoElemEditMenu.Append( self.recoElemWriteToBssId,
                                      "Write new version",
                                      "Write new version")
        EVT_MENU(self.parent, self.recoElemWriteToBssId,
                 self.recoElemWriteToBss)
        

        #
        # Shared editing
        
        self.recoElemEditMenu.AppendSeparator()
        self.recoElemEditMenu.Append( self.recoElemWriteToCbId,
                                      "Copy to shared clipboard",
                                      "Copy to shared clipboard")
        EVT_MENU(self.parent, self.recoElemWriteToCbId, self.recoElemWriteToCb)

        

        #
        # plug-ins will be added beyond here via extendRecoElemEditMenu.
        #

        self.recoElemEditMenu.AppendSeparator()

        self.recoElemEditMenu.Append( self.recoElemVizWithCytoScapeId,
                                      "Visualize in CytoScape",
                                      "Visualize in CytoScape")
        EVT_MENU(self.parent, self.recoElemVizWithCytoScapeId,
                 self.recoElemVizWithCytoScape)

        return

    #------------------------------------------------------------------
    def extendRecoElemEditMenu(self, menuText, helpText,  theModule):
    #------------------------------------------------------------------
        """
        Used for plugins.  Adds them to the RecoElemEditMenu

        @type menuText: string   The text to be shown in the menu.
        @type helpText: string   The help text for the item.
        @type theModule:  Instance of KahModule
        """

        # is there already a module with this name?  Name must be unique.
        #

        mgr       = self.parent.getConfigMgr()

        try:
            cfg = mgr.getSection( theModule.name() )
            self.parent.error("extendRecoelemEditMenu: Already have a module with name %s. Abort loading new plugin." % theModule.name())
            return
        except KeyError:
            pass
            
        # Make new menu item
        #

        theNewId = wxNewId()
        self._pluginDict[theNewId] = theModule

        # Configure the new module.  Don't reread config script...just
        # allows its values to be used.

        try:
            cfg = mgr.newSection( theModule.name() )
        except ValueError:
            # should never get here....
            self.parent.error( "extendRecoelemEditMenu: program error.")
            return

        theModule.config( cfg )
        mgr.redoOverrides()

        #
        self.recoElemEditMenu.Append( theNewId, menuText, helpText )
        EVT_MENU(self.parent, theNewId, self.pluginHandler)

        return
    
    #------------------------------------------------------------------
    def pluginHandler(self, event):
    #------------------------------------------------------------------
        """
        Dispatches a wxCommandEvent that arose from an EVT_MENU
        registration to the actual plugin.
        """

        if (self.selectedItem == None):
            self.parent.error( "Select a RecoElem first" )
            return
        
        # form the arg list to the plugin

        treeData = self.parent.tree_ctr.GetPyData( self.selectedItem)
        re = treeData.re

        ##
        # find and call the plugin
        #
        # this is a little wonky right now.  we'r exposing the execution
        # rules and have the config file hardwired.  its a start.  don't
        # copy this code.
        ##

        theModule = self._pluginDict[ event.GetId() ]
        mgr       = self.parent.getConfigMgr()
        cfg       = mgr.getSection( theModule.name() )
        theModule.process( re, cfg )
        theModule.finish( cfg )

        # the plugin may have changed the recoelem, so we have to
        # bring the display up to date.  this is really inefficient,
        # but we're going to destroy everything for the recoelem
        # and rebuilt from scratch to make sure we get it right.
        
        id=treeData.itemId
        self.parent.tree_ctr.DeleteChildren( id )
        self.parent.tree_ctr.Delete( id )
        parentId = self.parent.tree_ctr.GetItemParent( id )

        self.addRecoElemTree( parentId, re, treeData.ce )

        return

    #------------------------------------------------------------------
    def updateTreeBranchText(self, sourceRXNum, recoElemName):
    #------------------------------------------------------------------

        rxPrevName = self.parent.tree_ctr.GetItemText(self.selectedItem)
        newLabel = "(" + str(sourceRXNum) + "rx" ") " + recoElemName

        #sylistic purposes get rid of (0 rx) if it exist
        if newLabel[1] == '0':
            newLabel = newLabel[6:]
        

        self.parent.tree_ctr.SetItemText(self.selectedItem, newLabel)
        return
    
    #------------------------------------------------------------------
    def recoElemNewKid(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None): return
        treeData = self.parent.tree_ctr.GetPyData( self.selectedItem)
        re = treeData.re
        
        prompt = "Name of new child for " + re.getName()
        dlg = wxTextEntryDialog(self.parent, message=prompt, style=wxOK)

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

        newKid = RecoElem( kidName )
        re.appendSubElems( newKid )
        self.addRecoElemTree( self.selectedItem, newKid, treeData.ce)
        
        return
    
    #------------------------------------------------------------------
    def recoElemCopyREBranch(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None): return
        self.cutOrCopiedRE   = self.selectedItem
        return

    #------------------------------------------------------------------
    def recoElemCutREBranch(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None): return
        
        # first, modify the actual recoElem tree.
        parentItem   = self.parent.tree_ctr.GetItemParent(self.selectedItem)
        parentRe     = self.parent.tree_ctr.GetPyData( parentItem ).re
        selectedRe   = self.parent.tree_ctr.GetPyData(self.selectedItem ).re

        # if we have a parent, i.e., if we are not the top of the tree,
        # then remove ourself from our parents kid-list
        
        if (parentRe != None):
            parentRe.getRawSubElems().remove( selectedRe )
            parentRe.setDirty()

        # this no longer points anyplace valid
        self.cutOrCopiedRE = None
        self.selectedItem == None
        
        # now modify the display
        self.parent.tree_ctr.Delete(self.selectedItem)
        return

    #------------------------------------------------------------------
    def recoElemPasteREBranch(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None):
            self.parent.error( "Select a tree branch first as target.")
            return

        if (self.cutOrCopiedRE == None):
            self.parent.error( "Select and copy a tree branch first.")
            return

        reSource = self.parent.tree_ctr.GetPyData( self.cutOrCopiedRE ).re
        reTarget = self.parent.tree_ctr.GetPyData( self.selectedItem).re
        ceTarget = self.parent.tree_ctr.GetPyData( self.selectedItem).ce

        reSourceCopy = copy.deepcopy( reSource )
        reTarget.appendSubElems( reSourceCopy )

        self.addRecoElemTree( self.selectedItem, reSourceCopy, ceTarget )

        return

    #------------------------------------------------------------------
    def recoElemMergeREBranch(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None):
            self.parent.error( "Select a tree branch first as target.")
            return

        if (self.cutOrCopiedRE == None):
            self.parent.error( "Select and copy a tree branch first.")
            return

        reSource = self.parent.tree_ctr.GetPyData( self.cutOrCopiedRE ).re
        reTarget = self.parent.tree_ctr.GetPyData( self.selectedItem).re
        ceTarget = self.parent.tree_ctr.GetPyData( self.selectedItem).ce

        for k in reSource.getRawSubElems():
            kCopy = copy.deepcopy( k )
            reTarget.appendSubElems( kCopy )
            self.addRecoElemTree( self.selectedItem, kCopy, ceTarget )


        # unlike pasting the whole branch in, we must drain any
        # reactions out of the cut/copied branch

        for rx in reSource.getRawReactions():
            reTarget.appendReactions( copy.deepcopy(rx ) )
        
        rxListNum = reTarget.nReactions()
        self.updateTreeBranchText(rxListNum, reTarget.getName())
        self.redrawRecoElemRxList(reTarget)

        return

    #------------------------------------------------------------------
    def recoElemPasteRx(self, event):
    #------------------------------------------------------------------

        if ( None == self.selectedItem ): return
        if ( None == self.cutOrCopiedRx ): return
        
        
        re = self.parent.tree_ctr.GetPyData( self.selectedItem).re

        re.appendReactions( self.cutOrCopiedRx )

        rxListNum = re.nReactions()
        
        self.updateTreeBranchText(rxListNum, re.getName())
        self.redrawRecoElemRxList(re)

        return


    #------------------------------------------------------------------
    def recoElemPasteFromSelectedRxGrid(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None): return

        re = self.parent.tree_ctr.GetPyData( self.selectedItem).re

        rxList = self.parent.rx.getSelectedRx()
        rxListNum = re.nReactions()

        for rx in rxList:
            re.appendReactions(rx)

        self.updateTreeBranchText(rxListNum, re.getName())
        self.redrawRecoElemRxList(re)
                
        return

    #------------------------------------------------------------------
    def recoElemVizWithCytoScape(self, event):
    #------------------------------------------------------------------
        if (self.selectedItem == None): return
        re = self.parent.tree_ctr.GetPyData( self.selectedItem).re

        # "not connectable" is for things like ATP that happen so much
        # that you can't just draw one node or you get a mess.  But cytoscape
        # SIF files uses "same name, same node."  So we're going to bump up
        # a counter every time we hit a not-connectible and tack it on the
        # end of the name.
        #
        nNotConnect = 0
        
        #
        # KEGG provides isConnected as an attribute of a reactant.  We're
        # going to try something here and ignore the isConnected int he
        # Reactant object and make our decions based upon the number of
        # edges coming into a reactant node in the full graph of the
        # system

        rxGraph = re.getGraph()
        thresh = 5

	allRxList = []
	re.gatherReactions( allRxList )

	output          = mktemp(suffix=".sif")
	nodeAttribs     = mktemp(suffix=".node")
        out_file        = open(output, "w")      # Create temporary
        nodeAttrib_file = open(nodeAttribs, "w")      # Create temporary
	print output, nodeAttribs

        nodeAttrib_file.write("NodeType\n")

	nRx = 0
	for r in allRxList:
	    nRx += 1
            rxKind="enzymatic"
            if (r.nEnzymeNames() > 0):
                enzName= r.getEnzymeNames(0)
                if (enzName == "spontaneous"): rxKind = "spontaneous"
                enzName.replace(' ', '_')
            else:
                enzName= ""
                rxKind="spontaneous"
            rxName = "Rx%d-%s" % ( nRx, enzName)
            nodeAttrib_file.write("%s = %s\n" % ( rxName, rxKind) )
            if (r.getDirection() == "forward"):
                #edgeName = "ci"           # compound to irreversible rx
                edgeName = "cr"           # turn offf for now
            else:
                edgeName = "cr"           # compound to reversible rx
	    for i in r.getRawInputs():
                if (not i.getIsConnected() or (rxGraph.all_degree( i.getName()) > thresh) ):
                    theName=i.getName().replace(' ','_')+ ":%d" % (nNotConnect)
                    nNotConnect += 1
                else:
                    theName=i.getName().replace(' ','_')
		out_file.write( "%s %s %s\n" % (theName, edgeName, rxName) )
                nodeAttrib_file.write("%s = reactant\n" % (theName) )

            for o in r.getRawOutputs():
                if (not o.getIsConnected() or (rxGraph.all_degree( o.getName())>thresh) ):
                    theName=o.getName().replace(' ','_')+ ":%d" % (nNotConnect)
                    nNotConnect += 1
                else:
                    theName=o.getName().replace(' ','_')
		out_file.write( "%s rc %s\n" % (rxName, theName))
                nodeAttrib_file.write("%s = reactant\n" % (theName) )

	out_file.close()
	cytoInstallDir = os.getenv( 'CYTOSCAPE_HOME' )
	cytoCmd        = os.getenv( 'CYTOSCAPE_COMMAND' )

        #cmd = "(cd $CYTOSCAPE_HOME; java -jar cytoscape.jar -i %s -n %s&)" % (output, nodeAttribs)
        cmd = "(cd %s; %s -i %s -n %s&)" % (cytoInstallDir, cytoCmd, output, nodeAttribs)
	print cytoInstallDir
	print cytoCmd
	print cmd

	os.system( cmd )

	#os.unlink( output )

	return

    #------------------------------------------------------------------
    def recoElemAppendFromTextFile(self, event):
    #------------------------------------------------------------------
        """
        Read contents of a text file, convert to recoElem and append
        as child to the selected item.
        """

        if (self.selectedItem == None):
            self.parent.error( "Select an item first.")
            return
        
        prompt = "Choose a file"
        dlg = wxFileDialog(self.parent, message=prompt, style=wxOPEN)
        if ( dlg.ShowModal() == wxID_OK):
            path = dlg.GetPath()
            print "using path ", path
            dlg.Destroy()
        else:
            #didn't get a kid name
            dlg.Destroy()
            return
        
        reTemp = RecoElem("temp")
        try:
            modelFromText(path, reTemp, verbose=1)
        except Exception, e:
            print e

        print "snarfed:"
        reTemp.dump()

        treeData = self.parent.tree_ctr.GetPyData( self.selectedItem)
        re = treeData.re
        for k in reTemp.getRawSubElems():
            # we don't deepcopy here because  we've taken ownership
            re.appendSubElems( k )
            self.addRecoElemTree( self.selectedItem, k, treeData.ce)

        for rx in reTemp.getReactions():
            re.appendReactions( rx )
        
        rxListNum = re.nReactions()
        self.updateTreeBranchText(rxListNum, re.getName())
        self.redrawRecoElemRxList(re)

        return
    
    #------------------------------------------------------------------
    def recoElemWriteToBss(self, event):
    #------------------------------------------------------------------
        # there are so many problems here it's not funny.  We _will_
        # corrupt the DB with this but have to start somewhere.  Example:
        # How do we know we've not been passed a REcoElem that is
        # owned by a higher entity?  The system needs to be extended to
        # understand the ownership model and catalog arch.

        if (self.selectedItem == None): return

        re = self.parent.tree_ctr.GetPyData( self.selectedItem).re
        catalogEntry = self.parent.tree_ctr.GetPyData( self.selectedItem).ce

        if (None == catalogEntry):
            # clipboard item.  can't write that.
            self.parent.error( "No catalog entry. Copy this item into a loaded model before writing")
            return

        wxBeginBusyCursor()
        foundryItemKey = catalogEntry.write( re )

        if (foundryItemKey == None ):
            self.parent.error( "Write failed.")
        else:
            self.parent.error( "Write succeeded" )

        wxEndBusyCursor()
        return

    #------------------------------------------------------------------
    def recoElemWriteToCb(self, event):
    #------------------------------------------------------------------
        """ Copy the current recoElem to the clipboard """

        if not self.parent.cat.cbStarted:
            self.parent.error( "Shared editing not started.")
            return

        if (self.selectedItem == None): 
            self.parent.error( "Make a selection first.")
            return

        re0 = self.parent.tree_ctr.GetPyData( self.selectedItem).re
        re  = copy.deepcopy(re0)        #$$.deepCopy()

        print "copying to clipboard ", re0.getName(), re.getName()
        intId = putOnClipboard( re )
        print "deep copied to clipboard:"


        # tell everyone about it

        descriptor = getCbDescriptor()
        print "descriptor is ", descriptor

        self.parent.meAGMgr.cbChanged(descriptor)

        return
       
    #------------------------------------------------------------------
    def addCbItem(self, re):
    #------------------------------------------------------------------
        """
        Add the passed clipboard item to the Navigator.

        @type re: FaST.RecoElem
        @rtype: None

        Note- clipboard items haven't a catalogEntry associated with
        them.  They did not come from a factory or, better, they are
        disowned?
        """

        # add it to the tree.  Set CatalogEntry to None...a magic value
        # sniffed out elsewhere.  Ick.
        
        self.addRecoElemTree( self.parent.rootId, re, None)
        
        return

    #------------------------------------------------------------------
    def recoElemRxListCut(self, event):
    #------------------------------------------------------------------
    # this has gotta be wrong...what were we thinking?  It's ok for a
    # single selection.
    
        re = None
        for count in self.parent.list_box.GetSelections():
            (rx,offset, re) = self.parent.list_boxData[count]
            del( re.getRawReactions()[offset:offset+1] )
            re.setDirty()
            self.redrawRecoElemRxList(re)
            self.cutOrCopiedRx   = rx

        if (re == None):
            return
        else:
            rxListNum = re.nReactions()
            self.updateTreeBranchText(rxListNum, re.getName())
        
        return

    #------------------------------------------------------------------
    def recoElemRxListCopy(self, event):
    #------------------------------------------------------------------
        for count in self.parent.list_box.GetSelections():
            (rx,offset,re) = self.parent.list_boxData[count]
            self.cutOrCopiedRx   = rx

        return    

    #------------------------------------------------------------------
    def recoElemRxListPaste(self, event):
    #------------------------------------------------------------------
        # we're pasting into a list_box.  This list box is being
        # shown *because* a recoElem has been selected.  that means
        # _it_ is the recoElem to modify.

        #since the listbox is only allowing single select.
        #we won't count how big the selection is

        if (self.selectedItem == None): return
        re = self.parent.tree_ctr.GetPyData( self.selectedItem).re
        re.appendReactions( self.cutOrCopiedRx )

        rxListNum = re.nReactions()
        self.updateTreeBranchText(rxListNum, re.getName())
        self.redrawRecoElemRxList(re)

        return    

    #------------------------------------------------------------------
    def startAGClipboard(self):
    #------------------------------------------------------------------
        raise NotImplementedError


        if (self.cbStarted): return


        (host, portnum) = ("porta-video.mcs.anl.gov", 303030)

        self.cbStarted  = 1
        self.clipboard  = {}
        self.nCb        = 0     #used to make unique id for items on cb

        return

    #------------------------------------------------------------------
    def stopAGClipboard(self):
    #------------------------------------------------------------------
        raise NotImplementedError


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3