Edit Molecule Button

    
    /*
    * Edit molecule button
    * 
    *
    * Usage:
    * 1. Edit the name of the Structure field
    * 2. Run button script
    *
    * @author David Pech <dpech@chemaxon.com>
    * @author Vita Stejskal <vstejskal@chemaxon.com>
    */
    
    import com.im.df.api.dml.*
    import com.im.df.api.*
    import com.im.df.api.support.SelectionDescription
    import com.im.ijc.core.api.util.IJCCoreUtils
    import com.im.ijc.sketchreg.*
    import chemaxon.struc.Molecule
    import chemaxon.formats.MolExporter
    import chemaxon.formats.MolImporter
    import com.im.df.api.chem.MarvinStructure
    
    init = { widget ->
    }
    destroy = { widget ->
    }
    evaluate = { widget ->
        def ety = dataTree.rootVertex.entity // assumes you have reference to the data tree
        def edp = ety.schema.dataProvider.getEntityDataProvider(ety)
        def molFld = ety.fields.items.find { it.name == 'Structure' } // find the structure field
        def rs = ety.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL) // find the ResultSet
        def rootVS = rs.getVertexState(dataTree.rootVertex) // obtain the VertexState
    
        List ids = rootVS.getSelectedRowsIds() // get the selected IDs
        if (ids.size == 1) {
            Map rows = rootVS.getData(ids, DFEnvironmentRO.DEV_NULL) // get the data
            Map row = rows[ids[0]] // get the first and only row
            MarvinStructure mol = row[molFld.id] // Get the Structure. Its a com.im.df.api.chem.MarvinStructure instance
            Molecule cxnMol = mol.getNative() // obtain the chemaxon.struc.Molecule instance
    
            // convert chemaxon.struc.Molecule instance into canonical SMILES
            molSmiles = cxnMol.toFormat("smiles:u")
    
            // using SketchRegistry uses the default Sketcher
            // so this will work with both Marvin or ChemDraw
            SketchRegistry registry = SketchRegistry.getDefault()
            Sketch sketcher = registry.getDefaultFactory().createSketcher()
            sketcher.setDialogTitle('My Molecule sketcher')
            // load current molecule into the Sketcher
            sketcher.setMolecule(MolImporter.importMol(molSmiles))
            sketcher.setSketchFeedback(new SketchFeedback() {
                public boolean buttonPressed(Sketch sketch, String buttonName) {
                    println 'Button pressed: ' + buttonName
                    // do something when button in Sketcher is pressed
                }
                public void moleculeChanged(Sketch sketch) {
                    //println 'Sketcher: ' + MolExporter.exportToFormat(sketch.getMolecule(), 'smiles')
    
                    // rewrite structure in the database with the changed one
                    def dataToUpdate = [(molFld.id):MolExporter.exportToFormat(sketch.getMolecule(), 'smiles')]
                    def lock = edp.lockable.withLock('Updating') { envRW ->
                        def ud = DFUpdateDescription.create(ety, ids[0], dataToUpdate)
                        def submitList = Collections.singletonList(ud)
                        edp.update(submitList, DFUndoConfig.OFF, envRW)
                        }
                    println "Molecule edited"
                }
            })
            sketcher.openSketcher()
        } else {
            println "Bad Selection"
        }
    }
    on_change = { widget, button ->
    }