cloud-control-panel/example-UI-table/table.go

97 lines
2.3 KiB
Go

// based off andlabs/ui/examples/table.go
package main
import "fmt"
import "log"
import "github.com/andlabs/ui"
import _ "github.com/andlabs/ui/winmanifest"
import "github.com/davecgh/go-spew/spew"
type vmRowData struct {
hostname string
size int
IPv6 string
IPv4 string
memory int
disk int
}
type modelHandler struct {
name string
rows int
bgcolorColumn int
yellowRow int
checkStates []int
vms []vmRowData
columnTypes string
funcColumnTypes func() []ui.TableValue
scanCellValue func(*modelHandler, int, int) ui.TableValue
setCellValue func(*modelHandler, *ui.TableModel, int, int, ui.TableValue)
}
func newModelHandler(rows int) *modelHandler {
mh := new(modelHandler)
mh.rows = rows
mh.columnTypes = "standard"
mh.funcColumnTypes = standardColumnTypes
mh.scanCellValue = defaultCellValue
mh.setCellValue = defaultSetCellValue
mh.bgcolorColumn = 0
mh.checkStates = make([]int, mh.rows)
mh.vms = make([]vmRowData, mh.rows)
mh.vms[1].hostname = "jcarr"
mh.yellowRow = -1
log.Println("Called newModelhandler() with mh=", mh)
spew.Dump(mh)
return mh
}
func standardColumnTypes() []ui.TableValue {
return []ui.TableValue{
ui.TableColor{}, // row background color
ui.TableString(""), // column 0 text
ui.TableColor{}, // column 0 text color
ui.TableString("test"), // column 1 button text
}
}
func makeTable(name string, rows int, row1Name string) (*ui.Table, *modelHandler, *ui.TableModel) {
mh := newModelHandler(rows)
mh.name = name
model := ui.NewTableModel(mh)
table := ui.NewTable(
&ui.TableParams{
Model: model,
RowBackgroundColorModelColumn: mh.bgcolorColumn,
})
table.AppendTextColumn(row1Name, 1, ui.TableModelColumnAlwaysEditable, nil)
table.AppendButtonColumn("Details", 3, ui.TableModelColumnAlwaysEditable)
return table, mh, model
}
func defaultCellValue(mh *modelHandler, row, column int) ui.TableValue {
switch column {
case 0:
if (row % 2) == 1 {
return ui.TableColor{0.5, 0.5, 0.5, .1}
}
return nil
case 1:
return ui.TableString(fmt.Sprintf("jcarrgood %d", row))
}
return ui.TableString(fmt.Sprintf("jcarrbad %d", row))
}
func defaultSetCellValue(mh *modelHandler, m *ui.TableModel, row, column int, value ui.TableValue) {
if column == 3 { // button row (?)
log.Println("Button was pressed START", row, column)
}
return
}