如何在JInternalFrame中加载数据库表?

时间:2013-04-02 03:57:12

标签: griffon

我在JInternal框架内加载表时遇到问题,我无法描述它,但我会告诉你一个图像让你理解我的意思。

pic

这是我的代码:

- SparepartController.groovy -

package sparepart

import griffon.transform.Threading

class SparepartController {
    def model
    def view
    def builder

   @Threading(Threading.Policy.INSIDE_UITHREAD_ASYNC)

    def supplier = {
       String id = 'supplier'
       def (m, v, c) = createMVCGroup('supplier', id, title: "Supplier")
       builder.desktopPane(view.desktop) {
           widget(v.window)
       }
    }

    def logout = {

    }

    def quit = {
        app.shutdown()
    }
}

- SparepartView.groovy -

package sparepart

actions {
    action(id: 'loginAction',
            name: 'Login')
    action(id: 'logoutAction',
            enabled: bind {model.auth},
            name: 'Logout',
            closure: controller.logout)
    action(id: 'quitAction',
            name: 'Quit',
            closure: controller.quit)
    action(id: 'jenisBarangAction',
            name: 'Jenis Barang',
    )
    action(id: 'barangAction',
            name: 'Barang',
    )
    action(id: 'supplierAction',
            name: 'Supplier',
            closure: controller.supplier
    )
    action(id: 'pembelianAction',
            name: 'Pembelian',
    )
    action(id: 'penjualanAction',
            name: 'Penjualan',
    )
    action(id: 'userAction',
            name: 'User',
    )
    action(id: 'reportBarangAction',
            name: 'Barang',
    )
    action(id: 'reportSupplierAction',
            name: 'Supplier',
    )
    action(id: 'reportPembelianAction',
            name: 'Pembelian',
    )
    action(id: 'reportPejualanAction',
            name: 'Penjualan',
    )
    action(id: 'reportBarangTerlarisAction',
            name: 'Barang Terlaris',
    )
}

application(title: 'DIKA MOTOR',
  preferredSize: [950, 650],
  pack: true,
  //location: [50,50],
  locationByPlatform: true,
  iconImage:   imageIcon('/griffon-icon-48x48.png').image,
  iconImages: [imageIcon('/griffon-icon-48x48.png').image,
               imageIcon('/griffon-icon-32x32.png').image,
               imageIcon('/griffon-icon-16x16.png').image]) {
    // add content here
    menuBar {
        menu('Menu') {
            menuItem loginAction
            menuItem logoutAction
            menuItem quitAction
        }

        //if(model.auth) {
            menu('Master Data') {
                menuItem jenisBarangAction
                menuItem barangAction
                menuItem supplierAction
            }
            menu('Transaksi') {
                menuItem pembelianAction
                menuItem penjualanAction
            }
            menu('Utility') {
                menuItem userAction
            }
            menu('Report') {
                menuItem reportPembelianAction
                menuItem reportPejualanAction
                menuItem reportSupplierAction
                menuItem reportBarangTerlarisAction
            }
        //}
    }
    desktopPane(id: 'desktop')
}

- SupplierController.groovy -

package sparepart

import groovy.sql.DataSet
import groovy.sql.Sql

class SupplierController {
    def model
    def view
    private String id

    void mvcGroupInit(Map args) { 
        id = args.mvcName

        edt {
            model.listSupplier.clear()
        }

        withSql { dataSourceName, Sql sql ->
            sql.eachRow("SELECT * FROM tblsupplier") {
                Map baris = [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email]
                edt {
                    model.listSupplier << baris
                }
            }
        }
    }

    void mvcGroupDestroy() {
        execOutsideUI() {
            def desktop = view.window.parent
            desktop.remove view.window
            desktop.invalidate()
            desktop.repaint()
        }
    }

    def keluar = { destroyMVCGroup id }

    def simpan = {
        withSql { dataSourceName, Sql sql ->
            DataSet supplier = sql.dataSet("tblsupplier")
            if (sql.firstRow("SELECT kodeSupplier FROM tblsupplier WHERE kodeSupplier = ${model.kodeSupplier}") != null) {
                // sudah ada NIM yang sama, maka lakukan update.
                supplier.executeUpdate("UPDATE tblsupplier SET namaSupplier = ${model.namaSupplier}, alamat = ${model.alamat}, telp = ${model.telp}, email = ${model.email} WHERE kodeSupplier = ${model.kodeSupplier}")

                // update baris di model
                edt {
                    int index = model.listSupplier.findIndexOf{it['kodeSupplier']==model.kodeSupplier}
                    model.listSupplier[index] += [namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email]
                }

            } else {
                // belum ada NIM, maka tambahkan.
                supplier.add(kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email)

                // tambahkan baris baru ke model
                edt { model.listSupplier << [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email] }
            }
        }
    }

    def hapus = {
        withSql { dataSourceName, Sql sql ->
            sql.executeUpdate("DELETE FROM tblsupplier WHERE kodeSupplier = ${model.kodeSupplier}")
        }

        // menghapus baris dari model
        edt {
            int index = model.listSupplier.findIndexOf{it['kodeSupplier']==model.kodeSupplier}
            model.listSupplier.remove(index)
        }
    }

    def refresh = { app ->
        withSql { dataSourceName, Sql sql ->
            sql.eachRow("SELECT * FROM tblsupplier") {
                Map baris = [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email]
                edt {
                    model.listSupplier << baris
                }
            }
        }
    }
}

- SupplierModel.groovy -

package sparepart

import ca.odell.glazedlists.BasicEventList
import ca.odell.glazedlists.EventList
import ca.odell.glazedlists.swing.EventSelectionModel
import groovy.beans.Bindable

class SupplierModel {
    @Bindable String kodeSupplier
    @Bindable String namaSupplier
    @Bindable String alamat
    @Bindable String telp
    @Bindable String email

    EventList listSupplier = new BasicEventList()
    EventSelectionModel eventSelectionModel

- SupplierView.groovy -

package sparepart

import ca.odell.glazedlists.FilterList
import ca.odell.glazedlists.impl.filter.StringTextFilterator
import ca.odell.glazedlists.swing.EventSelectionModel
import ca.odell.glazedlists.swing.TextComponentMatcherEditor
import net.miginfocom.swing.MigLayout

// Helper untuk mengambil nilai kolom dari baris terpilih
def getSelectedValue = { kolom ->
    EventSelectionModel selection = model.eventSelectionModel
    if (!selection.isSelectionEmpty()) {
        selection.getSelected()[0][kolom]
    } else {
        null
    }
}

internalFrame(title: title, size: [650, 500], id: 'window', visible: true, resizable: true, iconifiable: true, maximizable: true) {
    borderLayout()

    panel(constraints: PAGE_START) {
        flowLayout(alignment: FlowLayout.RIGHT)
        label("Cari: ")
        textField(id: "txtPencarian", columns: 10)
    }

    scrollPane(constraints: CENTER) {
        table (rowSelectionAllowed: true, id: 'table') {
            eventTableModel(source: new FilterList(model.listSupplier,
                    new TextComponentMatcherEditor(txtPencarian, new StringTextFilterator())),
                    format: defaultTableFormat(columnNames: ['Kode Supplier', 'Nama Supplier', 'Alamat', 'Telp', 'Email']))
            model.eventSelectionModel = installEventSelectionModel(source: model.listSupplier)
        }
    }

    panel(layout: new MigLayout('', '[right][left,grow]',''), constraints: PAGE_END) {
        label('Kode Supplier:')
        textField(id: 'txtKodeSupplier', columns: 5, text: bind(target: model, targetProperty: 'kodeSupplier'), constraints: 'wrap')
        label('Nama Supplier:')
        textField(id: 'txtNamaSupplier', columns: 20,text: bind(target: model, targetProperty: 'namaSupplier'), constraints: 'wrap')
        label('Alamat:')
        textField(id: 'txtAlamat', columns: 100, text: bind(target: model, targetProperty: 'alamat'), constraints: 'growx, wrap')
        label('Telp.:')
        textField(id: 'txtTelp', columns: 20,text: bind(target: model, targetProperty: 'telp'), constraints: 'wrap')
        label('Email:')
        textField(id: 'txtEmail', columns: 30,text: bind(target: model, targetProperty: 'email'), constraints: 'wrap')

        bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged',
                sourceValue: {getSelectedValue("kodeSupplier")}, target: txtKodeSupplier, targetProperty: 'text')
        bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged',
                sourceValue: {getSelectedValue("namaSupplier")}, target: txtNamaSupplier, targetProperty: 'text')
        bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged',
                sourceValue: {getSelectedValue("alamat")}, target: txtAlamat, targetProperty: 'text')
        bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged',
                sourceValue: {getSelectedValue("telp")}, target: txtTelp, targetProperty: 'text')
        bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged',
                sourceValue: {getSelectedValue("email")}, target: txtEmail, targetProperty: 'text')

        panel(constraints: 'span, growx, wrap') {
            flowLayout()
            button("Simpan", actionPerformed: controller.simpan)
            button("Hapus", enabled: bind (source: model.eventSelectionModel,
                    sourceEvent: 'valueChanged', sourceValue: {!model.eventSelectionModel.isSelectionEmpty()}),
                    actionPerformed: controller.hapus)
            //button("Refresh", actionPerformed: controller.refresh)
            button keluarAction
        }
    }
}

它会给我这个:

this

2 个答案:

答案 0 :(得分:0)

使用JTable / JTree / JList的简单方法是使用GlazedLists(http://www.glazedlists.com/),幸运的是有一个glazedlists插件http://artifacts.griffon-framework.org/plugin/glazedlists

答案 1 :(得分:0)

在您的SupplierController.groovy中,有以下内容:

sql.eachRow("SELECT * FROM tblsupplier") {
   // Look here!
   Map baris = [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, 
      alamat: model.alamat, telp: model.telp, email: model.email]
   ...
}

您永远不会使用从SQL查询返回的值。根据您的代码,model.kodeSupplier可能为null。你的意思是it.kodeSupplier

顺便说一下,对于这种应用程序,我使用这个plugin来生成基于域类(from here)的MVCGroups。使用域类比使用地图更容易。