Download NetBeans!

20090612 Friday June 12, 2009

How to Incorporate a Wizard into a Griffon Application (Part 3)

I've solved several problems I had with integrating a db into a Griffon wizard, with this result:

In other words, though I can successfully populate a drop-down list with customer names and put related city and state information into the output, I am unable to bind that output to the related text fields. I wonder how the @Bindable annotation works in the context of wizards. Or maybe it should be solved in a different way.

Here's conf/DataSource.groovy:

dataSource {
    url = "jdbc:derby://localhost:1527/sample"
    driverClassname = "org.apache.derby.jdbc.ClientDriver"
    username = "app"
    password = "app"
}

Here's conf/Events.groovy:

onBootstrapEnd = { app ->
   def dataSource = new ConfigSlurper().parse(DataSource).dataSource
   MyDbService dbs = MyDbService.instance
   dbs.initDataSource(dataSource)
}

Here's services/MyDbService.groovy:

import groovy.sql.Sql

@Singleton
class MyDbService {
    
    private sql

    def initDataSource( dataSource ) {
        sql = Sql.newInstance(
            dataSource.url,
            dataSource.username,
            dataSource.password,
            dataSource.driverClassname
        )
    }

    def getAllNames() {
        sql.rows("select * from APP.CUSTOMER").NAME
    }

    def getCityForSelectedName(String selectedName) {
        sql.firstRow("select CITY from APP.CUSTOMER where NAME = ${selectedName}").CITY
    }

    def getStateForSelectedName(String selectedName) {
        sql.firstRow("select STATE from APP.CUSTOMER where NAME = ${selectedName}").STATE
    }
  
}

And, finally, here's wizards/OneWizardPage.groovy, where the change in 'selCity' should be reflected in the City text field. Both parts are highlighted below.

import net.miginfocom.swing.MigLayout
import ca.odell.glazedlists.*
import javax.swing.event.ListSelectionListener
import java.util.Map
import ca.odell.glazedlists.swing.EventSelectionModel
import ca.odell.glazedlists.swing.EventListModel

class OneWizardPage {

    def stepId = "step1" // must be unique per WizardPage
    def description = "Customer Details"
    def autoListen = true

    def selCity = '<empty>'
    def selState = '<empty>'

    def pageContents = {

        EventList namesEventList = new BasicEventList()
        namesEventList.addAll( MyDbService.instance.getAllNames() )

        EventListModel listModel = new EventListModel(namesEventList)
  
        EventSelectionModel selectionModel = new EventSelectionModel(namesEventList)
        selectionModel.addListSelectionListener( [ valueChanged: {e ->
                    if (e.valueIsAdjusting){
                        def customer = selectionModel.selected
                        selCity = MyDbService.instance.getCityForSelectedName(customer)
                        selState = MyDbService.instance.getStateForSelectedName(customer)
                        println "${customer} from ${selCity} (${selState})"
                    }
                } ] as ListSelectionListener)

        panel(border:lineBorder(1, color: java.awt.Color.black), layout: new MigLayout('fill')) {

            label( text: "Name:" )
            scrollPane(constraints: "growx, wrap"){
                list( name: "tf1", model: listModel, selectionModel: selectionModel, visibleRowCount: 1 )
            }
            label( text: "Address:" )
            cityField = textField( name: "tf2",
                text: selCity,
                columns: 20, constraints: "wrap" )
            label( text: "City:" )
            textField( name: "tf3", text: "", columns: 20 )

        }

    }

    // Either return a String that indicates a problem
    // or return a null value indicating no problem
    def onValidate = { component, /*PropertyChangeEvent*/ event ->
        return null // no problems
    }

}

Can anyone help?

Jun 12 2009, 11:58:41 PM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/how_to_incorporate_a_wizard2
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed