Skip to content

Commit a57cd76

Browse files
authored
Merge pull request #23 from mmeents/develop
Adds Modified property to track if anything changed. worked out sorti…
2 parents 3c25565 + cf58a20 commit a57cd76

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

PackedTables.Viewer.Winforms/PackedTables.Viewer/Form1.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ private void DoOpenFileTable() {
9292
}
9393

9494
private void DoCloseFileTable() {
95+
96+
if (_workingPack != null && _workingPack!.Modified) {
97+
if (MessageBox.Show("You have unsaved changes. Do you want to save before closing?", "Unsaved Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
98+
if (!string.IsNullOrEmpty(_fileName)) {
99+
_workingPack.SaveToFile(_fileName);
100+
AddFileToMRUL(_fileName);
101+
} else {
102+
if (saveDialog.ShowDialog() == DialogResult.OK) {
103+
_fileName = saveDialog.FileName;
104+
_workingPack.SaveToFile(_fileName);
105+
AddFileToMRUL(_fileName);
106+
}
107+
}
108+
}
109+
}
110+
95111
btnOpenClose.Text = "Open";
96112
btnBrowse.Visible = true;
97113
comboBox1.Visible = true;
@@ -101,6 +117,7 @@ private void DoCloseFileTable() {
101117
LogMsg(_fileName + " closed.");
102118
_column = null;
103119
_table = null;
120+
_workingPack = null;
104121
splitContainer3_Panel1_Resize(null, null);
105122
}
106123

@@ -109,8 +126,7 @@ private void btnOpenClose_Click(object sender, EventArgs e) {
109126
if (btnOpenClose.Text == "Open") {
110127
_fileName = comboBox1.SelectedItem?.ToString();
111128
DoOpenFileTable();
112-
} else if (btnOpenClose.Text == "Close") {
113-
_workingPack = null;
129+
} else if (btnOpenClose.Text == "Close") {
114130
DoCloseFileTable();
115131
}
116132
}
@@ -329,6 +345,7 @@ private void vrMain_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseE
329345
} else {
330346
OrderByColumnName = columnName;
331347
}
348+
_table.RebuildRowIndex();
332349
LoadDataGridViewFromTable(_table!);
333350
}
334351

src/FieldModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ public Object Value {
4343
}
4444
}
4545
throw new Exception($"FieldModel:Column ValueType {this.ValueType} does not match new data type {FieldExt.GetColumnType(value)}");
46+
}
47+
if (this.OwnerRow != null && this.OwnerRow.Owner != null && this.OwnerRow.Owner.Owner != null) {
48+
this.OwnerRow.Owner.Owner.Modified = true;
4649
}
47-
4850
}
4951
}
5052
}

src/PackedTableSet.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ namespace PackedTables.Net
44
{
55
//
66
public class PackedTableSet {
7-
private string _FileName { get; set; } = "";
7+
private string _fileName { get; set; } = "";
8+
private bool _modified { get; set; } = false;
9+
public bool Modified {
10+
get { return _modified; }
11+
set { _modified = value; }
12+
}
813
private DataSetModel _Package { get; set; } = new DataSetModel();
914

1015
public PackedTableSet() {}
@@ -32,6 +37,7 @@ public void LoadFromBase64String(string base64) {
3237
ResetOwnership();
3338
}
3439
}
40+
_modified = false;
3541
}
3642

3743
private void ResetOwnership() {
@@ -48,7 +54,8 @@ private void ResetOwnership() {
4854

4955
public void LoadFromFile(string fileName) {
5056
if (File.Exists(fileName)) {
51-
_FileName = fileName;
57+
_fileName = fileName;
58+
_modified = false;
5259
var encoded = Task.Run(async () => await fileName.ReadAllTextAsync().ConfigureAwait(false)).GetAwaiter().GetResult();
5360
LoadFromBase64String(encoded);
5461
}
@@ -57,6 +64,7 @@ public void LoadFromFile(string fileName) {
5764
public void SaveToFile(string fileName) {
5865
var base64 = SaveToBase64String();
5966
Task.Run(async () => await base64.WriteAllTextAsync(fileName).ConfigureAwait(false)).GetAwaiter().GetResult();
67+
_modified = false;
6068
}
6169

6270
public string SaveToJson() {
@@ -73,6 +81,7 @@ public void LoadFromJson(string json) {
7381
ResetOwnership();
7482
}
7583
}
84+
_modified = false;
7685
}
7786
#endregion
7887

@@ -106,6 +115,7 @@ public TableModel AddTable(string tableName) {
106115
};
107116
_Package.Tables[tableNew.Id] = tableNew;
108117
_Package.NameIndex[tableName] = tableNew.Id;
118+
_modified = true;
109119
return tableNew;
110120
}
111121

@@ -127,6 +137,7 @@ public void RemoveTable(string tableName) {
127137
if (table != null) {
128138
_Package.Tables.TryRemove(table.Id, out _);
129139
_Package.NameIndex.TryRemove(tableName, out _);
140+
_modified = true;
130141
} else {
131142
throw new Exception($"Table {tableName} does not exist.");
132143
}

src/PackedTables.Net.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/mmeents/PackedTables.NET</PackageProjectUrl>
1212
<PackageTags>MessagePack Tables</PackageTags>
1313
<PackageLicenseFile>LICENSE</PackageLicenseFile>
14-
<Version>1.1.1</Version>
14+
<Version>1.1.2</Version>
1515
<Title>PackedTables.NET</Title>
1616
<Authors>$(AssemblyName)</Authors>
1717
<Copyright> Copyright © 2025 MattMeents</Copyright>

src/RowModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ public FieldModel this[string columnName] {
5858
if (value != null) {
5959
if (value.ColumnId != columnId) throw new Exception("ColumnId mismatch");
6060
RowFields[value.Id] = value;
61+
if (this.Owner != null && this.Owner.Owner != null) {
62+
this.Owner.Owner!.Modified = true;
63+
}
6164
} else {
6265
var field = RowFields?.FirstOrDefault(x => x.Value.ColumnId == columnId);
6366
if (field != null) {
64-
RowFields!.Remove(field.Value.Value.Id, out var field1);
67+
RowFields!.Remove(field.Value.Value.Id, out var field1);
68+
if (this.Owner != null && this.Owner.Owner != null) {
69+
this.Owner.Owner!.Modified = true;
70+
}
6571
}
6672
}
6773
}

src/TableModel.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public ColumnModel AddColumn(string columnName, ColumnType columnType) {
7979
};
8080
row.RowFields[field.Id] = field;
8181
}
82+
if (this.Owner != null ) {
83+
this.Owner.Modified = true;
84+
}
8285
return column;
8386
}
8487

@@ -94,6 +97,9 @@ public void RemoveColumn(int columnId) {
9497
_ = this._columnOrderByRankToIdIndex.TryRemove(column.Rank, out _);
9598
this.Columns.Remove(columnId, out var columnM);
9699
this.RebuildColumnIndex();
100+
if (this.Owner != null) {
101+
this.Owner.Modified = true;
102+
}
97103
}
98104
}
99105

@@ -187,13 +193,19 @@ public RowModel AddRow() {
187193
}
188194
}
189195
Current = newRow;
196+
if (this.Owner != null) {
197+
this.Owner.Modified = true;
198+
}
190199
return newRow;
191200
}
192201
public void RemoveRow(RowModel row) {
193202
if (row == null) return;
194203
if (Rows.ContainsKey(row.Id)) {
195204
_ = Rows.TryRemove(row.Id, out _);
196205
RebuildRowIndex();
206+
if (this.Owner != null) {
207+
this.Owner.Modified = true;
208+
}
197209
}
198210
}
199211

0 commit comments

Comments
 (0)