Skip to content

Commit

Permalink
Added import and export for TableEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
thesupersonic16 committed Sep 28, 2024
1 parent dc4c1a1 commit b04c82a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
8 changes: 8 additions & 0 deletions DALTools/DALLib/ImportExport/TranslationCSVFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class TranslationCSVFile : TranslationBase

public override string TypeExtension => ".csv";

public TranslationCSVFile() { }

public TranslationCSVFile(string text) : this()
{
_buffer = text;
_bufferIndex = 0;
}

public override TranslationLine[] ImportTranslation(string data)
{
var lines = new List<TranslationLine>();
Expand Down
8 changes: 8 additions & 0 deletions DALTools/DALLib/ImportExport/TranslationTSVFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class TranslationTSVFile : TranslationBase

public override string TypeExtension => ".tsv";

public TranslationTSVFile() { }

public TranslationTSVFile(string text) : this()
{
_buffer = text;
_bufferIndex = 0;
}

public override TranslationLine[] ImportTranslation(string data)
{
var lines = new List<TranslationLine>();
Expand Down
2 changes: 2 additions & 0 deletions DALTools/TableEditor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Background="#323235">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="About" VerticalAlignment="Top" Margin="0,8,10,0" Height="23" Width="65" Click="AboutButton_Click" />
<Button Content="Export" VerticalAlignment="Top" Margin="0,8,10,0" Height="23" Width="65" Click="ExportButton_Click" />
<Button Content="Import" VerticalAlignment="Top" Margin="0,8,10,0" Height="23" Width="65" Click="ImportButton_Click" />
<Button Content="Load" VerticalAlignment="Top" Margin="0,8,10,0" Height="23" Width="65" Click="LoadButton_Click" />
<Button Content="Save" VerticalAlignment="Top" Margin="0,8,10,0" Height="23" Width="65" Click="SaveButton_Click" />
</StackPanel>
Expand Down
101 changes: 96 additions & 5 deletions DALTools/TableEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DALLib.File;
using DALLib.ImportExport;
using Microsoft.Win32;
using System;
using System.Collections;
Expand Down Expand Up @@ -28,11 +29,8 @@ public partial class MainWindow : Window, INotifyPropertyChanged
protected List<ObservableCollection<string>> TableData { get; set; }
protected string FilePath;

public void LoadTable(string path)
public void UpdateUI()
{
Table = new TableFile();
Table.Load(path);

TableData = new List<ObservableCollection<string>>();
for (int i = 0; i < Table.Rows.Count; i++)
{
Expand All @@ -43,8 +41,16 @@ public void LoadTable(string path)
// TODO: Fix header not shrinking
Editor.ColumnHeadersSource = Table.Columns.Select((x, i) => x.Name).ToList();
Editor.ItemsSource = TableData;
Title = $"{App.ProgramName} - {Path.GetFileName(FilePath)}";
}

public void LoadTable(string path)
{
Table = new TableFile();
Table.Load(path);

FilePath = path;
Title = $"{App.ProgramName} - {Path.GetFileName(path)}";
UpdateUI();
}

public void SaveTable(string path)
Expand All @@ -68,6 +74,54 @@ public void SaveTable(string path)
}
}

public void ExportTable(string path, string ext)
{
string text = "";
if (ext.ToLowerInvariant() == ".csv")
{
var file = new TranslationCSVFile();
text += file.AddRow(Table.Columns.Select(x => x.Name).ToArray());
Table.Rows.ForEach(x => text += file.AddRow(x));
} else if (ext.ToLowerInvariant() == ".tsv")
{
var file = new TranslationTSVFile();
text += file.AddRow(Table.Columns.Select(x => x.Name).ToArray());
Table.Rows.ForEach(x => text += file.AddRow(x));
}

File.WriteAllText(path, text, Encoding.UTF8);
}

public void ImportTable(string path, string ext)
{
string text = File.ReadAllText(path, Encoding.UTF8);
Table.Rows.Clear();
if (ext.ToLowerInvariant() == ".csv")
{
var file = new TranslationCSVFile(text);
file.ReadCSVRow(); // Skip header
while (true)
{
string[] split = file.ReadCSVRow();
if (split == null)
break; // EOF
Table.Rows.Add(split);
}
}
else if (ext.ToLowerInvariant() == ".tsv")
{
var file = new TranslationTSVFile();
file.ReadTSVRow(); // Skip header
while (true)
{
string[] split = file.ReadTSVRow();
if (split == null)
break; // EOF
Table.Rows.Add(split);
}
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (App.Args.Length > 0 && File.Exists(App.Args[0]))
Expand Down Expand Up @@ -117,5 +171,42 @@ private void AboutButton_Click(object sender, RoutedEventArgs e)
{
new AboutWindow().ShowDialog();
}

private void ExportButton_Click(object sender, RoutedEventArgs e)
{
if (Table == null)
{
MessageBox.Show("No table is loaded.", "Export Error",
MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var sfd = new SaveFileDialog
{
Filter = "Comma-Separated Values (*.csv)|*.csv|Tab-Separated Values (*.tsv)|*.tsv",
FileName = Path.ChangeExtension(FilePath, "csv")
};
if (sfd.ShowDialog() == true)
{
ExportTable(sfd.FileName, Path.GetExtension(sfd.FileName));
}
}

private void ImportButton_Click(object sender, RoutedEventArgs e)
{
if (Table == null)
{
MessageBox.Show("No table is loaded.", "Import Error",
MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var sfd = new OpenFileDialog
{
Filter = "Comma-Separated Values (*.csv)|*.csv|Tab-Separated Values (*.tsv)|*.tsv",
FileName = Path.ChangeExtension(FilePath, "csv")
};
if (sfd.ShowDialog() == true)
ImportTable(sfd.FileName, Path.GetExtension(sfd.FileName));
UpdateUI();
}
}
}

0 comments on commit b04c82a

Please sign in to comment.