This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/BookmarkEditForm.cs

132 lines
3.9 KiB
C#

using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using Form = System.Windows.Forms.Form;
using Resources = bzit.bomg.Properties.Resources;
namespace bzit.bomg
{
public partial class BookmarkEditForm : Form
{
protected bool isBusy = false;
protected BookmarkItemModel itemModel = null;
protected BookmarkTreeView treeView = null;
public BookmarkEditForm(BookmarkTreeView treeview) : base()
{
InitializeComponent();
treeView = treeview;
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
if (treeView.SNode.GetNodeType() == BookmarkTreeView.NodeType.Page)
{
BookmarkItemViewModel bookmarkItem = (BookmarkItemViewModel)treeView.SelectedNode.Tag;
if (bookmarkItem != null)
{
if (itemModel == null)
{
itemModel = new BookmarkItemModel();
}
updateViewModel(bookmarkItem);
}
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
protected bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
textBox1.Enabled = textBox2.Enabled = memoBox1.Enabled = !value;
ThreadControl.SetImage(pictureBox1, (value) ? Resources.aniZomq2x32 : null);
}
}
protected void updateViewModel(BookmarkItemViewModel viewModel)
{
itemModel.SiteName = viewModel.SiteName?.Trim();
itemModel.SiteAddress = viewModel.SiteAddress?.Trim();
itemModel.SiteDescription = viewModel.SiteDescription?.Trim();
itemModel.FaviconAddress = viewModel.FaviconAddress?.Trim();
textBox1.Text = itemModel.SiteName;
textBox2.Text = itemModel.SiteAddress;
memoBox1.Text = itemModel.SiteDescription;
pictureBox2.Image = treeView.ImageList.Images[treeView.SelectedNode.ImageIndex];
}
private async void button1_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text))
{
return;
}
this.IsBusy = true;
await Task.Run(() =>
{
if (itemModel == null) itemModel = new BookmarkItemModel();
itemModel.SiteAddress = textBox2.Text;
bool rv = itemModel.Update();
if (rv)
{
ThreadControl.SetText(textBox1, itemModel.SiteName);
ThreadControl.SetText(textBox2, itemModel.SiteAddress);
ThreadControl.SetText(memoBox1, itemModel.SiteDescription);
ThreadControl.SetImage(pictureBox2, itemModel.RetrieveFavicon());
}
});
this.IsBusy = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy) return;
if (string.IsNullOrWhiteSpace(textBox1.Text)) return;
if (string.IsNullOrWhiteSpace(textBox2.Text)) return;
if (itemModel == null) itemModel = new BookmarkItemModel();
itemModel.SiteName = textBox1.Text?.Trim();
itemModel.SiteAddress = textBox2.Text?.Trim();
itemModel.SiteDescription = memoBox1.Text?.Trim();
treeView?.SNode.AddOrUpdateItem(itemModel.ToViewModel());
this.Close();
}
}
}