2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TableAdapter describes the [TableView] content
|
2021-09-07 17:36:50 +03:00
|
|
|
type TableAdapter interface {
|
2022-01-16 20:25:45 +03:00
|
|
|
// RowCount returns number of rows in the table
|
2021-09-07 17:36:50 +03:00
|
|
|
RowCount() int
|
2022-01-16 20:25:45 +03:00
|
|
|
|
|
|
|
// ColumnCount returns number of columns in the table
|
2021-09-07 17:36:50 +03:00
|
|
|
ColumnCount() int
|
2022-01-16 20:25:45 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TableColumnStyle describes the style of [TableView] columns.
|
|
|
|
// To set column styles, you must either implement the [TableColumnStyle] interface in the table adapter
|
2022-01-16 20:25:45 +03:00
|
|
|
// or assign its separate implementation to the "column-style" property.
|
2021-09-07 17:36:50 +03:00
|
|
|
type TableColumnStyle interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// ColumnStyle returns a map of properties which describe the style of the column
|
2021-09-07 17:36:50 +03:00
|
|
|
ColumnStyle(column int) Params
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TableRowStyle describes the style of [TableView] rows.
|
|
|
|
// To set row styles, you must either implement the [TableRowStyle] interface in the table adapter
|
2022-01-16 20:25:45 +03:00
|
|
|
// or assign its separate implementation to the "row-style" property.
|
2021-09-07 17:36:50 +03:00
|
|
|
type TableRowStyle interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// RowStyle returns a map of properties which describe the style of the row
|
2021-09-07 17:36:50 +03:00
|
|
|
RowStyle(row int) Params
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TableCellStyle describes the style of [TableView] cells.
|
|
|
|
// To set row cells, you must either implement the [TableCellStyle] interface in the table adapter
|
2022-01-16 20:25:45 +03:00
|
|
|
// or assign its separate implementation to the "cell-style" property.
|
2021-09-07 17:36:50 +03:00
|
|
|
type TableCellStyle interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// CellStyle returns a map of properties which describe the style of the cell
|
2021-09-07 17:36:50 +03:00
|
|
|
CellStyle(row, column int) Params
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TableAllowCellSelection determines whether [TableView] cell selection is allowed.
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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.
|
2022-01-15 02:06:10 +03:00
|
|
|
type TableAllowCellSelection interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// AllowCellSelection returns "true" if we allow the user to select particular cell at specific rows and columns
|
2022-01-15 02:06:10 +03:00
|
|
|
AllowCellSelection(row, column int) bool
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TableAllowRowSelection determines whether [TableView] row selection is allowed.
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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.
|
2022-01-15 02:06:10 +03:00
|
|
|
type TableAllowRowSelection interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// AllowRowSelection returns "true" if we allow the user to select particular row in the table
|
2022-01-15 02:06:10 +03:00
|
|
|
AllowRowSelection(row int) bool
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// TextTableAdapter is implementation of [TableAdapter] where the content
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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 {
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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 {
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:25:45 +03:00
|
|
|
// 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
|
|
|
|
}
|