I18NCommander/I18N Commander/UI WinForms/Components/TextElements.cs

96 lines
2.8 KiB
C#
Raw Normal View History

2022-07-12 18:50:42 +00:00
using DataModel.Database;
using Processor;
using UI_WinForms.Dialogs;
2022-07-12 18:50:42 +00:00
namespace UI_WinForms.Components;
2022-07-11 17:52:15 +00:00
public partial class TextElements : UserControl
{
2022-07-12 18:50:42 +00:00
private Section? currentSection;
2022-07-11 17:52:15 +00:00
public TextElements()
{
this.InitializeComponent();
2022-07-12 18:50:42 +00:00
// Check if we are in the designer:
if(Program.SERVICE_PROVIDER is null)
return;
2022-07-12 18:50:42 +00:00
// When the section is changed, update this component:
AppEvents.WhenSectionChanged += async (sender, section) =>
2022-07-12 18:50:42 +00:00
{
this.currentSection = section;
this.buttonAdd.Enabled = this.currentSection is not null;
// Update the path:
if (this.currentSection is null)
return;
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey);
this.LoadTextElements();
2022-07-12 18:50:42 +00:00
};
2022-07-11 17:52:15 +00:00
}
// Loads all the text elements for the current section.
private async void LoadTextElements()
{
if (this.currentSection is null)
return;
// Load the text elements:
var textElements = await TextElementProcessor.GetTextElements(this.currentSection);
// Update the list:
this.listTextElements.Items.Clear();
foreach (var textElement in textElements)
{
var item = new ListViewItem(textElement.Name)
{
Tag = textElement.Code,
};
this.listTextElements.Items.Add(item);
}
}
private async void buttonAdd_Click(object sender, EventArgs e)
{
if(this.DesignMode)
return;
var result = InputDialog.Show(new InputDialog.Options(
Message: "Please type the desired text element's name.",
Title: "Add a text element",
Placeholder: "My text element",
ShowQuestionCheckbox: false
));
if(result.DialogResult == DialogResult.Cancel)
return;
// Add the text element to the database into the current section:
var newTextElement = await TextElementProcessor.AddTextElement(this.currentSection?.DataKey, result.Text);
newTextElement.ProcessError();
if(!newTextElement.Successful)
return;
// Add the text element to the list:
var item = new ListViewItem(newTextElement.Result!.Name)
{
Tag = newTextElement.Result.Code,
};
this.listTextElements.Items.Add(item);
}
private void buttonRemove_Click(object sender, EventArgs e)
{
}
private void buttonRename_Click(object sender, EventArgs e)
{
}
2022-07-11 17:52:15 +00:00
}