rui_orig/tableAdapter.go

329 lines
8.1 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
// TableAdapter describes the TableView content
2021-09-07 17:36:50 +03:00
type TableAdapter interface {
// RowCount returns number of rows in the table
2021-09-07 17:36:50 +03:00
RowCount() int
// ColumnCount returns number of columns in the table
2021-09-07 17:36:50 +03:00
ColumnCount() int
// Cell returns the contents of a table cell. The function can return elements of the following types:
// * string
// * rune
// * float32, float64
// * integer values: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64
// * bool
// * rui.Color
// * rui.View
// * fmt.Stringer
// * rui.VerticalTableJoin, rui.HorizontalTableJoin
2022-07-26 18:36:00 +03:00
Cell(row, column int) any
2021-09-07 17:36:50 +03:00
}
// TableColumnStyle describes the style of TableView columns.
// To set column styles, you must either implement the TableColumnStyle interface in the table adapter
// or assign its separate implementation to the "column-style" property.
2021-09-07 17:36:50 +03:00
type TableColumnStyle interface {
ColumnStyle(column int) Params
}
// TableRowStyle describes the style of TableView rows.
// To set row styles, you must either implement the TableRowStyle interface in the table adapter
// or assign its separate implementation to the "row-style" property.
2021-09-07 17:36:50 +03:00
type TableRowStyle interface {
RowStyle(row int) Params
}
// TableCellStyle describes the style of TableView cells.
// To set row cells, you must either implement the TableCellStyle interface in the table adapter
// or assign its separate implementation to the "cell-style" property.
2021-09-07 17:36:50 +03:00
type TableCellStyle interface {
CellStyle(row, column int) Params
}
// TableAllowCellSelection determines whether TableView cell selection is allowed.
// It is only used if the "selection-mode" property is set to CellSelection (1).
// To set cell selection allowing, you must either implement the TableAllowCellSelection interface
// in the table adapter or assign its separate implementation to the "allow-selection" property.
type TableAllowCellSelection interface {
AllowCellSelection(row, column int) bool
}
// TableAllowRowSelection determines whether TableView row selection is allowed.
// It is only used if the "selection-mode" property is set to RowSelection (2).
// To set row selection allowing, you must either implement the TableAllowRowSelection interface
// in the table adapter or assign its separate implementation to the "allow-selection" property.
type TableAllowRowSelection interface {
AllowRowSelection(row int) bool
}
// SimpleTableAdapter is implementation of TableAdapter where the content
2022-07-26 18:36:00 +03:00
// defines as [][]any.
// When you assign [][]any value to the "content" property, it is converted to SimpleTableAdapter
2021-09-07 17:36:50 +03:00
type SimpleTableAdapter interface {
TableAdapter
TableCellStyle
}
type simpleTableAdapter struct {
2022-07-26 18:36:00 +03:00
content [][]any
2021-09-07 17:36:50 +03:00
columnCount int
}
// TextTableAdapter is implementation of TableAdapter where the content
// defines as [][]string.
// When you assign [][]string value to the "content" property, it is converted to TextTableAdapter
2021-09-07 17:36:50 +03:00
type TextTableAdapter interface {
TableAdapter
}
type textTableAdapter struct {
content [][]string
columnCount int
}
// NewTextTableAdapter is an auxiliary structure. It used as cell content and
// specifies that the cell should be merged with the one above it
2021-09-07 17:36:50 +03:00
type VerticalTableJoin struct {
}
// HorizontalTableJoin is an auxiliary structure. It used as cell content and
// specifies that the cell should be merged with the one before it
2021-09-07 17:36:50 +03:00
type HorizontalTableJoin struct {
}
// NewSimpleTableAdapter creates the new SimpleTableAdapter
2022-07-26 18:36:00 +03:00
func NewSimpleTableAdapter(content [][]any) SimpleTableAdapter {
2021-09-07 17:36:50 +03:00
if content == nil {
return nil
}
adapter := new(simpleTableAdapter)
adapter.content = content
adapter.columnCount = 0
for _, row := range content {
if row != nil {
columnCount := len(row)
if adapter.columnCount < columnCount {
adapter.columnCount = columnCount
}
}
}
return adapter
}
func (adapter *simpleTableAdapter) RowCount() int {
if adapter.content != nil {
return len(adapter.content)
}
return 0
}
func (adapter *simpleTableAdapter) ColumnCount() int {
return adapter.columnCount
}
2022-07-26 18:36:00 +03:00
func (adapter *simpleTableAdapter) Cell(row, column int) any {
2021-09-07 17:36:50 +03:00
if adapter.content != nil && row >= 0 && row < len(adapter.content) &&
adapter.content[row] != nil && column >= 0 && column < len(adapter.content[row]) {
return adapter.content[row][column]
}
return nil
}
func (adapter *simpleTableAdapter) CellStyle(row, column int) Params {
if adapter.content == nil {
return nil
}
getColumnSpan := func() int {
count := 0
for i := column + 1; i < adapter.columnCount; i++ {
next := adapter.Cell(row, i)
switch next.(type) {
case HorizontalTableJoin:
count++
default:
return count
}
}
return count
}
getRowSpan := func() int {
rowCount := len(adapter.content)
count := 0
for i := row + 1; i < rowCount; i++ {
next := adapter.Cell(i, column)
switch next.(type) {
case VerticalTableJoin:
count++
default:
return count
}
}
return count
}
columnSpan := getColumnSpan()
rowSpan := getRowSpan()
var params Params = nil
if rowSpan > 0 {
params = Params{RowSpan: rowSpan + 1}
}
if columnSpan > 0 {
if params == nil {
params = Params{ColumnSpan: columnSpan + 1}
} else {
params[ColumnSpan] = columnSpan
}
}
return params
}
// NewTextTableAdapter creates the new TextTableAdapter
2021-09-07 17:36:50 +03:00
func NewTextTableAdapter(content [][]string) TextTableAdapter {
if content == nil {
return nil
}
adapter := new(textTableAdapter)
adapter.content = content
adapter.columnCount = 0
for _, row := range content {
if row != nil {
columnCount := len(row)
if adapter.columnCount < columnCount {
adapter.columnCount = columnCount
}
}
}
return adapter
}
func (adapter *textTableAdapter) RowCount() int {
if adapter.content != nil {
return len(adapter.content)
}
return 0
}
func (adapter *textTableAdapter) ColumnCount() int {
return adapter.columnCount
}
2022-07-26 18:36:00 +03:00
func (adapter *textTableAdapter) Cell(row, column int) any {
2021-09-07 17:36:50 +03:00
if adapter.content != nil && row >= 0 && row < len(adapter.content) &&
adapter.content[row] != nil && column >= 0 && column < len(adapter.content[row]) {
return adapter.content[row][column]
}
return nil
}
2023-05-04 16:45:03 +03:00
type simpleTableLineStyle struct {
2021-09-07 17:36:50 +03:00
params []Params
}
2023-05-04 16:45:03 +03:00
func (style *simpleTableLineStyle) ColumnStyle(column int) Params {
if column < len(style.params) {
params := style.params[column]
2021-09-07 17:36:50 +03:00
if len(params) > 0 {
return params
}
}
return nil
}
2023-05-04 16:45:03 +03:00
func (style *simpleTableLineStyle) RowStyle(row int) Params {
if row < len(style.params) {
params := style.params[row]
if len(params) > 0 {
return params
2021-09-07 17:36:50 +03:00
}
}
2023-05-04 16:45:03 +03:00
return nil
}
2021-09-07 17:36:50 +03:00
2023-05-04 16:45:03 +03:00
func (table *tableViewData) setLineStyle(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
switch value := value.(type) {
case []Params:
2023-05-04 16:45:03 +03:00
if len(value) > 0 {
style := new(simpleTableLineStyle)
style.params = value
table.properties[tag] = style
2021-09-07 17:36:50 +03:00
} else {
2023-05-04 16:45:03 +03:00
delete(table.properties, tag)
2021-09-07 17:36:50 +03:00
}
case DataNode:
2023-05-04 16:45:03 +03:00
if params := value.ArrayAsParams(); len(params) > 0 {
style := new(simpleTableLineStyle)
style.params = params
table.properties[tag] = style
2021-09-07 17:36:50 +03:00
} else {
2023-05-04 16:45:03 +03:00
delete(table.properties, tag)
2021-09-07 17:36:50 +03:00
}
default:
return false
}
return true
}
2023-05-04 16:45:03 +03:00
func (table *tableViewData) setRowStyle(value any) bool {
switch value := value.(type) {
case TableRowStyle:
table.properties[RowStyle] = value
}
return table.setLineStyle(RowStyle, value)
}
2021-09-07 17:36:50 +03:00
func (table *tableViewData) getRowStyle() TableRowStyle {
for _, tag := range []string{RowStyle, Content} {
if value := table.getRaw(tag); value != nil {
if style, ok := value.(TableRowStyle); ok {
return style
}
}
}
return nil
}
2022-07-26 18:36:00 +03:00
func (table *tableViewData) setColumnStyle(value any) bool {
2021-09-07 17:36:50 +03:00
switch value := value.(type) {
case TableColumnStyle:
table.properties[ColumnStyle] = value
}
2023-05-04 16:45:03 +03:00
return table.setLineStyle(ColumnStyle, value)
2021-09-07 17:36:50 +03:00
}
func (table *tableViewData) getColumnStyle() TableColumnStyle {
for _, tag := range []string{ColumnStyle, Content} {
if value := table.getRaw(tag); value != nil {
if style, ok := value.(TableColumnStyle); ok {
return style
}
}
}
return nil
}
func (table *tableViewData) getCellStyle() TableCellStyle {
for _, tag := range []string{CellStyle, Content} {
if value := table.getRaw(tag); value != nil {
if style, ok := value.(TableCellStyle); ok {
return style
}
}
}
return nil
}