CPF/CPF.ReoGrid/Actions/RemoveColumnsAction.cs
2024-06-24 10:15:59 +08:00

121 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using CPF.ReoGrid.Common;
using CPF.ReoGrid.Outline;
namespace CPF.ReoGrid.Actions
{
public class RemoveColumnsAction : WorksheetReusableAction
{
public RemoveColumnsAction(int column, int count) : base(new RangePosition(0, column, -1, count))
{
}
public override void Do()
{
bool flag = base.Range.Cols == -1;
if (flag)
{
Logger.Log("remove columns", "attempt to remove all columns but worksheet must be have at least one column, operation aborted.");
}
else
{
this.backupWidths = new int[base.Range.Cols];
for (int i = base.Range.Col; i <= base.Range.EndCol; i++)
{
this.backupWidths[i - base.Range.Col] = (int)base.Worksheet.RetrieveColumnHeader(i).InnerWidth;
}
this.backupData = base.Worksheet.GetPartialGrid(base.Range);
Debug.Assert(this.backupData != null);
base.Worksheet.DeleteColumns(base.Range.Col, base.Range.Cols, this);
}
}
public override void Undo()
{
bool flag = base.Range.Cols == -1;
if (flag)
{
Logger.Log("remove columns", "attempt to undo removing all columns but grid must have one column, operation aborted.");
}
else
{
base.Worksheet.InsertColumns(base.Range.Col, base.Range.Cols);
base.Worksheet.SetColumnsWidth(base.Range.Col, base.Range.Cols, (int col) => this.backupWidths[col - base.Range.Col], true, true);
bool flag2 = this.backupData == null;
if (flag2)
{
Logger.Log("remove rows", "no backup data");
Debug.Assert(false, "why no backup data here?");
}
base.Worksheet.SetPartialGrid(base.Range, this.backupData);
bool flag3 = this.changedOutlines != null;
if (flag3)
{
foreach (KeyValuePair<IReoGridOutline, BackupRangeInfo> keyValuePair in this.changedOutlines)
{
keyValuePair.Key.Start = keyValuePair.Value.start;
keyValuePair.Key.Count = keyValuePair.Value.count;
}
}
bool flag4 = this.deletedOutlines != null;
if (flag4)
{
foreach (IReoGridOutline reoGridOutline in this.deletedOutlines)
{
base.Worksheet.GroupColumns(reoGridOutline.Start, reoGridOutline.Count);
}
}
bool flag5 = this.deletedNamedRanges != null;
if (flag5)
{
foreach (NamedRange namedRange in this.deletedNamedRanges)
{
base.Worksheet.AddNamedRange(namedRange);
}
}
bool flag6 = this.deletedHighlightRanges != null;
if (flag6)
{
foreach (HighlightRange range in this.deletedHighlightRanges)
{
base.Worksheet.AddHighlightRange(range);
}
}
bool flag7 = this.changedOutlines != null && this.changedOutlines.Count > 0;
if (flag7)
{
base.Worksheet.UpdateViewportController();
}
}
}
public override string GetName()
{
return "Remove Columns";
}
public override WorksheetReusableAction Clone(RangePosition range)
{
return new RemoveColumnsAction(range.Col, range.Cols);
}
private PartialGrid backupData = null;
private int[] backupWidths;
internal List<NamedRange> deletedNamedRanges;
internal List<HighlightRange> deletedHighlightRanges;
internal Dictionary<NamedRange, BackupRangeInfo> changedNamedRange;
internal Dictionary<HighlightRange, BackupRangeInfo> changedHighlightRanges;
internal List<IReoGridOutline> deletedOutlines;
internal Dictionary<IReoGridOutline, BackupRangeInfo> changedOutlines;
}
}