2019-05-06 02:54:49 -05:00
|
|
|
// based off andlabs/ui/examples/table.go
|
2019-05-06 02:32:46 -05:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
2019-05-07 18:58:44 -05:00
|
|
|
// import "os"
|
2019-05-06 02:54:49 -05:00
|
|
|
import "log"
|
2019-05-06 02:32:46 -05:00
|
|
|
import "github.com/andlabs/ui"
|
2019-05-06 19:42:59 -05:00
|
|
|
import _ "github.com/andlabs/ui/winmanifest"
|
2019-05-06 15:50:10 -05:00
|
|
|
import "github.com/davecgh/go-spew/spew"
|
|
|
|
|
2019-05-06 02:54:49 -05:00
|
|
|
type vmRowData struct {
|
|
|
|
hostname string
|
|
|
|
size int
|
|
|
|
IPv6 string
|
|
|
|
IPv4 string
|
|
|
|
memory int
|
|
|
|
disk int
|
|
|
|
}
|
|
|
|
|
2019-05-06 18:41:59 -05:00
|
|
|
type uiColumn struct {
|
|
|
|
offset int
|
|
|
|
name string
|
|
|
|
what ui.TableValue
|
|
|
|
color ui.TableColor
|
|
|
|
|
|
|
|
// ui.TableString(""), // column 4 button text
|
|
|
|
// ui.TableColor{}, // column 1 text color
|
|
|
|
// ui.TableImage{}, // column 1 image
|
|
|
|
// ui.TableInt(0), // column 3 checkbox state
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:32:46 -05:00
|
|
|
type modelHandler struct {
|
2019-05-06 16:14:32 -05:00
|
|
|
name string
|
2019-05-06 16:22:40 -05:00
|
|
|
rows int
|
2019-05-06 19:33:29 -05:00
|
|
|
bgcolorColumn int
|
2019-05-06 02:32:46 -05:00
|
|
|
yellowRow int
|
2019-05-06 16:22:40 -05:00
|
|
|
checkStates []int
|
|
|
|
vms []vmRowData
|
2019-05-07 18:58:44 -05:00
|
|
|
columnTypes string
|
2019-05-06 02:32:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-06 16:14:32 -05:00
|
|
|
func newDefaultModelHandler() *modelHandler {
|
2019-05-06 15:50:10 -05:00
|
|
|
mh := new(modelHandler)
|
2019-05-06 16:22:40 -05:00
|
|
|
mh.rows = 20
|
|
|
|
mh.checkStates = make([]int, mh.rows)
|
|
|
|
mh.vms = make([]vmRowData, mh.rows)
|
2019-05-06 15:50:10 -05:00
|
|
|
mh.vms[8].hostname = "fire"
|
|
|
|
mh.vms[9].hostname = "librem15.this.is.a.really.long.string.test"
|
|
|
|
mh.yellowRow = -1
|
2019-05-07 18:42:16 -05:00
|
|
|
log.Println("Called newDefaultModelhandler() with mh=", mh)
|
2019-05-06 15:50:10 -05:00
|
|
|
spew.Dump(mh)
|
|
|
|
return mh
|
2019-05-06 02:32:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-06 16:14:32 -05:00
|
|
|
func newJcarrModelHandler() *modelHandler {
|
|
|
|
mh := new(modelHandler)
|
2019-05-06 16:22:40 -05:00
|
|
|
mh.rows = 10
|
2019-05-07 18:58:44 -05:00
|
|
|
mh.columnTypes = "standard"
|
2019-05-06 19:33:29 -05:00
|
|
|
mh.bgcolorColumn = 0
|
2019-05-06 16:22:40 -05:00
|
|
|
mh.checkStates = make([]int, mh.rows)
|
|
|
|
mh.vms = make([]vmRowData, mh.rows)
|
2019-05-06 16:14:32 -05:00
|
|
|
mh.vms[8].hostname = "jcarr"
|
|
|
|
mh.vms[9].hostname = "jcarr2"
|
|
|
|
mh.yellowRow = -1
|
2019-05-07 18:42:16 -05:00
|
|
|
log.Println("Called newJcarrModelhandler() with mh=", mh)
|
2019-05-06 16:14:32 -05:00
|
|
|
spew.Dump(mh)
|
|
|
|
return mh
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:42:16 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:32:46 -05:00
|
|
|
func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue {
|
2019-05-06 16:14:32 -05:00
|
|
|
// log.Println("ColumnTypes() with m=", m, "mh=", mh)
|
2019-05-07 18:58:44 -05:00
|
|
|
if (mh.columnTypes == "standard") {
|
2019-05-07 18:42:16 -05:00
|
|
|
return standardColumnTypes()
|
2019-05-07 18:58:44 -05:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
}
|
|
|
|
*/
|
2019-05-06 16:14:32 -05:00
|
|
|
}
|
2019-05-06 02:32:46 -05:00
|
|
|
return []ui.TableValue{
|
|
|
|
ui.TableString(""), // column 0 text
|
|
|
|
ui.TableString(""), // column 1 text
|
|
|
|
ui.TableString(""), // column 2 text
|
2019-05-06 15:50:10 -05:00
|
|
|
ui.TableColor{}, // row background color
|
|
|
|
ui.TableColor{}, // column 1 text color
|
2019-05-06 02:32:46 -05:00
|
|
|
ui.TableImage{}, // column 1 image
|
|
|
|
ui.TableString(""), // column 4 button text
|
|
|
|
ui.TableInt(0), // column 3 checkbox state
|
|
|
|
ui.TableInt(0), // column 5 progress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var img [2]*ui.Image
|
|
|
|
|
|
|
|
func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableValue {
|
2019-05-06 15:50:10 -05:00
|
|
|
// TODO: Figure out why this is being called 1000 times a second (10 times for each row & column)
|
|
|
|
// log.Println("CallValue() row=", row, "column=", column)
|
2019-05-07 18:58:44 -05:00
|
|
|
if (mh.columnTypes == "standard") {
|
2019-05-06 16:14:32 -05:00
|
|
|
switch column {
|
2019-05-06 16:45:44 -05:00
|
|
|
case 0:
|
|
|
|
if (row % 2) == 1 {
|
|
|
|
return ui.TableColor{0.5, 0.5, 0.5, .1}
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-06 16:14:32 -05:00
|
|
|
case 1:
|
|
|
|
return ui.TableString(fmt.Sprintf("jcarrgood %d", row))
|
|
|
|
}
|
|
|
|
return ui.TableString(fmt.Sprintf("jcarrbad %d", row))
|
|
|
|
// panic("unreachable")
|
|
|
|
}
|
2019-05-06 02:32:46 -05:00
|
|
|
switch column {
|
|
|
|
case 0:
|
|
|
|
return ui.TableString(fmt.Sprintf("Row %d", row))
|
|
|
|
case 1:
|
|
|
|
return ui.TableString("Colors!")
|
|
|
|
case 7:
|
|
|
|
return ui.TableInt(mh.checkStates[row])
|
|
|
|
case 8:
|
|
|
|
if row == 0 {
|
|
|
|
return ui.TableInt(0)
|
|
|
|
}
|
|
|
|
if row == 13 {
|
|
|
|
return ui.TableInt(100)
|
|
|
|
}
|
|
|
|
if row == 14 {
|
|
|
|
return ui.TableInt(-1)
|
|
|
|
}
|
|
|
|
return ui.TableInt(50)
|
|
|
|
case 5:
|
|
|
|
if row < 8 {
|
|
|
|
return ui.TableImage{img[0]}
|
|
|
|
}
|
|
|
|
return ui.TableImage{img[1]}
|
|
|
|
case 4:
|
|
|
|
if (row % 2) == 1 {
|
|
|
|
return ui.TableColor{0.5, 0, 0.75, 1}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case 3:
|
|
|
|
if row == mh.yellowRow {
|
2019-05-06 15:50:10 -05:00
|
|
|
return ui.TableColor{1, 1, 0, .1}
|
2019-05-06 02:32:46 -05:00
|
|
|
}
|
|
|
|
if row == 3 {
|
2019-05-06 15:50:10 -05:00
|
|
|
// Red with .1 transparency
|
|
|
|
// R G B Trans
|
|
|
|
return ui.TableColor{1, 0, 0, .1}
|
2019-05-06 02:32:46 -05:00
|
|
|
}
|
|
|
|
if row == 11 {
|
|
|
|
return ui.TableColor{0, 0.5, 1, 0.5}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case 2:
|
2019-05-06 02:54:49 -05:00
|
|
|
return ui.TableString(mh.vms[row].hostname)
|
2019-05-06 02:32:46 -05:00
|
|
|
case 6:
|
|
|
|
return ui.TableString("Make Yellow")
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *modelHandler) SetCellValue(m *ui.TableModel, row, column int, value ui.TableValue) {
|
2019-05-06 15:50:10 -05:00
|
|
|
log.Println("SetCallValue() START")
|
|
|
|
spew.Dump(m)
|
2019-05-06 16:14:32 -05:00
|
|
|
spew.Dump(mh)
|
2019-05-07 18:58:44 -05:00
|
|
|
if (mh.columnTypes == "standard") {
|
2019-05-06 16:45:44 -05:00
|
|
|
if column == 3 { // button row (?)
|
|
|
|
log.Println("Button was pressed START", row, column)
|
|
|
|
}
|
2019-05-06 16:14:32 -05:00
|
|
|
return
|
|
|
|
}
|
2019-05-06 02:54:49 -05:00
|
|
|
if column == 2 {
|
|
|
|
mh.vms[row].hostname = string(value.(ui.TableString))
|
2019-05-06 02:32:46 -05:00
|
|
|
}
|
|
|
|
if column == 6 { // row background color
|
|
|
|
prevYellowRow := mh.yellowRow
|
|
|
|
mh.yellowRow = row
|
|
|
|
if prevYellowRow != -1 {
|
|
|
|
m.RowChanged(prevYellowRow)
|
|
|
|
}
|
|
|
|
m.RowChanged(mh.yellowRow)
|
|
|
|
}
|
|
|
|
if column == 7 { // checkboxes
|
|
|
|
mh.checkStates[row] = int(value.(ui.TableInt))
|
|
|
|
}
|
2019-05-06 15:50:10 -05:00
|
|
|
log.Println("SetCallValue() END")
|
|
|
|
spew.Dump(m)
|
2019-05-06 02:32:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-06 16:14:32 -05:00
|
|
|
func makeDemotable(name string) *ui.Table {
|
2019-05-06 02:32:46 -05:00
|
|
|
img[0] = ui.NewImage(16, 16)
|
|
|
|
img[1] = ui.NewImage(16, 16)
|
|
|
|
|
2019-05-06 16:14:32 -05:00
|
|
|
mh := newDefaultModelHandler()
|
|
|
|
mh.name = name
|
2019-05-06 02:32:46 -05:00
|
|
|
model := ui.NewTableModel(mh)
|
|
|
|
|
|
|
|
table := ui.NewTable(
|
|
|
|
&ui.TableParams{
|
|
|
|
Model: model,
|
|
|
|
RowBackgroundColorModelColumn: 3,
|
|
|
|
})
|
|
|
|
|
|
|
|
table.AppendTextColumn("Column 1", 0, ui.TableModelColumnNeverEditable, nil)
|
|
|
|
|
|
|
|
table.AppendImageTextColumn("Column 2", 5,
|
|
|
|
1, ui.TableModelColumnNeverEditable, &ui.TableTextColumnOptionalParams{
|
|
|
|
ColorModelColumn: 4,
|
|
|
|
});
|
|
|
|
|
2019-05-06 02:54:49 -05:00
|
|
|
table.AppendTextColumn("hostname", 2, ui.TableModelColumnAlwaysEditable, nil)
|
2019-05-06 02:32:46 -05:00
|
|
|
table.AppendCheckboxColumn("Checkboxes", 7, ui.TableModelColumnAlwaysEditable)
|
|
|
|
table.AppendButtonColumn("Buttons", 6, ui.TableModelColumnAlwaysEditable)
|
|
|
|
table.AppendProgressBarColumn("Progress Bar", 8)
|
|
|
|
|
|
|
|
return table
|
|
|
|
}
|
2019-05-06 15:50:10 -05:00
|
|
|
|
2019-05-06 18:41:59 -05:00
|
|
|
func makeJcarrtable(name string) (*ui.Table, *modelHandler, *ui.TableModel) {
|
2019-05-06 16:14:32 -05:00
|
|
|
mh := newJcarrModelHandler()
|
|
|
|
mh.name = name
|
2019-05-06 15:50:10 -05:00
|
|
|
model := ui.NewTableModel(mh)
|
|
|
|
|
|
|
|
table := ui.NewTable(
|
|
|
|
&ui.TableParams{
|
|
|
|
Model: model,
|
2019-05-06 19:33:29 -05:00
|
|
|
RowBackgroundColorModelColumn: mh.bgcolorColumn,
|
2019-05-06 15:50:10 -05:00
|
|
|
})
|
|
|
|
|
2019-05-06 16:14:32 -05:00
|
|
|
table.AppendTextColumn("hostname", 1, ui.TableModelColumnNeverEditable, nil)
|
2019-05-06 16:45:44 -05:00
|
|
|
table.AppendButtonColumn("Details", 3, ui.TableModelColumnAlwaysEditable)
|
2019-05-06 15:50:10 -05:00
|
|
|
|
2019-05-06 18:41:59 -05:00
|
|
|
return table, mh, model
|
2019-05-06 15:50:10 -05:00
|
|
|
}
|
2019-05-07 18:42:16 -05:00
|
|
|
|
|
|
|
func newModelHandler(rows int) *modelHandler {
|
|
|
|
mh := new(modelHandler)
|
|
|
|
mh.rows = rows
|
2019-05-07 18:58:44 -05:00
|
|
|
mh.columnTypes = "standard"
|
2019-05-07 18:42:16 -05:00
|
|
|
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 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.TableModelColumnNeverEditable, nil)
|
|
|
|
table.AppendButtonColumn("Details", 3, ui.TableModelColumnAlwaysEditable)
|
|
|
|
|
|
|
|
return table, mh, model
|
|
|
|
}
|