140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
// based off andlabs/ui/examples/table.go
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
import "log"
|
|
// import "os"
|
|
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 jwcTest struct {
|
|
part0 ui.TableColor // row background color
|
|
part1 ui.TableString // column 0 text
|
|
part2 ui.TableColor // column 0 text color
|
|
part3 ui.TableString // column 1 button text
|
|
}
|
|
|
|
type modelHandler struct {
|
|
name string
|
|
rows int
|
|
bgcolorColumn int
|
|
yellowRow int
|
|
checkStates []int
|
|
vms []vmRowData
|
|
bgcolor []ui.TableColor
|
|
|
|
cellValues []jwcTest
|
|
|
|
columnTypes string
|
|
funcColumnTypes func() []ui.TableValue
|
|
scanCellValue func(*modelHandler, int, int) ui.TableValue
|
|
setCellValue func(*modelHandler, *ui.TableModel, int, int, ui.TableValue)
|
|
}
|
|
|
|
func initValues(mh *modelHandler) {
|
|
for i := 0; i < mh.rows; i++ {
|
|
log.Println("i=",i)
|
|
mh.vms[i].hostname = fmt.Sprintf("jcarrnew %d", i)
|
|
|
|
mh.cellValues[i].part0 = ui.TableColor{0.1, 0.5, 0.5, .3}
|
|
mh.cellValues[i].part1 = ui.TableString(fmt.Sprintf("fun %d", i))
|
|
mh.cellValues[i].part2 = ui.TableColor{0.5, 0.1, 0.1, .7}
|
|
mh.cellValues[i].part3 = ui.TableString(fmt.Sprintf("awesome %d", i))
|
|
}
|
|
// os.Exit(-1)
|
|
}
|
|
|
|
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.bgcolor = make([]ui.TableColor, mh.rows)
|
|
|
|
mh.cellValues = make([]jwcTest, mh.rows)
|
|
|
|
mh.vms[1].hostname = "jcarr"
|
|
mh.yellowRow = -1
|
|
|
|
initValues(mh)
|
|
|
|
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:
|
|
return mh.cellValues[row].part0
|
|
case 1:
|
|
return mh.cellValues[row].part1
|
|
case 2:
|
|
return mh.cellValues[row].part2
|
|
case 3:
|
|
return mh.cellValues[row].part3
|
|
}
|
|
return ui.TableString(fmt.Sprintf("jcarrbad %d", row))
|
|
}
|
|
|
|
func defaultSetCellValue(mh *modelHandler, m *ui.TableModel, row, column int, value ui.TableValue) {
|
|
switch column {
|
|
case 0:
|
|
// mh.cellValues[row].part0 = ui.TableColor(value)
|
|
case 1:
|
|
mh.cellValues[row].part1 = value.(ui.TableString)
|
|
case 2:
|
|
// mh.cellValues[row].part2 = ui.TableColor(value)
|
|
case 3:
|
|
// mh.cellValues[row].part3 = ui.TableString(value)
|
|
log.Println("Button was pressed START", row, column)
|
|
}
|
|
log.Println("ui.TableValue=", value)
|
|
return
|
|
}
|