Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

73 datagrid display a custom text in the body of the grid when there are no data #97

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ver 1.2.0
- [feature] DataGrid: Added support for displaying horizontal border lines between any two content rows.
- [feature] DataGrid: Implemented grid MinWidth and MaxWidth.
- [feature] DataGrid: Added top and bottom padding to the data grid cells.
- [feature] DataGrid: Added ContentOverflow property for cells to control the overflow behavior.


ver 1.1.0
Expand Down
5 changes: 5 additions & 0 deletions sources/ConsoleTools/ConsoleTools.Controls.Tables/CellBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public abstract class CellBase
/// </summary>
public static int DefaultPaddingBottom => 0;

/// <summary>
/// Gets or sets the row that contains the current cell.
/// </summary>
public RowBase ParentRow { get; internal set; }

/// <summary>
/// Gets or sets the content of the cell.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions sources/ConsoleTools/ConsoleTools.Controls.Tables/ColumnList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,25 @@ public class ColumnList : IEnumerable<Column>
{
private readonly DataGrid parentDataGrid;
private readonly List<Column> columns = new();
private HeaderRow headerRow;

/// <summary>
/// Gets the number of columns contained in the current instance.
/// </summary>
public int Count => columns.Count;

public HeaderRow HeaderRow
{
get => headerRow;
set
{
headerRow = value;

foreach (Column column in columns)
column.HeaderCell.ParentRow = value;
}
}

/// <summary>
/// Gets the <see cref="Column"/> at the specified index.
/// If the index is outside of the bounds of the list, <c>null</c> is returned.
Expand Down
45 changes: 8 additions & 37 deletions sources/ConsoleTools/ConsoleTools.Controls.Tables/ContentCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public class ContentCell : CellBase
{
private int columnSpan = 1;

/// <summary>
/// Gets or sets the row that contains the current cell.
/// </summary>
public ContentRow ParentRow { get; internal set; }

/// <summary>
/// Gets or sets a value that specifies across how many columns should the cell be displayed.
/// </summary>
Expand Down Expand Up @@ -188,22 +183,10 @@ public override int CalculatePaddingRight()
[Obsolete("Intended for internal usage only.")]
public override ConsoleColor? CalculateForegroundColor()
{
ConsoleColor? color = ForegroundColor;
if (color != null)
return color;

color = ParentRow?.ForegroundColor;
if (color != null)
return color;

Column column = GetColumn();
color = column?.ForegroundColor;
if (color != null)
return color;

color = ParentRow?.ParentDataGrid?.ForegroundColor;

return color;
return ForegroundColor
?? ParentRow?.ForegroundColor
?? GetColumn()?.ForegroundColor
?? ParentRow?.ParentDataGrid?.ForegroundColor;
}

/// <summary>
Expand All @@ -213,22 +196,10 @@ public override int CalculatePaddingRight()
[Obsolete("Intended for internal usage only.")]
public override ConsoleColor? CalculateBackgroundColor()
{
ConsoleColor? color = BackgroundColor;
if (color != null)
return color;

color = ParentRow?.BackgroundColor;
if (color != null)
return color;

Column column = GetColumn();
color = column?.BackgroundColor;
if (color != null)
return color;

color = ParentRow?.ParentDataGrid?.BackgroundColor;

return color;
return BackgroundColor
?? ParentRow?.BackgroundColor
?? GetColumn()?.BackgroundColor
?? ParentRow?.ParentDataGrid?.BackgroundColor;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ public ContentCell AddCell(object cellContent)
/// Returns the index of the specified cell.
/// If the cell is not part of the current <see cref="ContentRow"/> instance, returns <c>null</c>.
/// </summary>
public int? IndexOfCell(ContentCell cell)
public override int? IndexOfCell(CellBase cell)
{
int indexOfCell = cells.IndexOf(cell);
if (cell is not ContentCell contentCell)
return null;

int indexOfCell = cells.IndexOf(contentCell);

return indexOfCell == -1
? null
Expand Down
29 changes: 29 additions & 0 deletions sources/ConsoleTools/ConsoleTools.Controls.Tables/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class DataGrid : BlockControl
private TitleRow titleRow;
private HeaderRow headerRow;
private DataGridBorder border;
private EmptyMessageRow emptyMessageRow;
private FooterRow footerRow;

/// <summary>
Expand Down Expand Up @@ -302,6 +303,30 @@ public bool IsBorderVisible
set => Border.IsVisible = value;
}

/// <summary>
/// Gets the row containing the message to be displayed in the content area when there is no
/// data in the grid.
/// </summary>
public EmptyMessageRow EmptyMessageRow
{
get => emptyMessageRow;
private set
{
emptyMessageRow = value;
emptyMessageRow.ParentDataGrid = this;
}
}

/// <summary>
/// Gets or sets the text to be displayed in the content area when there is no data in the
/// grid.
/// </summary>
public MultilineText EmptyMessage
{
get => EmptyMessageRow.EmptyMessageCell.Content;
set => EmptyMessageRow.EmptyMessageCell.Content = value;
}

/// <summary>
/// Initializes a new instance of the <see cref="DataGrid"/> class.
/// </summary>
Expand All @@ -312,6 +337,7 @@ public DataGrid()
HeaderRow = new HeaderRow(Columns);
TitleRow = new TitleRow();
FooterRow = new FooterRow();
EmptyMessageRow = new EmptyMessageRow();
Border = new DataGridBorder();
}

Expand All @@ -326,6 +352,7 @@ public DataGrid(string title)
HeaderRow = new HeaderRow(Columns);
TitleRow = new TitleRow(title);
FooterRow = new FooterRow();
EmptyMessageRow = new EmptyMessageRow();
Border = new DataGridBorder();
}

Expand All @@ -340,6 +367,7 @@ public DataGrid(MultilineText title)
HeaderRow = new HeaderRow(Columns);
TitleRow = new TitleRow(title);
FooterRow = new FooterRow();
EmptyMessageRow = new EmptyMessageRow();
Border = new DataGridBorder();
}

Expand All @@ -354,6 +382,7 @@ public DataGrid(object title)
HeaderRow = new HeaderRow(Columns);
TitleRow = new TitleRow(title);
FooterRow = new FooterRow();
EmptyMessageRow = new EmptyMessageRow();
Border = new DataGridBorder();
}

Expand Down
117 changes: 117 additions & 0 deletions sources/ConsoleTools/ConsoleTools.Controls.Tables/EmptyMessageCell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// ConsoleTools
// Copyright (C) 2017-2024 Dust in the Wind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;

namespace DustInTheWind.ConsoleTools.Controls.Tables;

/// <summary>
/// Represents the sing;e cell displayed in the <see cref="EmptyMessageRow"/>.
/// It contains a message to be displayed to the user in the content area when the
/// <see cref="DataGrid"/> contains no content rows.
/// </summary>
public class EmptyMessageCell : CellBase
{
/// <summary>
/// Initializes a new instance of the <see cref="EmptyMessageCell" /> class with
/// empty content.
/// </summary>
public EmptyMessageCell()
{
PaddingLeft = 10;
PaddingTop = 1;
PaddingRight = 10;
PaddingBottom = 1;
HorizontalAlignment = HorizontalAlignment.Center;
}

/// <inheritdoc />
[Obsolete("Intended for internal usage only.")]
public override ConsoleColor? CalculateForegroundColor()
{
return ForegroundColor
?? ParentRow?.ForegroundColor
?? ParentRow?.ParentDataGrid?.ForegroundColor;
}

/// <inheritdoc />
[Obsolete("Intended for internal usage only.")]
public override ConsoleColor? CalculateBackgroundColor()
{
return BackgroundColor
?? ParentRow?.BackgroundColor
?? ParentRow?.ParentDataGrid?.BackgroundColor;
}

/// <inheritdoc />
[Obsolete("Intended for internal usage only.")]
public override int CalculatePaddingLeft()
{
int? paddingLeft = PaddingLeft;
if (paddingLeft != null)
return paddingLeft.Value;

paddingLeft = DefaultPaddingLeft;

return paddingLeft.Value;
}

/// <inheritdoc />
[Obsolete("Intended for internal usage only.")]
public override int CalculatePaddingRight()
{
int? paddingRight = PaddingRight;
if (paddingRight != null)
return paddingRight.Value;

paddingRight = DefaultPaddingRight;

return paddingRight.Value;
}

/// <inheritdoc />
[Obsolete("Intended for internal usage only.")]
public override HorizontalAlignment CalculateHorizontalAlignment()
{
HorizontalAlignment alignment = HorizontalAlignment;
if (alignment != HorizontalAlignment.Default)
return alignment;

alignment = CalculateHorizontalAlignmentAtRowLevel();
if (alignment != HorizontalAlignment.Default)
return alignment;

alignment = DefaultHorizontalAlignment;

return alignment;
}

private HorizontalAlignment CalculateHorizontalAlignmentAtRowLevel()
{
return ParentRow?.CellHorizontalAlignment ?? HorizontalAlignment.Default;
}

internal override CellContentOverflow ComputeContentOverflow()
{
CellContentOverflow contentOverflow = ContentOverflow;
if (contentOverflow != CellContentOverflow.Default)
return contentOverflow;

contentOverflow = DefaultContentOverflow;

return contentOverflow;
}
}
Loading
Loading